1. Declaring variables
2. Assigning values to multiple variables
We can assign values to multiple variables in one line with array destructuring.
3. The Ternary operator
We can save 5 lines of code here with ternary (conditional) operator.
4. Assigning default value
We can use OR(||) short circuit evaluation to assign a default value to a variable in case the expected value found falsy.
5. AND(&&) Short circuit evaluation
If you are calling a function only if a variable is true, then you can use AND(&&) short circuit as an alternative for this.
The AND(&&) short circuit shorthand is more useful in React when you want to conditionally render a component. For example:
6. Swap two variables
To swap two variables, we often use a third variable. We can swap two variables easily with array destructuring assignment.
7. Arrow Function
8. Template Literals
We normally use + operator to concatenate string values with variables. With ES6 template literals we can do it in a more simple way.
9. Multi-line String
For multiline string we normally use + operator with a new line escape sequence (\n). We can do it in an easier way by using backticks (`).
10. Multiple condition checking
For multiple value matching, we can put all values in array and use indexOf() or includes() method.
11. Object Property Assignment
12. String into a Number
There are built in methods like parseInt and parseFloat available to convert a string to number. We can also do this by simply providing a unary operator (+) in front of string value.
13. Repeat a string for multiple times
To repeat a string for a specified number of time you can use a for loop. But using the repeat() method we can do it in a single line.
14. Exponent Power
We can use Math.pow() method to find the power of a number. There is a shorter syntax to do it with double asterik (**).
15. Double NOT bitwise operator (~~)
The double NOT bitwise operator is a substitute for Math.floor() method.
16. Find max and min number in array
We can use for loop to loop through each value of array and find the max or min value. We can also use the Array.reduce() method to find the max and min number in array. But using spread operator we can do it in a single line.
17. For loop
To loop through an array we normally use the traditional for loop. We can make use of the for...of loop to iterate through arrays. To access the index of each value we can use for...in loop.
We can also loop through object properties using for...in loop.
18. Merging of arrays
19. Deep cloning of multi-level object
To deep clone a multi-level object, we can iterate through each property and check if the current property contains an object. If yes, then do a recursive call to the same function by passing the current property value (i.e. the nested object). We can also do it by using JSON.stringify() and JSON.parse() if our object doesn't contains functions, undefined, NaN or Date as values. If we have single level object i.e no nested object present, then we can deep clone using spread operator also.
20. Get character from string
Some of these shorthand techniques may seem inappropriate to use in a project, but it's a good idea to know some additional techniques. Happy coding and have a nice day!