Difference between "==" and "===" in java script [duplicate] - javascript

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Hi
I am not clear why there are two == and === comparator operators in java script.

The equality operator == coerces, or converts the data type temporarily to see if it's equal to the other operand whereas the identity operator === doesn't need to do any converting whatsoever since it directly compares them without conversion meaning it's stricter and faster.
2=='2'
true
2==='2'
false

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.

what does it mean to have multiple equal signs on the same line? [duplicate]

This question already has answers here:
What is exactly the meaning of "===" in javascript? [duplicate]
(5 answers)
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
JavaScript comparison operators: Identity vs. Equality
(4 answers)
Closed 4 years ago.
what does this line of code mean below? is it some some sort of ternary operation?
this.object01 = object02.attribute01 === someString;
this is using aurelia javascript
Break it down. There's two operations going on here: an assignment statement (one equals sign) and an equality check (three equals sign) which returns a boolean.
this.object01 is now a boolean containing true or false depending if object02.attribute01 === someString.

Javascript && operator behavior in chrome's V8 [duplicate]

This question already has answers here:
Javascript AND operator within assignment
(7 answers)
Closed 6 years ago.
As I'm writing a javascript interpreter I need to implement some basic functions. I was playing around a bit in my Chrome console I discovered this strange behavior in js. How can I interpret what is js doing in this particular case?
Thanks
Javascript's && operator returns the left part if it can be converted to false, otherwise returns the right part.
So in both cases you are getting the expression on the right (since neither 012 nor true can be converted to false). For the first case, true, and for the second case, 012.
Since 012 is octal, you are getting 10 output, which is the base-10 representation of octal 12
I can't find a better link for a source, so I'll quote this one (it's not V8 specific, but should do): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
Logical AND (&&)
expr1 && expr2
Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands can be converted to true; otherwise, returns false.
(bold is mine)
The way I interpret the && and || operators is like this:
a && b = a ? b : a;
a || b = a ? a : b;

If 1=='1' //true and 1==true //true then why 1=='true' //false [duplicate]

This question already has answers here:
Equality of truthy and falsy values (JavaScript)
(3 answers)
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 7 years ago.
Why is 1=='true' false?
If 1=='1' is true and 1==true is true.
If JavaScript compares only values not the types in the == scenario.
It's because of type coercion.
In effect, this is what JavaScript is trying to do on your behalf when using the == operator.
1 == Number('true'); // 1 == NaN
1 == Number('1'); // 1 == 1
1 == Number(true); // 1 == 1
When two different types are compared using ==, JavaScript attempts to coerce them to the same type for comparission.
You can read more about the algorithm here: http://webreflection.blogspot.com/2010/10/javascript-coercion-demystified.html

Do angular conditions work the same way as pure javascript? [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 7 years ago.
I wonder if there is the same difference between identity === and equality == operators in angular directives like in pure javascript?
For example is
ng-if="value === 'foo'
better than
ng-if="value == 'foo'
What I checked is that
ng-if="true == 1
passes but
ng-if="true === 1
doesn't, so it looks like it works the same way like pure js. On the other hand in angular source they use just equality check, even tho in js identity is preferred.
https://github.com/angular/angular.js/search?utf8=%E2%9C%93&q=ng-if
Which operator should we use in angular directives?
EDIT:
To clarify - I'm not asking about javascript conditions, this has been already answered on stack, my question is - is there any difference in conditions between pure js and angular directive conditions?
Main difference between "==" and "===" operator is that former compares variable by making type correction e.g. if you compare a number with a string with numeric literal, == allows that, but === doesn't allow that, because it not only checks the value but also type of two variable, if two variables are not of same type "===" return false, while "==" return true.
Since JavaScript support both strict equality and type-converting equality, it's important to know which operator is used for which operation. As I said that, === takes type of variable in consideration, while == make type correction based upon values of variables, following are couple of more differences between "==" and "===" operator in JavaScript programming language :
1) When we compare two variables of different type e.g. a boolean with a string or a number with String using == operator, it automatically converts one type into another and return value based upon content equality, while === operator is strict equality operator in Java, and only return true if both variable of same type and also contains same value. This will be much clear with following example of == and === operator in JavaScript :
0==false // true, because false is equivalent of 0
0===false // false, because both operands are of different type
2=="2" // true, auto type coercion, string converted into number
2==="2" // false, since both operands are not of same type
2) "==" operator is known as type coercion operator and anytime if both values are same and compared using ==operator, type coercion happens. On the other hand === is known as strictly equality operator. It's much similar Java's equality operator (==), which gives compilation error if you compare two variables, whose types are not compatible to each other. In fact, you should always use "===" operator for comparing variables or just for any comparison.

Categories

Resources