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>

http://sandbox.phpcode.eu/g/c801e.php

Share:
13,853
ilyo
Author by

ilyo

Updated on June 05, 2022

Comments

  • ilyo
    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
    Pwnna almost 13 years
    Well, i think loose comparisons are useful.
  • pimvdb
    pimvdb almost 13 years
    That would mean [1] == [1] and that you cannot do 1 === 1 as numbers have no reference.
  • wanovak
    wanovak almost 13 years
    I 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
    hungryMind almost 13 years
    by reference, i meant value & type. See edit
  • JensG
    JensG about 5 years
    That's not a sufficient answer. Iit is better, because ... ? You are running into a lot less bugs, because ...?