Shorthands (no matter what language we are working in) always help to make your code shorter, optimized and much cleaner. Here I want to recap some helpful shorthand coding techniques in Javascript that will surely help you a lot.

  1. Ternary operator

We can use “conditional ternary operator” instead of if … else statement and use only one line of code:

2. Null, Undefined or Empty

You can check if a variable is null, undefined or empty with a simple shorthand:

3. Convert string into a number

A very useful shorthand if you use some parseInt or parseFloat:

4. Default parameter values
Assign default values to variables:

5. Destructuring assignment
Is a very useful way to shorten your code with objects:

6. Declaring variables
A very similar shorthand like “destructuring objects” but now for multiple variables:

7. For loop
Other ways to use a loop, but with less code:

8. Shor-circuit logical operator

Another example for this is logical shorthand:

9. Spread operator
I like a lot this way to join and clone an array. After, I made concat() to join 2 arrays, and slice() to clone one. But now. Less code!

10. Assignment operators
This operators are very useful for arithmetics operations:

But we have more! Look this examples thinking that a = 20 and b = 10

11. Decimal base exponents
This shorthand is a way to write numbers without zeros. For example, 1e5 means that 1 is followed by 5 zeros (100.000)

12. Object.entries()
Honestly, I learned this shorthand a few days ago and I think it’s very important to add it in this article. This is a shorthand introduced in ES8 and allows you to convert a literal object into a key/value pair array:

13. Object.values()
This shorthand is very similar to Object.entries() but without the key. Is was introduced in ES8 too.

14. && (and) short circuit
You can get a fancy code with this shorthand. If you are calling a function only if a variable is true, you can use:

15. ~~ (double not bitwise operator)
This shorthand is a substitute for Math.floor() method:

16. Find max/min number in array
We can use a loop to find the max and min value in an array, but you can also use Math.max() and Math.min() and remove a few lines of code:

But, like we have seen, we can add a spread operator and create a new shorthand:

I hope you have learned some shorthands!

Junior frontend developer