Difference between !== and != [duplicate] - javascript

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

Related

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.

Object is not null and not undefined. What is better - double inversion `!!` or strict equillity `!==` in JavaScript [duplicate]

This question already has answers here:
JavaScript checking for null vs. undefined and difference between == and ===
(8 answers)
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 7 years ago.
Very often we check our objects if they are not null and not undefined. I always use condition if (obj !== null && obj !== undefined). Few days ago my colleague shown me the syntax of double inversion !! and now I can use condition if (!!obj). This syntax is less.
I'm not a person who are only learning js, but I have a little interest.
So is there any difference between these two ways of object validation? Performance difference? Semantic difference? Any difference?
There isn’t any particularly good reason to ever use if (!!obj), as it’s equivalent to if (obj). !!obj and obj !== null && obj !== undefined do different things, though, and you should use whichever’s most appropriate.
obj !== null && obj !== undefined (surprise, surprise) results in false for null and undefined.
!!obj results in false for anything falsy, including null, undefined, '', 0, NaN, and false.
!!foo is used to force coerce a value into its Boolean value. This is, in my experience, often used in APIs that want to return a Boolean value for a value that contains sensitive data. For example, to return whether or not a password is entered you might say return !!password rather than return password.
Continuing on that, if (!!obj) is not the same as if (obj !== null && obj !== undefined) for many values you can think of! Such as false.

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

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.

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