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

C++, C#, Java, PHP, ect...
Post Reply
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

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

Post 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?
Orgullo Catracho
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

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

Post by Xaos »

Faulty code? :lol:

I've got no idea. It seems impossible, logically.
Echo
Posts: 16
Joined: Wed Sep 28, 2011 5:01 pm

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

Post by Echo »

Not sure what you mean but...

Code: Select all

var a = 0;
console.log(a === 0); // true
console.log(a === 1); // false
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

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

Post 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!
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

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

Post 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 :)
The indelible lord of tl;dr
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

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

Post by Winawer »

Code: Select all

var a = {
	i: 0,
	valueOf: function() {
		return this.i++;
	}
}
User avatar
a_bertrand
Posts: 1536
Joined: Mon Feb 25, 2013 1:46 pm

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

Post 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 ;)
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

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

Post 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).
The indelible lord of tl;dr
Post Reply

Return to “Coding”