I'm raising the question as seen from this Code Academy lesson. Thanks in advance for your explanations.
Hope this is not repeated too often.
-------- Explanation from the lesson is added below ----------------
You may have noticed we've used two types of equals thus far, the single equals (=) and the double or triple equals (==, ===). The single equals (=) assigns a variable, while the double and triple equals (==, ===) are used to check equivalence between values. Since == can have some odd behavior in JavaScript, it is almost always better to use ===.
Run this exercise to see what it does. It first sets the variable word to the string "this". Change the code so that word === "that" evaluates to true, and the console.log() mand is run.
I'm raising the question as seen from this Code Academy lesson. Thanks in advance for your explanations.
Hope this is not repeated too often.
-------- Explanation from the lesson is added below ----------------
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 5, 2012 at 6:42 stanigatorstanigator 10.9k35 gold badges97 silver badges131 bronze badges 7You may have noticed we've used two types of equals thus far, the single equals (=) and the double or triple equals (==, ===). The single equals (=) assigns a variable, while the double and triple equals (==, ===) are used to check equivalence between values. Since == can have some odd behavior in JavaScript, it is almost always better to use ===.
Run this exercise to see what it does. It first sets the variable word to the string "this". Change the code so that word === "that" evaluates to true, and the console.log() mand is run.
- 2 Would you care to post the question here? – Sergio Tulentsev Commented Jan 5, 2012 at 6:43
- @SergioTulentsev: I thought the question was evident in the title. – stanigator Commented Jan 5, 2012 at 6:44
- Hitting the run button the link you provided just generates an "Oops, try again" message. Pretty sure a tiny bit of research would help you here. – Matt Lacey Commented Jan 5, 2012 at 6:45
- 1 What odd behavior are you asking about? The question here is obviously not clear to many of us and all you say is "you thought it was evident in the title" and don't offer any clarification. Either you want us to help you or not. If you want help, please make the question a lot more obvious! – jfriend00 Commented Jan 5, 2012 at 6:51
-
7
I think you mean
==
can have odd behavior, not===
. – Patrick Fisher Commented Jan 5, 2012 at 6:54
5 Answers
Reset to default 6The triple equals ===
returns true if both operands are the same type and have the same value.
The double equals ==
returns true if both operands can be coerced to the same type (following a specific set of rules) and have the same value after being coerced.
So, some examples:
1 === 1 // true
1 === "1" // false
1 == 1 // true
1 == "1" // true
"5" == 2 + 3 // true
0 == "" // true
0 === "" // false
null == undefined // true
null === undefined // false
The only cases I know of ===
having "odd behavior" is when the native-value (e.g. string
) is pared against an object-value (e.g. String
) that "is convertible to" the native value:
"a" === "a" // true
"a" == new String("a") // true
"a" === new String("a") // false- uh, what?
new String("a") == new String("a") // false- thought it was gonna be true? :)
new String("a") === new String("a") // false
var a = new String("a"); a === a // true
The same holds for the other wrapper types, such as Number
. This is because, when using ===
, JavaScript uses a different algorithm that does not perform coercions.
There have been some interesting browser bugs in the past where certain methods, say in String.prototype
, would return a String
and not a string
causing code to break in subtle ways.
As others have pointed out, the behavior of ==
is often more ... interesting. I almost exclusively find that ==
Does The Right Thing™ , but understanding the details is important to mastering JavaScript.
Happy coding.
If two variables contain the same value but are not of the same type, == "forces" the equality and return true. However, === will return false, as it requires that two variables have the same value and be of the same tye in order to be equal. Otherwise, they behave similarily.
==
checks if the values are equivalent. ===
checks if the values are identical.
To illustrate this example, take a look at this:
"1" == 1 // this is TRUE, because the values are equivalent.
"1" === 1 // this is FALSE, because the values are of different types.
I have yet to encounter a case where I need ===
in JavaScript, because it is a relatively recent addition to the language, so for example indexOf
returns a negative number to mean "no match" (whereas PHP returns false
so you have to use ===
to differentiate between "no match" and "match at start of string").
I would highly remend you watch this presentation by Bucky Schwarz:
- http://textiles.online.ncsu.edu/online/SilverlightPlayer/Default.aspx?peid=b2e41bcc02cc4a169a0baaa4ecb960ac1d
Within the first 5 minutes he talks about this topic.
Taken from one of his slides:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == 0 // true
false == undefined // false
false == null // false
null == undefined // true