This content originally appeared on DEV Community and was authored by Shuvo
- You can use + in front of a string to convert it into a number.
console.log(typeof +"5") //number
-
+ in front of date object will convert in into number of milliseconds.
console.log(+new Date()) //1634538267248
-
| 0 after a floating point number will convert in into a integer.
console.log(35.354 | 0) //35
- If an array only contains one number you can use + in front of array to convert it into a number.
console.log(typeof +[6]) //number
- Use es6 to remove duplicates from array.
console.log([...new Set([1,2,2,3,4,4,5])]) //[1, 2, 3, 4, 5]
- Converting Numbers array to Strings array
console.log([1,2,2,3,4,4,5].map(String)) //['1', '2', '2', '3', '4', '4', '5']
- Converting String array to Numbers array
console.log(['1', '2', '2', '3', '4', '4', '5'].map(Number)) //[1, 2, 2, 3, 4, 4, 5]
-
HTML comment is valid in JavaScript WTF 🤣
<!--Don't mind me I am just a comment--> console.log("Hello")
Compare three values without using &&.
console.log(3 > 2 < 5) //false
Deep copy object using JSON.stringify and JSON.parse
console.log(obj == JSON.parse(JSON.stringify(obj))) // false
This content originally appeared on DEV Community and was authored by Shuvo
Shuvo | Sciencx (2021-10-18T06:51:33+00:00) 10 JS tricks you *probably* didn’t know. Retrieved from https://www.scien.cx/2021/10/18/10-js-tricks-you-probably-didnt-know/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.