What is the fundamental difference between the use of == and === in Javascript


#1



Please explain why the below code is incorrect when the variables used are Strings.
if(text[i]==myName[0])


#2

Why is if(text[i]==myName[0]) incorrect? It is fine, it just doesn't compare data type. Using two equals means data type is not compared. lets say we have this code:

number1 = 1
number2 = "1"

now, number1 == number2 would be true, since both have a value of 1. But number1 === number2 is false, since number1 is integer, and number2 a string.


#3

Thank you for your reply.
What if the data structures under comparison consist of a character array and a string?


#4

No idea, one easy way to find out:

test1 = ['hello'];
test2 = 'hello';
if (test1 == test2){
  console.log(true)
} else {
  console.log(false)
}
if (test1 === test2){
  console.log(true)
} else {
  console.log(false)
}

as long as data type doesn't matter (==), this will return true


#5

Thanks...
Well, what happened was this. I've never seen the === because i used to program in Java, and all of a sudden the editor suggests me to correct the == to ===. I just got a little confused.