Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: strange comparison behaviour

Tags:

javascript

If I execute the following snippet in FireBug console it somehow prints surprise!:

['surprise!', 'boring'][Number(0=="​")]

But why?

UPD

I am sorry, people, that was a joke! Jere is the first who noticed! Yesterday I found a ZERO WIDTH SPACE in a string, and had since then temptation to have some fun =)

like image 450
newtover Avatar asked Jan 19 '12 14:01

newtover


1 Answers

There is an extra, non visible, character between your quotes.

If you type this out, you will get 'boring' because 0=="" evaluates to true, Number(true) evalutes to 1.

Paste these two and watch the differing output:

0=="​"

outputs false

0==""

outputs true

The only thing I have changed is deleting the character between "".

like image 146
Jere Avatar answered Sep 24 '22 13:09

Jere