xinglinkedinenvelopebubblesgoogleplusfacebooktwitterfeedgithub

One more reason to check for strict equality in JavaScript

12th February 2014 | by Adam Beres-Deak | javascript, fun

Some JavaScript fun without further explanation.

var a = [0, 1, 2, 3, 4, 5, 6];
console.log(a[3]); // 3
console.log(a[[3]]); // 3
console.log(a[[[3]]]); // 3
console.log(a[[[[3]]]]); // 3
console.log(a[[[[[3]]]]]); // 3
console.log(a["3"]); // 3

console.log(3 == [3]); // true
console.log(3 == [[3]]); // true

console.log(3 === [[3]]); // false

3 === Number([3].valueOf().toString()) // true
console.log([3].valueOf()) // [3]
console.log([3].valueOf().toString()) // 3
console.log(Number("3")) // 3
// therefore:
3 === Number([3].valueOf().toString()) // true

Resources - questions on Stack Overflow:

by Adam Beres-Deak

| Share | Tweet | Share | Share

Latest blog posts

Displaying icons with custom elements 14th October 2015

Plain JavaScript event delegation 26th January 2015

After the first year of blogging - what happened on my blog in 2014? 1st January 2015

Better webfont loading with using localStorage and providing WOFF2 support 18th December 2014

Worth watching: Douglas Crockford speaking about the new good parts of JavaScript in 2014 20th October 2014