From frontend to backend, Javascript is everywhere and developers are high in demand.
Here are some amazing hacks to be a pro javascript developer.
- Remove falsy values from an array.
Passing Boolean in filter() will remove all falsy values that is undefined, null, 0, “” (empty string), NaN and false from an array.
2. Convert any value to boolean
Using !! in front of any value will convert that value to boolean.
3. Resizing array
You can resize array using it’s length property.
4. Flatten multi-dimensional array
You can easily flatten multi-dimensional arrays using the flat() method but a drawback is that is flattens the first level only in a multi-dimensional array.
But you can flatten any level of multi-dimensional array by passing ‘Infinity’ in the flat() method.
5. Short Conditionals
You can make use of && and || to shorten some conditionals.
6. Replace all occurrence in a string
With replace() method including regex( with g modifier) you can replace all the occurrences in a string.
7. Check performance of a task
You can use performance.now() method to check how much ms it took to complete a specific task.
Just wrap your task within performance.now() timestamps and done!!
8. Log values with variable names
You can basically wrap console.log() arguments with curly backend to also see the variable name in console.
If you liked my post, please do follow me !
Thank you for Reading ! Happy coding !