DEV Community

Cover image for Javascript in a Ninja Way

Javascript in a Ninja Way

Deepan on October 18, 2019

Hi Dev, Today I'm gonna share my 7 favorite Javascript shorthand tips that will look cool and clean on your code. Alright, Let's begin. ...
Collapse
 
savagepixie profile image
SavagePixie

The equivalent long hand expression will be
if (myValue === true)

Wouldn't the equivalent longhand expression be if (myValue == true)? Your example will return false unless myValue is a boolean.

Other than that, all those are great features of JavaScript.

Collapse
 
blakemealey profile image
Blake Mealey

Came to the comments to say this 😃

Collapse
 
patricklai profile image
Patrick

Yea I agree, that part is very misleading... its equiv to if (Boolean(myValue)) {...}

Collapse
 
deepanmania profile image
Deepan • Edited

Yeah, you are right. I'll change it right away. Thanks, patrik, Blake

Collapse
 
deepanmania profile image
Deepan

I assumed that it's a boolean. I was just trying to explain the truthy expressions.
I'll put appropriate comments. Thanks for the feedback.

Collapse
 
savagepixie profile image
SavagePixie

Oh, gotcha! I assumed you were checking that it was a truthy value.

Collapse
 
mlaufer profile image
Michael Laufer

I would never use "2) Decimal Values with trailing zeroes" and would strongly advise against using this in anything other than personal projects. The only benefit you get is confusing other developers, especially juniors.

Collapse
 
deepanmania profile image
Deepan

I think that up to one's perspective. We can easily see those expressions in mathematics.

Collapse
 
samholmes profile image
Sam Holmes • Edited

Cool trick for #5. You could even include parameters for require like so:

required = (f, p) => {
  throw new Error(`Parameter ${p} for ${f} required.`);
}

foo = (bar = required('foo', 'bar')) => {
  return bar;
}

Collapse
 
karataev profile image
Eugene Karataev

Nice list.
In the last example I prefer to use [1,2,3].includes(1) instead of ~[1,2,3].indexOf(1)

Collapse
 
juanfrank77 profile image
Juan F Gonzalez

Wow, guess I became a ninja and didn't even realize it... 6 out of 7 tricks. Not bad

Collapse
 
ristetloegpose profile image
jacobdo

Mandatory parameter seems very useful

Collapse
 
ryanpwalker profile image
Ryan Walker

const num1 = +"100" Does this shorthand work in all browsers?

Collapse
 
deepanmania profile image
Deepan

It's just usage of a unary operator '+'. So, I believe it will work the same on all browsers. But you cannot use const, instead of const use var since const is introduced in ES6.

Collapse
 
samholmes profile image
Sam Holmes • Edited

I think #6 is code smell (bad advice). Instead be concise and use parseInt, parseFloat, or Number for typecasting

Collapse
 
samholmes profile image
Sam Holmes • Edited

For #7 we could just use Array.prototype.includes() (developer.mozilla.org/en-US/docs/W...)

Collapse
 
vagoel profile image
Varun

I like the Mandatory Parameter way of throwing error for required fields.Its cool and enhances readability.
Thanks for sharing

Collapse
 
papidev profile image
Papidev

Uhm nice to know , but points 5,6 and 7 seem less readable to me. Personally I prefer longhand in those cases.