DEV Community

Cover image for Improve your JS skills with those tips #1

Improve your JS skills with those tips #1

Code Oz on March 13, 2021

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...
Collapse
 
heyrohit profile image
Rohit Gupta

Very Nice

Some of the features are from ES6 and later
For some older browsers, use indexOf in place of includes:

if (['a', 'b', 'c'].indexOf(value) > -1) { ... }
Enter fullscreen mode Exit fullscreen mode

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.

Collapse
 
kenovienadu profile image
Ovienadu Ken

Yea, we should take advantage of the newer syntax and utilize tools like babel to transpile for older browsers.

Collapse
 
codeoz profile image
Code Oz

Nice thank you Rohit!

Collapse
 
whaison profile image
Whaison • Edited

Nice Article

Btw, you don't have to use the double not (!!) operator in a condition. It is enough to just write

if (value) { 
  ...
}
Enter fullscreen mode Exit fullscreen mode

because an if statement checks the truthyness (or the falsyness) 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:

let item = response.body && response.body.item || 'default'
Enter fullscreen mode Exit fullscreen mode

-> If the body of the response is truthy and the item field is truthy: assign item = response.body.item. If either of them is falsy assign item = 'default'.

NOTE: in JavaScript the && and || operators work differently than in other languages

left && right === left  // if left is falsy
left && right === right // if left is truthy

left || right === right // if left is falsy
left || right === left  // if left is truthy
Enter fullscreen mode Exit fullscreen mode
Collapse
 
codeoz profile image
Code Oz

Nice to know this! Thank you for sharing this it's very interesting Whaison

Collapse
 
yuridevat profile image
Julia 👩🏻‍💻 GDE

Amazing article on how to make your code look more professional. Thanks for sharing these great tips!

Collapse
 
jlitowitz profile image
Jason Litowitz

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?

Collapse
 
epresas profile image
epresas • Edited

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!

Collapse
 
codeoz profile image
Code Oz

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

Collapse
 
nicozerpa profile image
Nico Zerpa (he/him)

I think he's trying to say that beginners use forEach in situations in which other methods such as map, 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.

Collapse
 
xr0master profile image
Sergey Khomushin

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.

Collapse
 
codeoz profile image
Code Oz

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!

Collapse
 
manoharreddyporeddy profile image
Manohar Reddy Poreddy
I have seen this multiple time :
    if (...) { // the condition is not important in this example
      return 'toto'
    } else {
      return 'tutu'
    }

If your if return value you can just replace the code above by :

    if (...) { // the condition is not important in this example
      return 'toto'
    }

    return 'tutu'
Enter fullscreen mode Exit fullscreen mode

should it be below ?

return (...) ? 'toto': 'tutu' ;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
codeoz profile image
Code Oz

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!

Collapse
 
thanhtanpm2000 profile image
ThanhTanPM2000

thanks you so much

Collapse
 
asimarra profile image
Angelica Maria

Thank you so much!

Collapse
 
danielatwood profile image
Daniel Atwood

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.

Collapse
 
ambyjkl profile image
Ambareesh "Amby" Balaji

Equally fast on Chrome 89: jsben.ch/O1q01
Modern browsers are clever.

Collapse
 
kennyrafael profile image
kennyrafael

Hi, awesome content! I have also a post about these and other tips. Here at dev.to

Collapse
 
bhagirath1312 profile image
Bhagirath Bhatti

Nice thank you for improve JS 🙂

Collapse
 
amandaolsen profile image
amandaolsen

Why use the Double !! when you could use the Boolean function?

Collapse
 
qq449245884 profile image
qq449245884

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.

Collapse
 
r2h_4869 profile image
R2h1

关注 了 Twitter 怎么获取Underrated skills in javascript, make the difference