Different between == and === operators in javascript
I am a beginner in Javascript and I am studying about operators, I am searching on the internet and I see many people use === to compare two variables so I have two questions need to explain:
1. What is different between == and === operators in javascript?
2. Which should I use?
Thanks for all your suggestions
-
S2
Sandeep Kumar Apr 03 2020
When declaring a variable we have two parts they are data type and value
var age = 18; // dataType = number, value = 18
So == operator only compare value and === compare both value and datatype
var age = 18; // dataType = number, value = 18 var myAge = "18"; // dataType = string, value = 18 age == myAge; // True age === myAge // False
-
P0
Phúc Lê Khắc Apr 04 2021
Here are the important differences between == and ===
== === == in JavaScript is used for comparing two variables, but it ignores the datatype of the variable. === is used for comparing two variables, but this operator also checks datatype and compares two values. It is called as a comparison operator It is also called as a comparison operator Checks the equality of two operands without considering their type. Compares equality of two operands with their types. 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. == make type correction based upon values of variables. === takes type of variable in consideration. The == checks for equality only after doing necessary conversations. If two variable values are not similar, then === will not perform any conversion. This is some view point from me about them, I hope it useful for you.
-
N0
Nhiên Hồng Phan Apr 04 2021
- == is used for comparing two variables, but it ignores the datatype of the variable whereas === is used for comparing two variables, but this operator also checks datatype and compares two values.
- == is called as a comparison operator whereas It is also called as comparison operator.
- == 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.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.