By Shiva


2009-02-07 11:53:04 8 Comments

What is the difference between == and === in JavaScript? I have also seen != and !== operators. Are there more such operators?

2 comments

@Jack Sleight 2009-02-07 11:55:34

=== and !== are strict comparison operators:

JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:

  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
  • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
  • Two Boolean operands are strictly equal if both are true or both are false.
  • Two objects are strictly equal if they refer to the same Object.
  • Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]

Comparison Operators - MDC

@Filip Vondrášek 2013-01-25 23:42:22

So, if I do for example: if (input == null) ..., will it also make the condition true when input is undefined?

@Matt Browne 2013-02-10 01:44:07

The above makes it sound as though a == comparison wouldn't check all the things in the first bullet point, "the same sequence of characters, same length, and same characters in corresponding positions" but in fact it does. As far as I can tell the only real difference when comparing two strings is that with ===, new String()===new String() returns false (different object references). But new String should be avoided anyway.

@CodyBugstein 2014-03-20 07:21:23

-1 The question was "what is the difference?" and you only explained the strict operators, but not the difference between them and the non-strict ones

@T J 2014-09-30 08:49:28

I didn't exactly get "Two objects are strictly equal if they refer to the same Object" - what? by two objects, does it mean two reference variables..?

@Luis Perez 2017-03-02 23:08:15

For plain English description of the issue see stackoverflow.com/a/38856418/984780

@sdfx 2009-02-07 11:57:35

Take a look here: http://longgoldenears.blogspot.com/2007/09/triple-equals-in-javascript.html

The 3 equal signs mean "equality without type coercion". Using the triple equals, the values must be equal in type as well.

0 == false   // true
0 === false  // false, because they are of a different type
1 == "1"     // true, automatic type conversion for value only
1 === "1"    // false, because they are of a different type
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false

@Koen Zomers 2011-02-01 13:02:46

Thanks for the clear answer! I guess if compared to C# the == would also be == and === would translate to .Equals()

@riship89 2012-09-18 00:36:58

what about "new String()===new String()", both values and types are same. But statement returns false.

@l8nite 2012-10-23 03:27:13

@hrishikeshp19: in that case, the values are actually different (different object references)

@Earth Engine 2012-11-22 02:37:13

@KoenZomers I don't think your C# case is right. Actually there are no equivalents in C#. == in C# do a reference compare, and Equals do predefined compare, none of them have equivalents in JavaScript either.

@danorton 2013-02-14 23:26:56

@hrishikeshp19, new String() is not of a string type, it's of an object type, so the === rule for objects applies. Usage of primitive strings, however, often results in coercing the strings into String objects, so the difference is subtle. If you were to assign new String() to two different objects, s1 and s2, the valueOf() method on each would return a string primitive for each, and s1.valueOf() === s2.valueOf() would return true.

@Pablo Jomer 2014-07-22 08:23:07

Also '0' == false // true and null == undefined //true

@Alexandru Severin 2015-03-09 09:54:34

Can someone explain why true == "true" is false?

@pixel 67 2015-06-26 10:39:18

I think in some cases you would would want to use == over === for example, getting a value from a text input would be '1234'// string not 1234// number

@Qwerty 2016-07-30 13:36:54

Is there a speed difference between == -1 and === -1 for anArray.indexOf(el)?

@Qwerty 2016-07-30 13:45:49

@AlexandruSeverin since true == 'true' === false but true == '1' === true, I would say that true gets converted to 1, which is then converted to '1', then '1' == 'true' === false && '1' == '1' === true

@Qwerty 2016-07-30 13:47:36

@Elisabeth Just wanted to bump you to see my previous comment on true == 'true' === false. Not an explanation, but it might give you some hints.

@Dror 2016-12-08 07:04:07

@sdfx you may have a typo above the example box. you wrote: " -The 3 equal signs mean equality without type coercion. - Using the triple equals, the values must be equal in type as well." i think you meant to write "The 2 equal signs mean "equality without type coercion". not "the 3..." Is this accurate ?

@Luis Perez 2017-03-02 23:09:08

For more unexpected examples check out stackoverflow.com/a/38856418/984780

@Shockwaver 2017-06-29 08:48:04

@EarthEngine Partially incorrect: in c# == makes a reference compare only with Reference Types. For Value Types the comparison involves the in-memory values. Otherwise with int a = 3; var b = (a == 3); b would always be false ;)

@Earth Engine 2017-06-29 11:11:45

@Shockwaver The fact is that == in c# only makes reference compare for reference types (class types) when there is no custom == operator defined (operator==). You can see this is especially true for string: it is defined as a value compare, not reference compare, because the existence of overloaded operator.

@Shockwaver 2017-06-29 15:01:36

@EarthEngine Sure thing, but all system defined value types call in a value comparison inside their operator== overloads. More in general as you now say it is true that the == behavior is decided inside the method body. But when you before said (5 yrs ago XD sry) that == only makes reference comparison it was not correct ;)

@Shockwaver 2017-06-29 15:08:59

For instance you could make a reference type operator== overload that will match against in-memory objects values!

@Earth Engine 2017-06-29 23:57:19

In fact I have already agreed your opinion in my last comment, and gives another counter-example: string is reference object, but its == compared by value, not by reference.

@Eggineer 2018-03-07 09:44:33

@riship89 Never declare Primitives (Number, String , Boolean etc) as objects as it produces nasty side effects eg. var x = "John"; var y = new String("John"); x===y will give false.

Related Questions

Sponsored Content

89 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 6159139 View
  • 7682 Score
  • 89 Answer
  • Tags:   javascript arrays

27 Answered Questions

[SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?

69 Answered Questions

[SOLVED] What is the most efficient way to deep clone an object in JavaScript?

86 Answered Questions

[SOLVED] How do JavaScript closures work?

47 Answered Questions

33 Answered Questions

[SOLVED] What's the difference between using "let" and "var"?

21 Answered Questions

[SOLVED] What is the difference between call and apply?

38 Answered Questions

[SOLVED] var functionName = function() {} vs function functionName() {}

Sponsored Content