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?
== vs ===
Learning JavascriptIt'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.
== 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.
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.
Thanks a lot for solving, a new knowledge learned.
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 ==.
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.
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.
Oh wow, I see. Nice tactic XD
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...
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...
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.
tripple equals checks type as well as value
Original thread: https://forums.rpgmakerweb.com/threads/vs.49946/