DEV Community

Discussion on: Write beautiful and Elegant Javascript code with short-circuit evaluation.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Your short example will also give the length of a string, or any other object that has a length property... so strictly speaking, the code is not equivalent at all.

Also, a shorter way to do the same thing (as the second example) would be:

let length = ~~arr?.length
Enter fullscreen mode Exit fullscreen mode
Collapse
 
fayomihorace profile image
Horace FAYOMI

Indeed it will give the length of a string. Even if a string behave like an array of characthers, it is not really an array as Array.isArray() will return false.
So yes the two code are not striclty equivalent. But it will do the job in a short and more elegant way.
And the way you showed is really nice, thanks.