DEV Community

Cover image for The Weird Side Of JavaScript
Arafat
Arafat

Posted on

The Weird Side Of JavaScript

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

var a = [0, 1, 2];
var b = [0, 1, 2];
var c = '0, 1, 2';

a == c // true
b == c // true
a == b // false
Enter fullscreen mode Exit fullscreen mode

Arrays are not equal

['a', 'b'] !== ['a', 'b']    // true

['a', 'b'] != ['a', 'b']     // true

['a', 'b'] == ['a', 'b']     // false
Enter fullscreen mode Exit fullscreen mode

Numbers are sorted alphabetically

[10, 9, 8, 3, 2, 1, 0].sort();

// [0, 1, 10, 2, 3, 8, 9];
Enter fullscreen mode Exit fullscreen mode

String is not a string

typeof 'Arafat';             // "string"

'Arafat' instanceof String;  // false
Enter fullscreen mode Exit fullscreen mode

Null comparison

0 > null                // false

0 < null                // false

0 >= null               // true

0 == null               // false

0 <= null               // true

typeof null             // "object"

null instanceof Object  // false
Enter fullscreen mode Exit fullscreen mode

NaN is a number

typeof NaN       // 'Number'

123 === 123      // true

NaN === NaN      // false
Enter fullscreen mode Exit fullscreen mode

Arrays and objects

[] + []     // ""

[] + {}     // "[object Object]"

{} + []     // 0

{} + {}     // NaN
Enter fullscreen mode Exit fullscreen mode

Boolean maths

true + false           // 1

true + true == true    // false
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
cookiemonsterdev profile image
Mykhailo Toporkov 🇺🇦

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.