Page 1 of 1

[js] how can ((a == 0) && (a == 1) && (a == 2)) === true?

Posted: Fri Apr 25, 2014 4:47 am
by Ark
This is a javascript question. What value does a has to be in order for the next expression to be true?

Code: Select all

(a == 0) && (a == 1) && (a == 2)
In an article on the web it stated that there's a way for that expression to return true, and I'm curious as I've searched, and tried several ways but have no idea how to solve it.

Anyone knows what value does variable a has to be?

Re: [js] how can ((a == 0) && (a == 1) && (a == 2)) === true

Posted: Fri Apr 25, 2014 12:29 pm
by Xaos
Faulty code? :lol:

I've got no idea. It seems impossible, logically.

Re: [js] how can ((a == 0) && (a == 1) && (a == 2)) === true

Posted: Fri Apr 25, 2014 2:22 pm
by Echo
Not sure what you mean but...

Code: Select all

var a = 0;
console.log(a === 0); // true
console.log(a === 1); // false

Re: [js] how can ((a == 0) && (a == 1) && (a == 2)) === true

Posted: Sun Apr 27, 2014 7:09 pm
by Xaleph
Because there`s a distinct difference between the == operator and the === operator. Whereas the == ( equality ) operator checks the the type equality, the tripple equality checks for value equality. So, my advice is, allways use triple equality!

Re: [js] how can ((a == 0) && (a == 1) && (a == 2)) === true

Posted: Mon Apr 28, 2014 12:09 am
by Jackolantern
Welcome to the magical world of WTF JS. It is a weird little language with all kinds of odd decisions, but it is near and dear to my heart, and my favorite language :)

Re: [js] how can ((a == 0) && (a == 1) && (a == 2)) === true

Posted: Mon Apr 28, 2014 5:49 am
by Winawer

Code: Select all

var a = {
	i: 0,
	valueOf: function() {
		return this.i++;
	}
}

Re: [js] how can ((a == 0) && (a == 1) && (a == 2)) === true

Posted: Mon Apr 28, 2014 12:05 pm
by a_bertrand
Odd language for sure, and odd choices too. Basically all is derived from the fact that Netscape created a "scripting" language most likely following roughly the C syntax for the base. The scripting language at the beginning wasn't all that powerful, and was mostly used to check form fields, interact with Java applet and such.

Slowly new features and concept have been added to Javascript, yet they had to keep "compatibility" with the first version of it, otherwise all the websites using the old syntax would not work anymore. THIS is what produced this kind of odd mix we have today. Keeping compatibility is some times really good as you don't break existing things, but most of the time you make things a lot more clumsy as you will not be able to make clean stuff.

Certainly not my preferred language, but yet, offers loads of options to make your page a whole game by itself ;)

Re: [js] how can ((a == 0) && (a == 1) && (a == 2)) === true

Posted: Mon Apr 28, 2014 2:39 pm
by Jackolantern
Actually what really caused many of the oddities is that JS was created in something like 8 weeks. Most new languages require at least a year or longer for proper development, but Brandon Eich was tasked with having it ready for Netscape 2.0. Even today he says he didn't have nearly as much time as he wanted. So a lot of the stuff on WTF JS are things he never even tried during its development that simply resolve based on rules meant for other situations.

But yes, the style of JS has evolved a lot, mostly from the introduction of CSS. The old "document.write()" days of JS are what Brandon Eich originally created, so oddities dealing with DOM manipulation are newer and were introduced at later times (Javascript is older than the DOM).