What is = in JavaScript?Equal to (=) is an assignment operator, which sets the variable on the left of the = to the value of the expression that is on its right. This operator assigns lvalue to rvalue. Show
For example, Writing a=10 is fine. If we write 10=10, ‘a’ = 10 or ‘a’ = ‘a’, it will result in a reference error. In this tutorial, you will learn:
What is == in JavaScript?Double equals (==) is a comparison operator, which transforms the operands having the same type before comparison. So, when you compare string with a number, JavaScript converts any string to a number. An empty string is always converts to zero. A string with no numeric value is converts to NaN (Not a Number), which returns false. What is === in JavaScript?=== (Triple equals) is a strict equality comparison operator in JavaScript, which returns false for the values which are not of a similar type. This operator performs type casting for equality. If we compare 2 with “2” using ===, then it will return a false value. Why use = in JavaScript?Here are the important 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. The basic assignment operator is =, that assigns the value of one operand to another. That is, a = b assigns the value of b to a. Why use == in JavaScript?Here are the important uses of == in JavaScript: The == operator is an equality operator. 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 order to compare the identity of two operands even though, they are not of a similar type. How === Works Exactly?
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 “b” is 7. Therefore, the output is 7. <!DOCTYPE html> <html> <body> <h2>JavaScript Operators</h2> <p>a = 2, b = 5, calculate c = a + b, and display c:</p> <p id="demonstration"></p> <script> var a = 2; var b = 5; var c= a + b; document.getElementById("demonstration").innerHTML = c; </script> </body> </html> Output: a = 2, b = 5, calculate c = a + b, and display c: 7 Example of ==In the below program, we have declared one variable “a” having value 10. Lastly, the statement a == 20 returns false as the value of a is 10. <!DOCTYPE html> <html> <body> <p id="demonstration"></p> <script> var a = 10; document.getElementById("demonstration").innerHTML = (a == 20); </script> </body> </html> Output: false Example of ===In the below program, the value of variable x is 10. It is compared to 10 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 x = 10; document.getElementById("demo").innerHTML = (x === "10"); </script> </body> </html> Output: false = Vs == VS === in JavaScriptHere are the important differences between =, ==, and ===
KEY DIFFERENCES:
Why we use == and === in JavaScript?= is used for assigning values to a variable in JavaScript. == is used for comparison between two variables irrespective of the datatype of variable. === is used for comparision between two variables but this will check strict type, which means it will check datatype and compare two values.
What is the use of == === operators?The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.
What does == === mean?The === operator means "is exactly equal to," matching by both value and data type. The == operator means "is equal to," matching by value only.
What is the function of === in JavaScript?Using Strict Equal (===) operator: In JavaScript, '===' Operator is used to check whether two entities are of equal values as well as of equal type provides a boolean result. In this example, we use the '===' operator. This operator, called the Strict Equal operator, checks if the operands are of the same type.
|