Declare and Initialize Arrays in javascript
Find out the sum, minimum and maximum value in javascript
Sorting Array of String, Numbers or Objects ...
For further actions, you may consider blocking this person and/or reporting abuse
For getting maximum and minimum values from an array,
Math.max
andMath.min
are orders of magnitude faster thanreduce
.Also, for filling a 2D array - all you need do is:
There is no need for
map
(but spot the mistake! Fix below)True
Nope man, you cant. All sub arrays are linked in 1 array. If you change matrix[0][0] = 15, all matrix[n][0] = 15. You need map to do this!
I was wondering when someone would notice! Took long enough ๐
But you don't need
map
...its the same like a map :D. Just another variant)
Awesome, thank you for sharing!
Thanks bro.
As the js formatting thingy has already been commented by @lukeshiru just going to add that nesting ternaries are considered a bad practice.
Also don't use them if you need just an if
i.e.
What I added was a silly example just to showcase.
The fact that you can chain ternaries doesn't mean they are convenient the same way the fact you can use recursivity it doesn't mean it's convenient in most cases.
Yes, ternaries are "new", fancy and so but they are intended to cover a need, and that need is to shorthand an if-else statement with conditions:
You just can have, and should have a single statement on both the cases, when the condition evaluates to true and to falsy in the other hand.
There are other tools intended for whatever case that sits out of this definition.
If you begin chaining ternaries you'll end up with a mess while adding conditions to it, plus most code style guides format them as a single line (for the reasons named above, if you need to chain them, you don't want a ternary).
Last but not least; No, there's no need to cover the "else" always.
In such cases both
if
and&&
operators come in handy, depending on the situation.Plus ternaries can be substituted by
&&
and||
operators if you want a generic hammer:hahaha that's just the point I want to reach. Anything that the API provides can be used but using it as a single tool to rule them all is not so good.
This is a common pattern in react (as example), nothing wrong with it:
This is also good:
The question would be if you need this component or not.
I'm not against ternaries, I'm against nested ternaries, the same way I'm against short circuit if you are forcing the usage out of reasonable limits.
At the end all this discussion is subjective and we are discussing personal preferences, of course. It is interesting, either way to see other's point of view on that stuff.
Hi there, we encourage authors to share their entire posts here on DEV, rather than mostly pointing to an external link. Doing so helps ensure that readers donโt have to jump around to too many different pages, and it helps focus the conversation right here in the comments section.
If you choose to do so, you also have the option to add a canonical URL directly to your post.
Thanks Luke, this is really helpful!
Thanks for this! I am having time learning JavaScript and it is not clicking in my head, So thank you so much for this
๐ฅ
If you are storing unique values, use sets in javascript.