22 Javascript shorthands that will save your time

Save your valuable time

Shorthand doesn’t mean quality compromisation. If we can save time with a shorthand, then why not. Go for shorthand and save your valuable time. Here I will discuss 20 javascript shorthand. I hope you will like this.

1. Swap two variables

Array restructuring can be used to swap two values instead of a lengthy process.

2. The ternary operator

We can replace the if-else statement with the ternary operator, which will help you shorten your code

3. Variable declaration

When we need to declare multiple variables, we don’t need to write separate let/var/const and variable names. We can do it in a single line.

4.Assign values to multiple variable

Assign values to multiple variables is a hassle to assign again and again in a different variable. Here I will do it in a line.

5.Default value

The default value is possible to set based on a condition.

6.Template literals

Concatenate is a hassle when we use the + operator. Here we will use the ES6 template literals.

7.Arrow function

You can write a function using the arrow function shorter than the usual function we use.

8.Multiline string

A multiline string is possible with the + operator in Javascript, but we can use backticks (``) instead of + operator.

9.Multiple conditions

Check for multiple values is possible in a single line with indexOf() or includes() method.

10.String to number conversion

We use parseInt and parseFloat for a string to int and float conversion, but we can do it just using a + sign.

11.Object property assignment

When we assign value to an object from a variable then this is a good practice to keep a variable name as the key name of the object. This will be a shorthand if you maintain that. This will help to assign both keys and values. Here is an example.

12. Maximum and minimum number of an array

Instead of a loop, we can use shorthand to find a minimum or maximum number from an array.

13. Exponent power

We used Math.pow() for the power of a number. Now we can use ** instead of that.

14. Double not

We can use ~~ instead of Math.floor() . Only works for a 32 bit number.

15. String repeat

Suppose we need a string five times then a loop is a solution but we can do it in a single line.

16. For loop

We can use for of and for in instead of the usual for loop. Sometimes this can be a shorthand based on your use case.

17. Get character from string

You can directly use [] operator to access a character from a string.

18. Merge array

Array.concat() is widely used to merge array but we can use another operator to do that. Here is an example.

19. If true

We used conditions if a statement is true or false but we can do it in a short way.

20. Decimal base exponents

Here is a fancy way to write numbers without the trailing zeros.

Here are some evaluations

1e0 === 1;
1e1 === 10;
1e2 === 100;
1e3 === 1000;
1e4 === 10000;
1e5 === 100000; 

21. Default parameter value

We can replace if statement to define default values for function parameters in another way. Have a look.

Output

24
24

22. Object.entries()

ES8 introduced with this function that converts an object into a key, value pair array. Have a look.

Output

[
  [ 'name', 'shoaib' ],
  [ 'age', 26 ],
  [ 'email', 'shoaib.mehedi@gmail.com' ]
]

Conclusion

Here I mentioned 22 shorthand that I find useful. Please mention if I missed a useful one. Thank you.

Have a great day.

Leave a Reply

Your email address will not be published. Required fields are marked *