- 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 +) //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]
<!--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
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (2)
Implicit type conversion is bad for readability and leads to errors.
Your "trick" number 9 prove my point : it does not compare 3 values like you say but instead evaluate
3 > 2which returns
false, then implicitly convert
0because a boolean cannot be compared to a number, then it returns the result of
0 < 5which is
2 > 0 < 1and you will see it does not work.
Try number 10 with a
Dateobject or any other object that define the
Be explicit about your type conversions. Using tricks like
+new Date()will just confuse beginners (and it requires you to memorize these "tricks". What's wrong with
new Date().getTime()(or even,
The last item is the worst here, though.
JSON.parse(JSON.stringify(obj))won't work in many cases. There are many cases, not even edge cases, where this won't perform a correct clone.
You lose any functions the source object has:
You lose any inherited properties from the prototype chain:
It messes with class instances too:
Use a proper deep clone function.
You are teaching beginners a lot of terrible practices with these posts tagged #beginners...