In this article I will share with you some tips about JS that can improve your skills in JS !
Includes method to avoid multiples checking...
For further actions, you may consider blocking this person and/or reporting abuse
Very Nice
Some of the features are from ES6 and later
For some older browsers, use indexOf in place of includes:
While I encourage developing for clients that have no problems to adapt only mainstream browsers like chrome. firefox, you can persuade them to avoid using deprecated browser support for better code readability.
Yea, we should take advantage of the newer syntax and utilize tools like babel to transpile for older browsers.
Nice thank you Rohit!
Nice Article
Btw, you don't have to use the double not (
!!
) operator in a condition. It is enough to just writebecause an
if
statement checks thetruthy
ness (or thefalsy
ness) of the value.falsy
values are:false
0
(zero)""
(empty string)null
undefined
NaN
Everything else will be treated as
truthy
.The same rules apply to
Boolean(value)
and!!value
.This also works inside other boolean expressions so you can write for example:
->
If the body of the response istruthy
and the item field istruthy
: assignitem = response.body.item
. If either of them isfalsy
assignitem = 'default'
.Nice to know this! Thank you for sharing this it's very interesting Whaison
Amazing article on how to make your code look more professional. Thanks for sharing these great tips!
You suggested avoiding forEach, but provided no reason not to. Can you please explain why not? It accepts a function for its input parameter, just like all the other functional programming examples you provided. Is it, for example, slower than a loop? Is it more of a memory hog than map?
HI, I wouldn't avoid forEach in favor of map, each method basically does the same thing: given an array of elements, execute a callback function on each of the elements, the main difference for me is that while forEach mutates the original array, map instead returns a new array of the same size with the result of the callback on each element of the original array, which is something to consider if you might need the original data later on your code.
If you want to "do something" with the data (logging it, storing it), you may use forEach without problems, but if you want to modify the data (the famous "double the number" example we see everywhere, or parse the data from an array of objects in any way), map is your best friend, because it will return a new array with the result of your operations.
I'm not 100% sure, but I think map is faster than forEach, and another cool feature is method chaining; with map you can do something like
myArr.map().filter().reduce() // and so on
.Is just a matter of what are you trying to accomplish.
I hope this was helpful! Take care!
Thanks dude it the explication that my article miss about foreach!
You don't need need for each in order to change item in the current array.
It not respect fp (function al programming) if you use for each since you are currently mutating the array that are being iterated!
Moreover foreach function return nothing
I think he's trying to say that beginners use
forEach
in situations in which other methods such asmap
,reduce
, etc. are more appropriate.I don't really think you should avoid
forEach
completely. But it is a good idea to see if you can use any of the other of the FP methods first.It's a strange article for me.
TLDR; how to improve your JS skill? Use JS functionality! Okay...
IMO. You can take a higher level and, for example, explain why such code (@epresas ) is not very efficient in terms of performance.
myArr.map().filter().reduce() // and so on.
should it be below ?
hey thanks for your comment, yes it's working in case of you only need to return something! If you add more logic in the if or after you cannot use ternary (unlike you create function).
The other point is about else if! If you need to have else if is more complicated!
hey thanks for your comment, yes it's working in case of you only need to return something! If you add more logic in the if or after you cannot use ternary (unlike you create function).
The other point is about else if! If you need to have else if is more complicated!
thanks you so much
I think using a switch is better than Array.includes. Includes is very very slow. ~95% slower than using a normal if or switch statement.
Equally fast on Chrome 89: jsben.ch/O1q01
Modern browsers are clever.
Thank you so much!
Why use the Double !! when you could use the Boolean function?
Dear CodeOz,may I translate your all dev articles into Chinese?I would like to share it with more developers in China. I will give the original author and original source.
Nice thank you for improve JS 🙂
Hi, awesome content! I have also a post about these and other tips. Here at dev.to
关注 了 Twitter 怎么获取Underrated skills in javascript, make the difference