== vs ===

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
Can anyone explain in a simpler term what is the difference of == operator to === in JavaScript in particular. Does it have a change when we compare any data type?
 

Ramiro

Now with an army of Mecha-Ralphs!
Veteran
Joined
Aug 5, 2015
Messages
858
Reaction score
364
First Language
Spanish
It's rather easy.

== is called unrestricted comparison, it checks equality without considering the types, for example, any falsy value will be considered equal with ==,

So then,

null == false, 0 == false, '23' == 23 (yes even strings are considered equal to numbers in js), and so on...

=== is == with a type check, so it must be truly equal to be considered ===, so '23' === 23 is false, and null === 0 is false too.
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
Ohhh now I understand. I keep using ==, and perhaps the flaw when I do something. That's weird then, I thought the == in Java would be the same in JS, as its a scripting language based from it.

Thanks a lot for solving, a new knowledge learned.
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,545
Reaction score
3,715
First Language
Java's Crypt
Primarily Uses
RMMZ
You should always use === when comparing against 0, null, undefined, false and true. Those are values that can easily cause problems when you compare them using ==.
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
31,352
Reaction score
7,666
First Language
German
Primarily Uses
RMMV
in Java would be the same in JS, as its a scripting language based from it.
That is where you went wrong - javascript has absolutely nothing to do with java and was never based on it.
Or as someone once said: java is to javascript what ham is to hamster.
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,545
Reaction score
3,715
First Language
Java's Crypt
Primarily Uses
RMMZ
That is where you went wrong - javascript has absolutely nothing to do with java and was never based on it.

Or as someone once said: java is to javascript what ham is to hamster.
That's not a bad assumption to make, though. It would be true for basically any language other than Javascript and PHP.
 
Last edited by a moderator:

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I had the assumption because I thought the reason why it is 'Java'Script is because the syntax and logic flow is the same as of Java. If it was not based on Java, I wonder why, I think the developer could just have used another name for it.
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,545
Reaction score
3,715
First Language
Java's Crypt
Primarily Uses
RMMZ
I had the assumption because I thought the reason why it is 'Java'Script is because the syntax and logic flow is the same as of Java. If it was not based on Java, I wonder why, I think the developer could just have used another name for it.
They changed the name to Javascript to try and get popularity over the Java name.
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
Oh wow, I see. Nice tactic XD
 

Ramiro

Now with an army of Mecha-Ralphs!
Veteran
Joined
Aug 5, 2015
Messages
858
Reaction score
364
First Language
Spanish
Well with ES6 it may have be more based on java than before, but javascript isn't based in syntax by java, it's closer to c and older languages from the '80s with the whole idea of not having data types.

Also they had a prototype based approach and not a class based approach like java.

Yes they named it JS because java was popular and they wanted to leech from that popularity...

Still JS and java are really different on many terms, but any javascript programmer actually knows than having == is practically useless, but they can't remove that for compatibility reasons...
 

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
The "==" operator in Java only works for simple data types (int, long, boolean, ...) or autopackaged classes (Integer, Long, Boolean, ...). For everything else, it will compare the actual memory storage of the objects you are comparing.
 
Last edited by a moderator:

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
tripple equals checks type as well as value
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:
To whom ever person or persons who re-did the DS/DS+ asset packs for MV (as in, they are all 48x48, and not just x2 the pixel scale) .... THANK-YOU!!!!!!!!! XwwwwX

Forum statistics

Threads
105,845
Messages
1,016,961
Members
137,561
Latest member
JaCrispy85
Top