what is the difference between != and !== in Javascript?
13,853
Solution 1
!==
and ===
are strict comparison, and ==
/ !=
are loose comparison. It's best to use strict comparison.
Solution 2
true == 1
gives you true
true === 1
gives you false
Reason is that == compares only the value (so that 1, '1' is considered as true)
=== compares the value and the type.
Same thing in PHP.
Solution 3
== compares the value of object while === compares the object value and type.
Solution 4
yes it is.
<script>
var str = '1234';
var int = parseInt('1234');
if (int !== str)
{
alert('returns true and alerts');
}
if (int === str)
{
alert('returns false');
}
</script>
Author by
ilyo
Updated on June 05, 2022Comments
-
ilyo almost 2 years
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?Are
!=
and!==
are respectively the same as==
and===
? -
Pwnna almost 13 yearsWell, i think loose comparisons are useful.
-
pimvdb almost 13 yearsThat would mean
[1] == [1]
and that you cannot do1 === 1
as numbers have no reference. -
wanovak almost 13 yearsI didn't say they weren't, but if you're used to using strict comparison and only use loose when it's useful to, you're going to run into a lot less bugs (generally.)
-
hungryMind almost 13 yearsby reference, i meant value & type. See edit
-
JensG about 5 yearsThat's not a sufficient answer. Iit is better, because ... ? You are running into a lot less bugs, because ...?