JavaScript has its quirks. In this blog post, we’ll explore the weird side of JavaScript that may leave you scratching your head. These quirks are not necessarily bad or wrong but are intriguing.
🧭Table Of Contents
- Arrays equal strings
- Arrays are not equal
- Numbers are sorted alphabetically
- String is not a string
- Null comparison
- NaN is a number
- Arrays and objects
- Boolean maths
- Conclusion
Arrays equal strings
var a = [0, 1, 2];
var b = [0, 1, 2];
var c = '0, 1, 2';
a == c // true
b == c // true
a == b // false
Arrays are not equal
['a', 'b'] !== ['a', 'b'] // true
['a', 'b'] != ['a', 'b'] // true
['a', 'b'] == ['a', 'b'] // false
Numbers are sorted alphabetically
[10, 9, 8, 3, 2, 1, 0].sort();
// [0, 1, 10, 2, 3, 8, 9];
String is not a string
typeof 'Arafat'; // "string"
'Arafat' instanceof String; // false
Null comparison
0 > null // false
0 < null // false
0 >= null // true
0 == null // false
0 <= null // true
typeof null // "object"
null instanceof Object // false
NaN is a number
typeof NaN // 'Number'
123 === 123 // true
NaN === NaN // false
Arrays and objects
[] + [] // ""
[] + {} // "[object Object]"
{} + [] // 0
{} + {} // NaN
Boolean maths
true + false // 1
true + true == true // false
Conclusion
Don't worry. It stops to get weird if you know why. 🙈
🎉Don't forget to save the post
Do Like 👍 & Share 🔄
Follow me for more amazing content related to Programming & Web Development 🚀
Credit: @thecodecrumbs🔥
Thanks for reading😊
Visit:
👨💻My Portfolio
🏞️My Fiverr
🌉My Github
🧙♂️My LinkedIn
Top comments (1)
For someone, it may look confusing (hell man, wtf js), but that is how js works under the hood. It is a dynamic typed language. Personally, for me, that is not the big deal cause I never ever use not type strict comparison.