1. Check property exists in an object
The in operator returns the boolean value true/false.
The in operator returns true if a property exists in the object or its prototype chain.
2. String to a number using the plus (+) operator
The unary plus operator (+)
is the fastest and preferred way of converting something into a number.
3. Mask numbers using slice and padStart
The slice()
method returns selected elements in an array, as a new array. Negative numbers select from the end of the array.
The padStart()
method pads the current string with another string until the resulting string reaches the given length. The padding is applied from the start of the current string.
4. Swap two variables
Use destructuring assignment approach because it is short and expressive. Swapping is performed in just one line statement. It works with any data type like numbers, strings, booleans, or objects.
5. Check every element of the array using Array.every
The Array every() method checks whether all the array elements pass the test implemented by the provided function.
It returns true if the function returns true for all elements.
It returns false if the function returns false for one element. When every() finds a false result, it will stop the loop and continue no more which improves the performance.
The every() method does not change the original array.
6. Use of nullish coalescing operator with numbers
I have use lot of time || operator for the logical disjunction, but it ends when value is 0 in the variable. So found better way to handle this.
A Nullish value is a value that is either null or undefined.
The Nullish Coalescing Operator (??)
is a logical operator that accepts two values and returns the second value if the first one is null or undefined and otherwise returns the first value.
7. Filter Array with only values
Many times we have an issue with array to have empty values and need to handle we write extra code. To avoid it just you have to use filter().Pass the Boolean to Array.filter as the first argument.
No comments:
Post a Comment
Your feedback is always appreciated. I will try to reply to your queries as soon as time allows.Please don't spam,spam comments will be deleted upon reviews.