- 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