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.
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:
Double equals (==) is a comparison operator, which transforms the operands having the same type before comparison.
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.
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.
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.
a = 2, b = 5, calculate c = a + b, and display c:
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>
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>
Here are the important differences between =, ==, and ===
|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.|
The static can be: Static Variables Static Methods Static Blocks Of Code. Let's look at static...
Classes and Objects in Java are the fundamental components of OOP's. Often there is a confusion...
What are Cookies? A cookie is a piece of data that is stored on your computer to be accessed by...
What is this Keyword in Java? this keyword in Java is a reference variable that refers to the...