This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 8 years ago.
[4] === 4 // is: false
[4] == 4 // is: true
'0' == 0 // is: true
'0' === 0 // is: false
Can anyone give the exact reason for this?Also what exactly does strict equality operator do or need for comparision?I learned that type and value should be same for strict(===) operator.Is this what strict equality operator checks .If yes,than how equl to operator works?
== Compare values
=== Compare values and type
For example
[4] //turns into "4" when comparing
"4" == 4 //They are the same
"4" === 4 //The values are the same, but not the type
Reference: http://es5.github.io/#x11.9.4
http://i.stack.imgur.com/q13LO.png
The === operator also compares the type of the object.
So, in [4] === 4
[4] is an array, but 4 is a number, so that evaluates to false.
And in '0' === 0
'0' is a string, but 0 is a number, so that evaluates to false.
The === operator compares both type and value, making it a stricter check.
The == operator performs a less strict value based checked. It will in some cases consider values of different types "equal" Such examples are 0 vs '' or 0 vs "0"
The == operator will see 0 and '' as equal whereas the === operator will not treat them as equal since they are of different types.
Related
This question already has answers here:
Why is "0" == [] false? [duplicate]
(4 answers)
Closed 2 years ago.
If 0 (as a digit) equals to 0 as a string, and 0 equals to an empty list, why 0 as a string is not equal to an empty list (from the moment that 0 as a digit equals to 0 as a string)?
> 0 == '0'
true
> 0 == []
true
> '0' == []
false
To understand the results of your code, you need to understand how == equality operator works in the following cases:
When both operands are of same type, return the result of strict equality comparison ===
If one operand is a number and other is a string, convert the string operand to a number
If any or both operands are non-primitive, convert non-primitive operand to a primitive value
Keeping the above mentioned points in mind, lets see how each comparison is processed:
0 == '0'
This evaluates to true because '0' is converted to a number and then strict equality comparison is performed since both operands have the same type after the conversion of '0' to a number.
0 == []
returns true because when any operand of the == operator is non-primitive, it is converted in to a primitive value. Empty array when converted into a primitive value, results in an empty string. So after this conversion, you have 0 == ''. Now one operand is a number and other is a string. Empty string is then converted to a number which results in 0 leading to the comparison of 0 == 0 which evaluated to true.
'0' == []
this returns false because empty array is converted to a primitive value which is an empty string. When [] is converted to an empty string, types of both operands ('0' == '') are same, so strict equality comparison is done which returns false.
For details, see Ecmascript Spec: 7.2.15: Abstract Equality Comparison
In JavaScript, double equals does not mean strictly equal to, whereas tripple equals === does. In the former cases a conversion is made as a result of the types involved, whereas no such available conversion (which results in semi-equality) exists in the latter case.
JavaScript is designed that way.
In 0 == '0'
== converts '0' to 0 because the right operand is a number
So what happens is it checks 0 == 0 which is true.
Tips use === called strick equality comparison to check, which will treat 0 as a number and '0' as a string.
0 == []
In Javascript empty array is treated as a falsy, while 0 is always false and 1 truth.
So == compares false == false which is true. Once again use strict equity sign ===
'0' == []
Here since [] is not a number Javascript won't care to convert '0' to 0 which means the comparison is comparing string and array which will won't be true.
. You have 3 cases.
when you write '0' == 0 , the Value of '0' in utf-8 is 0 hence
false ==false is true
String is basically a list in python when you write '0' you are basically making a list with an element '
'0' ie '0' ==['0'] and not [ ] hence true==false is false
while when you write 0==[ ] every value with 0 is false and the empty list returns the Boolean value of number of elements in the list which is False for 0 hence false ==false is true
This question already has answers here:
Why `null >= 0 && null <= 0` but not `null == 0`?
(6 answers)
JavaScript comparison operators: Identity vs. Equality
(4 answers)
Closed 6 years ago.
I want to ask about a weird javascript thing. All of these conditions, in my opinion, contradict each other and return false:
0 > null
0 < null
0 == null
0 === null
Why does using >= and <= operators return true? >= means gt and <= means lt. They couldn’t be equals. Moreover, “null” has a null value, 0 has a null value and, for the logic 0 > null should return true. Could someone explain me this fact?
When you use > and <, null is converted to the number 0. 0 > 0 and 0 < 0 are both false (that's basic math). When you use == and ===, null is not converted. 0 is not equal to null and hence both are false as well.
More generally speaking: Operators are defined for specific data types and if you pass a value of a different data type that value will be converted to the expected data type first. > and < are defined for strings and numbers but not for null. Hence null is (eventually) converted to a number.
== are a little different ===. While == usually performs type conversion, it doesn't do that if you compare against null. That's simply how the algorithm works.
This question already has answers here:
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 6 years ago.
I am not getting what or operator does with inters. I have following code
-1||4 // output -1
4||-1 //output 4
Does it converts integers in bytes and performs or operation.
It first checks wheter the number is truthy or falsey and returns the first truthy one. All numbers are truthy except for 0.
0 || 4; // 4
2 || 3; // 2 (picks the first one, because both true)
-3 || 0; // -3
0 || -2; // -2
Does it converts integers in bytes and performs or operation?
No. The || operator is logical and, not bitwise and.
This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 7 years ago.
I am trying to understand equality in JavaScript. Here is the code.
var x = prompt("What is 10 + 10");
if (x === 10) {
document.write("Correct")
}
else {
document.write("Incorrect")
}
Why wouldn't I make the equals sign like "===". So if "10" is equal("===") to "x"(user answer) then it should be correct right?
I searched on both Stack Overflow and W3Schools, but couldn't find what I was looking for. I guess I'm just nor getting this "true or false" thing. I mean this seems like a very simple equation. Help would be great thanks guys!
=== is strict type equality which compares by both value and type
== is non-strict type equality, which compares only by value.
In other words, == performs type conversion and then compares values for equality. Here are some examples
"3" == 3
=> true
Explanation: The string 3 is converted to the number 3, which is equal to 3.
"3" === 3
=> false
Explanation: The string is not converted to a number. Thus the string 3 does not equal the number 3.
In your example, incorrect would be written to the document. That is because the result of prompt returns a string, and you are performing strict equality with a number.
In your case, the interpreter sees it like this
if ("10" === 10) {
// does the string "10" equal the number 10? If so
document.write("Correct")
}
else {
// Hey, wait a minute. It doesn't equal the number. I should write "Incorrect" instead.
document.write("Incorrect")
}
In Javascript,
== means: is equivalent to
=== means: is identical to
When the value of x is "10", x is equivalent to 10.
But it isn't identical to 10.
This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 9 years ago.
I found == is little confusing for newbies, So I want someone to explain how it works.
For example -
new String("a") == "a" and "a" == new String("a") are both true.
new String("a") == new String("a") is false.
Why?
== is called comparison/equality operator, it compares 2 values, but not their data types so for example
1 == '1' will return true, for stricter comparison, use === which will compare the data types too so 1 === '1' will return false
== is a comparison operator that means "equal to" but does not take variable typing into account.
=== is a stricter comparison operator that means "equal to and same type".
So if you have a string called numberStr with a value of 2 and an integer called numberInt with a value of 2, they will evaluate as follows:
numberStr == numberInt // evaluates to true
numberStr === numberInt // evaluates to false because types are different