what does "===" imply in Javascript/Jquery? [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
The 3 different equals
I'm trying to understand what is happening here:
data.toPage = $('div#someID');
if ( typeof data.toPage === "string" ) {
// sth
console.log("hello");
}
So I'm checking for a string am I not? I'm curious because my console "helloes".
Thanks for some input!

==
This is the equal operator and returns a boolean true if both the operands are equal. JavaScript will attempt to convert different data types to the same type in order to make the comparison. Assuming 'a' to be 2 and 'b' to be 4, the following examples will return a value of true:
a == 2
a == "2"
2 == '2'
===
This is the strict equal operator and only returns a Boolean true if both the operands are equal and of the same type. These next examples return true:
a === 2
b === 4

The triple equals sign === compares both value and type, whereas the double == only compares value
for example "1" and 1 have the same value (so to speak) but are of different types. Therefore the following will occur:
"1" == 1 //true
"1" === 1 //false
This is a great read for some useful javascript knowledge, which includes the triple equals amongst other good-to-know stuff

The === comparison operator means that the two values won't have their types modified before the comparison is made, so they need to be of the same type as well as representing the same value for it to return true.
'1' == 1 // true
'1' === 1 // false

Related

When should we use '=== ' operator in javascript? [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 6 years ago.
When and why should we use '===' in javascript or jquery.
Is it recommended to test string using === and if yes why.
I have a code where i am checking a condition on the string like.
if(a == "some-thing") is this right or should i use '==='
=== means exactly equal - compare value and type.
'1' == 1 // true
'1' === 1 // false
=== is used for strictly type check, when you want to check value as well as its type.
For Example
var x = 5;
var y = "5";
var z=5;
alert(x==y);//string and int value same though type different - TRUE
alert(x===y);//value same but type different - FALSE
alert(x===z);//value same and type same- TRUE
More Information
with === you will compare the value AND the type of data. With == you only compare the value. So 1000 == "1000" it's true, while 1000 === "1000" is false.
In a simple answer:
Triple equals does a check for type equality. So it'll check the types as well as the contents.
e.g.
var a = '1' === 1;
Would be false.
A double equals would only check the contents, so the code above would return true.
As a good practice, as much as possible.
As a minimum : if you want to check specifically for 0/false/null value because when you use == they're all the same :
0 == false // true
0 === false // false
And f you test the existence of a value but that value can be false or 0 this won't work.
if(value){} // false is value === false or value === 0
There is also the type equality but i don't really think that much relevant unless you depend on some 3rd-party where you need this.
A === "some string" means equal value and moreover equal type.
It's a double test: In your case you obtain true not simply if the values are equal but moreover if the variable A is a string.
You should almost always use the === operator.
An example why:
1 == '1' //is true
1 === '1' //is false
And you want to achieve the second result, because === checks if also the type is the same.

Comparing values in javascript [duplicate]

This question already has answers here:
Difference between == and === in JavaScript [duplicate]
(2 answers)
Closed 6 years ago.
I am using java script to compare values
if (cellValue == "true" || cellValue == true)
Instead of doing this can i use the ===operator in java script ?
The difference between == and === is that the first one (double equals) does type coercion, which can lead to goofy results:
0 == false
true
0 === false
false
It is usually recommended to use === as that will provide more predictable results and evaluate the true types of the values you're comparing.

What's the difference between a == null and a === null? [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 9 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Can someone help me by explaining the difference. From what I understand the === does an exact match but what does this mean when comparing with null ?
What does this mean when comparing with null?
It means exactly what you already said: It checks whether the value is exactly null.
a === null is true if the value of a is null.
See The Strict Equality Comparison Algorithm in the specification:
1. If Type(x) is different from Type(y), return false.
2. If Type(x) is Undefined, return true.
3. If Type(x) is Null, return true.
So, only if Type(a) is Null, the comparison returns true.
Important: Don't confuse the internal Type function with the typeof operator. typeof null would actually return the string "object", which is more confusing than helping.
a == null is true if the value of a is null or undefined.
See The Abstract Equality Comparison Algorithm in the specification:
2. If x is null and y is undefined, return true.
3. If x is undefined and y is null, return true.
=== means it checks both the value and the type of the variables. For example pulled from the w3c page, given x = 5, x is an int, so x==="5" is false because it is comparing and int to string and x ===5 is true because it is both an int and the right value.
=== is strict operator it not only compare value, but also type of variables so
string===string
int===int
== only compare values.
Using the triple equals, the values must be equal in type as well.But not in ==.
i.e
1==true // this return true
1===true // but return false
a==null // will true if a is null or undefined
1==true will be true
but 1===true will be false
eg. === compares on data type level while using == JavaScript will typecast by it self

Difference between !== and != [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
Javascript operator !==
What's the difference between != and !==?
Can you give me an example where using != gives another result than using !==?
alert(1 != true);
alert(1 !== true);
The first one is false, the second true.
!= accept 1 as equals of true, null as equals of false and some others (because the values are automatically casted when being compared).
!== accept only "real" equalities (i.e. compares both the value and the type).
Example

Why 3 equal symbols in boolean comparisons? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Why do I see lots of javascript code lately with expressions that look like this:
if(val === "something")
Why "===" instead of just "=="? What's the difference? When should I use one or the other?
The === does not allow type coercion, so something like this would return false:
if (2 === '2') // false
The "normal" javascript == operator does allow type coercion, and so this would return true:
if (2 == '2') // true
var a = 3;
var b = "3";
if (a == b) {
// this is true.
}
if (a === b) {
// this is false.
}
=== is typically referred to as the identity operator. Values being compared must be of the same type and value to be considered equal. == is typically referred to as the equality operator and performs type coercion to check equality.
An example
1 == '1' // returns true even though one is a number, the other a string
1 === '1' // returns false. different datatypes
Doug Crockford touches briefly on this in JavaScript the Good Parts google tech talk video. Worth spending an hour to watch.
Checks that the type as well as the values match. This is important since (0 == false) is true but (0 === false) is not true.

Categories