DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Equality in JavaScript

Equality in JavaScript

Axel Rauschmayer user avatar by
Axel Rauschmayer
·
Jun. 16, 11 · Web Dev Zone · Interview
Like (0)
Save
Tweet
2.93K Views

Join the DZone community and get the full member experience.

Join For Free

There are two operators for comparing values in JavaScript: strict equality === and “normal” (or lenient) equality ==. Many style guides (correctly) tell programmers to avoid lenient equality and always use strict equality. This post explains why.

Whenever appropriate, related sections in the ECMAScript 5 language specification [1] are mentioned in square brackets.

Two ways of comparing

  • The strict equality operator === only considers values equal that have the same type.
  • The lenient equality operator == tries to convert values of different types, before comparing like strict equality.
Lenient equality causes two problems:
  • The conversion rules are counter-intuitive and do things you might not expect.
  • As the operator is so forgiving, type errors can remain hidden longer.

Strict equals ===

[ES5 11.9.6] Comparing two values. Values with different types are never equal. If both values have the same type then the following assertions hold.
  • undefined === undefined
  • null === null
  • Two (primitive) numbers:
        NaN !== _  // any value including NaN
    x === x
    +0 === -0
    for any number x. Thus equality is not transitive in JavaScript, because NaN is not equal to itself.
  • Two booleans, two strings (primitive): obvious results
  • Two objects (including arrays and functions): x === y only if x and y are the same object(!). That is, if you want to compare different objects, you have to do it manually.
Examples:
    > var a = NaN;
    > a === a
    false
    > var b = {}, c = {};
    > b === c
    false
    > b === b
    true
    > "abc" === new String("abc")
    false // different types (left: primitive, right: object)

Equals ==

[ES5 11.9.3] Comparing two values. If both values have the same type: compare with ===. Otherwise:
  1. undefined == null
  2. One number, one string: convert the string to a number
  3. A boolean and a non-boolean: convert the boolean to a number and then perform the comparison.
  4. Comparing a string or a number to an object: try to convert the object to a primitive and then make the comparison.
(3) leads to a weird idiosyncrasy where numbers greater than 1 are true in if statements, but not equal to true:
    > 0 == false
    true
    > 1 == true
    true
    > 2 == true
    false
    > 2 ? true : false
    true // because 2 !== 0
Equality and strings:
    > "" == 0
    true
    > "123" == 123
    true
    > "" == false
    true
    > "1" == true
    true
    > "2" == true
    false
    > "true" == true
    false
    > "2" ? true : false
    true // because string is non-empty
    > "abc" == new String("abc")
    true // right side converted to primitive

 

From http://www.2ality.com/2011/06/javascript-equality.html

JavaScript

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • When Writing Code Isn't Enough: Citizen Development and the Developer Experience
  • 4 Careers for People with Coding Backgrounds
  • What Is Pair Programming?
  • How BDD Works Well With EDA

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo