Difference Between =, ==, and === in JavaScript [Examples]

What is = in JavaScript?

equal to ( = ) is an assignment operator, which sets the varying on the entrust of the = to the measure of the expression that is on its right. This operator assigns lvalue to rvalue .
For exercise, Writing a=10 is ticket. If we write 10=10, ‘ a ’ = 10 or ‘ a ’ = ‘ a ’, it will result in a reference book error.

In this tutorial, you will learn :

What is == in JavaScript?

double equals ( == ) is a comparison operator, which transforms the operands having the like type before comparison .
therefore, when you compare string with a issue, JavaScript converts any string to a act. An empty string is always converts to zero. A string with no numeral value is converts to NaN ( not a Number ), which returns delusive .

What is === in JavaScript?

=== ( Triple equals ) is a rigorous equality comparison operator in JavaScript, which returns false for the values which are not of a exchangeable type. This operator performs type shed for equality. If we compare 2 with “ 2 ” using ===, then it will return a false respect .

Why use = in JavaScript?

here are the crucial uses of = in JavaScript :

= JavaScript operator assigns a value to the left operand depends on the value of operand available on the right side. The first operand should be a variable.
= JavaScript operator assigns a value to the bequeath operand depends on the value of operand available on the right side. The first operand should be a variable. The basic appointment operator is =, that assigns the value of one operand to another. That is, a = b-complex vitamin assigns the measure of b-complex vitamin to a .

See also  Difference between Volatile Memory and Non-Volatile Memory

Why use == in JavaScript?

here are the important uses of == in JavaScript :
The == operator is an equality hustler. It checks whether its two operands are the same or not by changing expression from one data type to others. You can use == operator in club to compare the identity of two operands even though, they are not of a alike type .

How === Works Exactly?

  • Strict equality === checks that two values are the same or not.
  • Value are not implicitly converted to some other value before comparison.
  • If the variable values are of different types, then the values are considered as unequal.
  • If the variable are of the same type, are not numeric, and have the same value, they are considered as equal.
  • Lastly, If both variable values are numbers, they are considered equal if both are not NaN (Not a Number) and are the same value.

Example of =

In the below program, there are two variables “ a ” and “ b ”. We are adding and printing their values using a third variable, “ c ”. The sum of the value of variable “ a ” and “ bel ” is 7. consequently, the output signal is 7 .







JavaScript Operators

a = 2, bel = 5, calculate c = a + boron, and display hundred :

Output:
a = 2, b = 5, forecast c = a + b-complex vitamin, and display vitamin c :
7

Example of ==

In the below program, we have declared one variable “ a ” having measure 10. last, the instruction a == 20 returns false as the value of a is 10 .













Output:
false

Example of ===

In the below course of study, the value of variable adam is 10. It is compared to 10 written in double-quotes, which is considered as a string, and consequently, the values are not rigorously the same. The end product of the broadcast is false .













Output:

delusive

= Vs == VS === in JavaScript

here are the crucial differences between =, ==, and ===

=
==
===

= in JavaScript is used for assigning values to a variable.
== in JavaScript is used for comparing two variables, but it ignores the datatype of variable.
=== is used for comparing two variables, but this operator also checks datatype and compares two values.

It is called as assignment operator
It is called as comparison operator
It is also called as comparison operator

The assignment operator can evaluate to the assigned value
Checks the equality of two operands without considering their type.
Compares equality of two operands with their types.

It does not return true or false
Return true if the two operands are equal. It will return false if the two operands are not equal.
It returns true only if both values and data types are the same for the two variables.

= simply assign one value of variable to another one.
== make type correction based upon values of variables.
=== takes type of variable in consideration.

== 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.

KEY DIFFERENCES:

  • = is used for assigning values to a variable, == is used for comparing two variables, but it ignores the datatype of variable whereas === is used for comparing two variables, but this operator also checks datatype and compares two values.
  • = is called as assignment operator, == is called as comparison operator whereas It is also called as comparison operator.
  • = does not return true or false, == Return true only if the two operands are equal while === returns true only if both values and data types are the same for the two variables.
generator : https://livingcorner.com.au
Category : What is?