Difference between == and === in JavaScript [duplicate] - javascript

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 3 years ago.
What is the difference between == and === in JavaScript? I have also seen != and !== operators. Are there more such operators?

Take a look here: http://longgoldenears.blogspot.com/2007/09/triple-equals-in-javascript.html
The 3 equal signs mean "equality without type coercion". Using the triple equals, the values must be equal in type as well.
0 == false // true
0 === false // false, because they are of a different type
1 == "1" // true, automatic type conversion for value only
1 === "1" // false, because they are of a different type
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false

=== and !== are strict comparison operators:
JavaScript has both strict and
type-converting equality comparison.
For strict equality the objects being
compared must have the same type and:
Two strings are strictly equal when they have the same sequence of
characters, same length, and same
characters in corresponding positions.
Two numbers are strictly equal when they are numerically equal (have
the same number value). NaN is not
equal to anything, including NaN.
Positive and negative zeros are equal
to one another.
Two Boolean operands are strictly equal if both are true or
both are false.
Two objects are strictly equal if they refer to the same Object.
Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]
Comparison Operators - MDC

Related

How true+false===1 evaluates to true [duplicate]

This question already has answers here:
Is true == 1 and false == 0 in JavaScript?
(10 answers)
What exactly is Type Coercion in Javascript?
(9 answers)
Closed 3 years ago.
I am trying to compare true+false to 1 i.e true+false===1
In the past, I tried the following:
true===1 //false
false===0 //false
true-true===0 //true
I expect the output to be false but the actual output is true.
true and false are implicitly type casted to number when used in mathematical operation
Note:- here + is type casting it to number
console.log(+true)
console.log(+false)
console.log(true+false)
so
true + false === 1
is actually
1 + 0 === 1
When you add up two Booleans, since the Booleans do not support arithmetic operation, but logic operation only, the JS environment just converts them to Number. So 1+0===1. But if you do true ===1, this is type sensitive, it will result false. If you do true == 1, it’ll be true. Cuz this is not type sensitive, type transformation will be done by JS environment.

jsHint error saying I should use "===" [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 8 years ago.
My code is giving me an error with jsHint. I am trying to do this:
if (data.result == 't' || task == 'show') {
But it tells me I should replace "==" with "===" can someone tell me why it gives this message?
=== is the strict equality operator.
== is the normal equality operator, == converts its operands to the same type if they are not already of the same type.
So there is a danger that something like "" == 0 will give you true although they are of different types.
Since there is implicit conversion involved which you might not no about because it is happening automatically, there is some danger and potential for errors or bugs that are hard to trace.
=== won't convert its operands, it will just compare them.
Silent type conversion can be a source of bugs. If you avoid converting between data types at comparison time, then you avoid many of those bugs.
=== means equality without type coercion. === operator will not do the conversion, it will only compare the operands.
The example given by sdfx here is very helpful in understanding this:
0==false // true
0===false // false, because they are of a different type
1=="1" // true, auto type coercion
1==="1" // false, because they are of a different type
From MDN:
JavaScript has both strict and Type–converting (abstract) comparisons.
A strict comparison (e.g., ===) is only true if the operands are the
same Type. The more commonly used abstract comparison (e.g., ==)
converts the operands to the same Type before making the comparison.
For relational abstract comparisons (e.g., <=), the operands are first
converted to primitives, then the same Type, before comparison.
Features of comparisons:
Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding
positions.
Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything,
including NaN. Positive and negative zeros are equal to one
another.
Two Boolean operands are strictly equal if both are true or both are false.
Two distinct objects are never equal for either strictly or abstract comparisons.
An expression comparing Objects is only true if the operands reference the same Object.
Null and Undefined Types are == (but not ===).

Javascript: the confuse about comparison "2 == true"

Here is a javascript comparison:
2 == true //false
it's said, the reason why return false, is because the comparison convert the true to Number datatype, and result is 1:
console.info(Number(true)) // 1
My confuse is, why the comparison don't convert the number 2 to Boolean datatype
console.info(Boolean(2)) // true
and the 2 == true result could be true ?
I find the doc here:
Comparison Operators, which said:
If the two operands are not of the same type, JavaScript converts the
operands then applies strict comparison. If either operand is a number
or a boolean, the operands are converted to numbers if possible; else
if either operand is a string, the other operand is converted to a
string if possible. If both operands are objects, then JavaScript
compares internal references which are equal when operands refer to
the same object in memory.
== does implicit conversion to compare. In this case 2 is number and true is boolean. The conversion rule is "while comparing a number to boolean, boolean will be converted to number" hence
true is converted to 1
and 2 == 1 will be false.
//similarly,
2 == false; //false
As false will be converted to 0 and 2 cannot be equal to 0 either.
However, 1 == true. for the same reason as true would be converted to 1 and 1==1

What's wrong with if (json.RowKey != json.NewRowKey)

I am using lint and for the following:
if (json.RowKey != json.NewRowKey)
It gives me a message:
Expected '!==' and instead saw '!='.
Can someone explain what this means?
== will attempt to convert the types of the two operands to be the same before comparing them. Thus "2" == 2 is true.
=== will not attempt to convert the types of the two operands so if they are not the same type, they will never be ===. Thus "2" === 2 is false.
It is better to use === and !== as your default choice and only use == or != when you explicitly want to allow type conversion. If you are not expecting the two operands to be different types, then === and !== is more likely to do what you are expecting.
This avoids coding mistakes when things are not really equal and your code doesn't intend for them to be equal.
Some odd things that happen with == (and do not happen with ===) that you may not expect and can lead to trouble. All of these evaluate to true:
null == undefined
false == '0'
0 == '0'
0 == ''
There's more detail written here: Which equals operator (== vs ===) should be used in JavaScript comparisons?
!== is not equal (neither value or type)
Eg:
var x = 5; x !== "5" returns true
var x = 5; x !== 5 returns false
It's a warning that tells you that you should consider using the !== operator instead of !=.
On the surface they both do the same thing - compare whether the two items are not equal. However, !== makes sure that both items are of the same type first (that is, it won't try to convert, say, a string to a number if one item being compared is a string and the other is a number), so it's generally safer to use and helps you avoid comparison errors.
For more details, see here: Which equals operator (== vs ===) should be used in JavaScript comparisons?
Yes,
if("1" == 1) //Returns true because it will convert the second 1 into a string to match
but
"1" !== 1 //Returns false because the first 1 is a string and the second 1 is an integer.
We are talking about variable types; with two equal signs, javascript (and php) will typecast the variable for you (convert the integer 1 into a string 1 to make the match and be true), but with the second statement, it will not.
1 === 1 //True
1 === "1" //False
1 == "1" //True
1 == 1 //True
Hope my answer makes sense to you.

Why javascript treats 0 equal to empty string? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Implied string comparison, 0='', but 1='1'
Executing the following case in javascript, I get 0 equal to '' (an empty string)
var a = 0;
var b = '';//empty string
if(a==b){
console.log('equal');//this is printed in console
}else{
console.log('not equal');
}
Could anyone guide that what is the reason behind this?
Quoting the doc (MDN):
Equal (==)
If the two operands are not of the same type, JavaScript converts the
operands then applies strict comparison. If either operand is a number
or a boolean, the operands are converted to numbers if possible; else
if either operand is a string, the other operand is converted to a
string if possible.
As a operand type here is Number, b gets converted to Number as well. And Number('') evaluates to 0.
This can be quite surprising sometimes. Consider this, for example:
console.log(0 == '0'); // true
console.log(0 == ''); // true
console.log('' == '0'); // O'RLY?
... or this:
console.log(false == undefined); // false
console.log(false == null); // false
console.log(null == undefined); // fal.... NO WAIT!
...and that's exactly why it's almost always recommended to use === (strict equality) operator instead.
0, "" (Empty String), false all technically have the same value, when typecasted. If you need to strictly treat them, you can use ===.
It is the same with similar programming languages, like PHP.
var a = 0;
var b = ''; //empty string
if(a == b){
console.log('equal'); //this is printed in console
}else{
console.log('not equal');
}
To make a strict comparison:
if(a === b){
console.log('equal');
}else{
console.log('not equal'); //this is printed in console
}
== operator in javascript don't compare types so 0=='' === true (because as a number string '' evaluates to 0) or 0==false === true (because bool false evaluates to 0) to compare types to you can use === operator.
Here you'll find useful information about comparison.
Javascript automatically converts variables of different types for comparison. That's a common feature in non-strictly typed languages.
If you need to compare variables and check the type, use the === operator.
In most programming language(including javascript) ""(string), 0(integer), \x0(null) losely mean the same thing: "empty". What's happening is your javascript engine finds "" == 0 false, due to the == it converts 0 to an integer. Again this is false, so it converts 0 to null which is false, so then it converts 0 to an empty string. (Not sure if this is the correct order of conversion). To make the condition "exact" match(no conversion) use === inplace of ==
== does typecasting. Always use ===.
In your example, the empty string of b is being converted to 0. So both a and b are the same.
Because empty string represented as number is zero. If you compare apples and oranges you should think of how your particular orange would look if it were an apple.
Because of coercion. It is usually a better idea to use === for comparisons in JavaScript.
Because == checks value equality so false, 0 and empty strings are equals. Use identity check ===.

Categories

Resources