Over a million developers have joined DZone.

The Javascript Equality Operator

You’re wondering why that piece of code inside an if block is being executed while you expected it to be skipped, right? Well, let’s look at what’s happening under the hood to understand why exactly that is happening.

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

You’re wondering why that piece of code inside an if block is being executed while you expected it to be skipped, right?

Well, let’s look at what’s happening under the hood to understand why exactly that is happening.

Javascript supports two types of equality operators:

  • The Equality Operator(==) and
  • The Strict Equality Operator (===)
  • The equality operator considers the "types" of the operands being compared and applies some rules on the operands with respect to type conversion.

    Two values are strictly equal only if they have the same type and the same value.

    We will focus on the equality operator in this article since, well, there’s not much to highlight with respect to the strict equality operator J.

    I hope you are aware of the primitive data types in JavaScript:

    • Boolean
    • Strings
    • Numbers

    Case1: Both Operands of the Same Type

    This is the simplest case. If both the operands are of the same type, then compare their values. No type conversion required.

    Case 2: Compare a String and a Number

    1. The string is converted to a number.

      • Does the string represent a valid number: Compare the values.

      • String does not have a valid number(NaN): Return false.

    Case 3: Compare a Boolean to a Number

    1. Convert the Boolean to a number. True evaluates to 1, false evaluates to 0.

    2. Compare the values.

    Case 4: Compare a Boolean to a String

    1. Convert the Boolean to a number. True evaluates to 1, false evaluates to 0.

    2. Convert the String to a number.

    3. Compare the values.

    Case 5: Compare Undefined to Null

    Evaluates to true. Strange but true.

    Since both null and undefined represent "no value" (i.e. an object with no value and a variable with no value respectively.)   

    Some Rules of Type Conversion:

    • The string " " converts to 0
    • Anything compared to NaN evaluates to False.
    • Anything (except null) compared to Undefined evaluates to False.

    Operand Type

    Boolean

    String

    Numbers

    NAN/Undefined

    Boolean

    True if both the operands have the same value

    Convert the string to a number if possible and then compare

    Convert Boolean to number and compare. True evaluates to 1 and false evaluates to 0

    False

    String

    Convert Boolean to number and compare. True evaluates to 1 and false evaluates to 0

    True if both the operands have the same value

    Convert the string to a number if possible and then compare

    False

    Numbers

    Convert Boolean to number and compare. True evaluates to 1 and false evaluates to 0

    Convert the string to a number if possible and then compare

    True if both the operands have the same value

    False

    Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

    Topics:
    javascript ,equality ,operators

    Opinions expressed by DZone contributors are their own.

    The best of DZone straight to your inbox.

    SEE AN EXAMPLE
    Please provide a valid email address.

    Thanks for subscribing!

    Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
    Subscribe

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}