10 JS tricks you *probably* didn’t know

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 wil…


This content originally appeared on DEV Community and was authored by Shuvo

  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


This content originally appeared on DEV Community and was authored by Shuvo


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » 10 JS tricks you *probably* didn’t know." Shuvo | Sciencx - Monday October 18, 2021, https://www.scien.cx/2021/10/18/10-js-tricks-you-probably-didnt-know/
HARVARD
Shuvo | Sciencx Monday October 18, 2021 » 10 JS tricks you *probably* didn’t know., viewed ,<https://www.scien.cx/2021/10/18/10-js-tricks-you-probably-didnt-know/>
VANCOUVER
Shuvo | Sciencx - » 10 JS tricks you *probably* didn’t know. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/18/10-js-tricks-you-probably-didnt-know/
CHICAGO
" » 10 JS tricks you *probably* didn’t know." Shuvo | Sciencx - Accessed . https://www.scien.cx/2021/10/18/10-js-tricks-you-probably-didnt-know/
IEEE
" » 10 JS tricks you *probably* didn’t know." Shuvo | Sciencx [Online]. Available: https://www.scien.cx/2021/10/18/10-js-tricks-you-probably-didnt-know/. [Accessed: ]
rf:citation
» 10 JS tricks you *probably* didn’t know | Shuvo | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.