10 JS tricks you *probably* didn't know

Shuvo - Oct 18 '21 - - Dev Community
  1. You can use + in front of a string to convert it into a number. console.log(typeof +"5") //number
  2. + in front of date object will convert in into number of milliseconds. console.log(+new Date()) //1634538267248
  3. | 0 after a floating point number will convert in into a integer. console.log(35.354 | 0) //35
  4. 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
  5. Use es6 to remove duplicates from array. console.log([...new Set([1,2,2,3,4,4,5])]) //[1, 2, 3, 4, 5]
  6. Converting Numbers array to Strings array console.log([1,2,2,3,4,4,5].map(String)) //['1', '2', '2', '3', '4', '4', '5']
  7. Converting String array to Numbers array console.log(['1', '2', '2', '3', '4', '4', '5'].map(Number)) //[1, 2, 2, 3, 4, 4, 5]
  8. HTML comment is valid in JavaScript WTF 🤣

    <!--Don't mind me I am just a comment-->
    console.log("Hello")
    
  9. Compare three values without using &&. console.log(3 > 2 < 5) //false

  10. Deep copy object using JSON.stringify and JSON.parse console.log(obj == JSON.parse(JSON.stringify(obj))) // false

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player