Signup/Sign In

Difference Equality Strict Operator(===) in JavaScript

Posted in Programming   LAST UPDATED: SEPTEMBER 6, 2021

    Are you also confused between =, ==, and === operators in javascript? If yes, then don’t worry because you have ended up in the right place. In this article, we will discuss what =, ==, and === operators in javascript are, and what is the difference between =, ==, and === operators.

    So, let’s get started.

    What is equal to (=) Operator in JavaScript?

    Equal to (=) is an assignment operator. This operator assigns the left side value to the right side value. Basically, it sets the value on the left of the = to the value of the expression that is on its right.

    What is equal to (==) Operator in JavaScript?

    Double equals (==) is a comparison operator that transforms the operands having the same type before comparison. It tests for abstract equality. This means before doing the equality comparison it performs the necessary type conversion.

    What is equal to (===) Operator in JavaScript?

    Triple equals(===) is a strict equality comparison operator in JavaScript, that returns false for the values which are not of a similar type. It performs type casting for equality.

    Uses of equal to (=) Operator in JavaScript

    These are the following important uses of = in JavaScript:

    • It assigns the value of one operand to another. For example, if x=y, then the equal to operator assigns the value of y to a.
    • It assigns a value to its left operand based on the value of its right operand. Here, the first operand should be a variable.

    Uses of equal to (==) Operator in JavaScript

    These are the following important uses of == in JavaScript:

    • The == operator in order to compare the identity of two operands even though they are not of a similar type.

    Uses of equal to (===) Operator in JavaScript

    These are the following important uses of === in JavaScript:

    • It checks whether the two values are the same or not.
    • If the two variable values are of different types, then the values are considered unequal.
    • If the two variables are of the same type, are not numeric, and have the same value, they are considered equal.
    • Here, before comparison, the values are not implicitly converted to some other value before comparison.

    Example of equal to (=) Operator

    In the below program, there are two variables "x" and "y". After adding, we will store the result in a third variable, "z". The sum of the value of variables "x" and "y" is 7. Therefore, the output is 7.

    <!DOCTYPE html>
    <html>
    <body>
    <h2>JavaScript Equal to (=) Operators</h2>
    <p>x = 2, y = 5, calculate z = x + y, and display z:</p> 
    <p id="Demonstration"></p>
    <script>
    var x = 2;
    var y = 5;
    var z= x + y;
    document.getElementById("demonstration").innerHTML = z;
    </script>
    </body>
    </html>
    
    


    x = 2, y = 5, calculate z = x + y, and display z:

    7

    Example of equal to (==) Operator

    In the below program, there is variable "x" having a value of 40. But, the statement x == 50 returns false as the value of x is 40.

    <!DOCTYPE html>
    <html>
    <body>
    <p id="Demonstration"></p>
    <script>
    var x = 40;
    document.getElementById("demonstration").innerHTML = (x == 50);
    </script>
    </body>
    </html>
    
    


    false

    Example of equal to (===) Operator

    In the below program, the value of variable a is 20. It is compared to 20 written in double-quotes, which is considered as a string, and therefore, the values are not strictly the same. The output of the program is false.

    <!DOCTYPE html>
    <html>
    <body>
    <p id="demo"></p>
    <script>
      var a = 20;
      document.getElementById("demo").innerHTML = (a === "20");
    </script>
    </body>
    </html>
    
    


    false

    equal to (=) Vs equal to (==) Vs equal to (===) Operator

    These are the following key difference between equal to (=), equal to (==), and equal to (===) Operator:

    • equal to (=) Operator is known as assignment operator, == is known as comparison operator, and === is known as strict equality comparison operator.
    • The assignment operator is used for assigning values to a variable, the comparison operator is used for comparing two variables, but it ignores the datatype of the variable whereas the strict equality comparison operator is used for comparing two variables, but this operator also checks datatype and compares two values.
    • The assignment operator can evaluate the assigned value, the comparison operator checks the equality of two operands without considering their type whereas the strict equality comparison operator compares the equality of two operands with their types.
    • The assignment does not return true or false, the comparison operator returns true if the two operands are equal. It will return false if the two operands are not equal whereas the strict equality comparison operator returns true only if both values and data types are the same for the two variables.
    • = will not compare the value of variables at all, the == checks for equality only after doing necessary conversations, whereas if two variable values are not similar, then === will not perform any conversion.

    Comparison Chart: equal to (=) Vs equal to (==) Vs equal to (===) Operator

    Let's summarize the above-discussed points in a tabular form.

    = Operator == Operator === Operator
    = is known as an assignment operator. == is known as a comparison operator. === is known as a strict equality comparison operator.
    The assignment operator is used for assigning values to a variable. The comparison operator is used for comparing two variables, but it ignores the datatype of the variable. The strict equality comparison operator is used for comparing two variables, but this operator also checks data type and compares two values.
    = will not compare the value of variables at all The == checks for equality only after doing necessary conversations. If two variable values are not similar, then === will not perform any conversion.
    The assignment does not return true or false. The comparison operator returns true if the two operands are equal. It will return false if the two operands are not equal. The strict equality comparison operator returns true only if both values and data types are the same for the two variables.
    The assignment operator can evaluate the assigned value. The comparison operator checks the equality of two operands without considering their type. The strict equality comparison operator compares the equality of two operands with their types.

    Conclusion:

    I hope from the above article it is clear what =, ==, and === operators are, what is the use of =, ==, and === operators, and what is the difference between =, ==, and === operators.

    You may also like:

    About the author:
    I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.
    Tags:JavaScriptEqualsOperator
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS