20 JavaScript Hacks You don't know!
Did you know?
NaN in JavaScript stands for Not-a-Number and it is used to indicate if a value is a legal number
Hello, are you a web developer?
If yes, then you must be using JavaScript very much for your projects. So this post is for you.
In this post I am going to make you aware of 20 great JavaScript hacks or features you don’t know! Using this hacks will make your coding life much easier and you don’t have to write more code and logics while making a project.
Then lets dive in...
- Swapping two Numbers
Swapping means interchanging values. In the above code we assigned the values 3 and 4 to the variables a and b respectively. Then to swap their values we used a simple method here other than using a third variable.
- Resize an array using array.length
In this method the array can be resized easily without rewriting or removing the elements manually. the
arr.length
assigns the new lenth to the array and then other elements are automatically removed. - Iterating on a map from 0 to n.
Iteration can be done in a easy way as above without using a long coded loop. Just one line of code to iterate through a map from 0 to n easily. and then the
console.log
method logs the output to console. - Using filter in a different Way
Filter can be used to remove null values so the error as written in the code can be solved using the filter method at the 6th line.
- Concatenating two or more arrays
While concatenating two arrays in the old times we were creating a new array and then use it to concatenate the 2 arrays. Which consumes a lot of memory. But now we using the push method the contents of one array can be pushed to another. Which results in use of less memory because no new array is created like the old method.
- Replace all occurrences of a word in a string
Above method can be used while replacing a word in an array easily.
- Shortcut for conditions
Above two ways can be used to make conditional codes smaller and save your time.
- String to Number or Number to String Conversion
The following method can be used to convert a String to Number or vice-versa in a simpler way.
- Using the console in a different way
- The console.table method
The output will show a table like in this image
- Get n power of any number
Generally, Everyone knows the above method but some begginers don't. So instead of using a loop we can use two astericks to find n power of a number.
- Check the false or true value
- typeof Operator
- Get the items near to last index of an array
- Call a function by its name stored in a string using eval function
- The label statement
- Rest parameters Syntax
- new.target in javascript
- function* in javascript
- yield keyword
Thanks for reading!
Keep visiting devtejas.me for more tricks and tips related to computer science.
1 comment