Why 3 equal symbols in boolean comparisons? [duplicate] - javascript

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.

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.

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

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

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

Javascript: == or ===? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Hi,
This is my tiny code:
var domains_before_update = storage.getItem('domain_list_original');
if(domains_before_update==null || domains_before_update=="" )
{
gBrowser.selectedTab = gBrowser.addTab("chrome://filter/content/block_and_redirect_list.html");
}
Is that correct or should I be using === instead of == ?
Thanks!
=== checks the strict equals (without coercion) that you're used to , where == checks the value [after built-in coercion] equality
but as the other answer(s) noted, strict equality does not work when checking for null, use !variable
same as this post: Difference between == and === in JavaScript
edit: clarified some of the wording thanks to the helpful comments!
In this case, it doesn't matter - and in all cases where it doesn't matter, you should use strict equality or identity, e.g. ===.
Neither.
Use:
if(!domains_before_update)
{
}
For comparisons with null, === is required.

Difference between == and === in JS [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Difference between == and === in JavaScript
Javascript === vs == : Does it matter which “equal” operator I use?
What's the difference between == and ===? Also between !== and !==?
There are lots of answers to this question on Stackoverflow already.
Short:
== only compares values
=== compares values + type
var check1 = '10',
check2 = 10;
check1 == check2 // true
check1 === check2 // false
"==" means equals, whereas "===" means identically equal.
In short, "==" will try and coerce/convert the types of values when doing a comparison, so "2"==2, whereas "===" will not.

Categories