Save your valuable time
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.
The default value is possible to set based on a condition.
Concatenate is a hassle when we use the
+ operator. Here we will use the ES6 template literals.
You can write a function using the arrow function shorter than the usual function we use.
A multiline string is possible with the
(``) instead of
Check for multiple values is possible in a single line with
10.String to number conversion
parseFloat for a
float conversion, but we can do it just using a
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
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.
ES8 introduced with this function that converts an object into a key, value pair array. Have a look.
[ [ 'name', 'shoaib' ], [ 'age', 26 ], [ 'email', 'firstname.lastname@example.org' ] ]
Here I mentioned 22 shorthand that I find useful. Please mention if I missed a useful one. Thank you.
Have a great day.