+ 1
What is the difference, in JavaScript, between =, == and === ?
3 Respuestas
+ 1
= for assigning a value to a variable
like var age = 20;
== for comparing two variables whether they are of equal type
=== for comparing two variables whether they are strictly equal
sample code:
var age1= 20; //age1 is just a variable not object
var age2 = new number(20); /* age2 is an object here as we used number constructor function using the keyword new */
alert(age1 == age2); // true
alert(age1 === age2); //false
hope this helps you
0
From what I've learned so far, I've only seen = and ===. = is used when you're using a variable to represent something, like a function, a string, etc (var firstName = Bob). === is used for comparing two data, like 3 === 3 (3 is equal to 3). I hope this helps.
0
example for ==
var a = "42"; // string
var b = 42; // number
if(a == b) // true
in case of == the comparison is made between two values after the necessary conversion of values of either side from one data type to another. like in the above example ist a is converted into NUMBER from STRING data type and then the comparison occurs and result is true.
but if we have used ===. operator then the result would have been false
because this operator won't let convert the values of either side their data type.
if(a===b) // false