21

Is this the correct way? I want the statement to run if the value of somevar equals the string?

if (somevar = '836e3ef9-53d4-414b-a401-6eef16ac01d6'){
 $("#code").text(data.DATA[0].ID);
}
Prometheus
  • 32,405
  • 54
  • 166
  • 302

3 Answers3

51

No. = sets somevar to have that value. use === to compare value and type which returns a boolean that you need.

Never use or suggest == instead of ===. its a recipe for disaster. e.g 0 == "" is true but "" == '0' is false and many more.

More information also in this great answer

Community
  • 1
  • 1
makeitmorehuman
  • 11,287
  • 3
  • 52
  • 76
10

NO, when you are using only one "=" you are assigning the variable.

You must use "==" : You must use "===" :

if (somevar === '836e3ef9-53d4-414b-a401-6eef16ac01d6'){
 $("#code").text(data.DATA[0].ID);
}

You could use fonction like .toLowerCase() to avoid case problem if you want

sdespont
  • 13,915
  • 9
  • 56
  • 97
1

First of all you should use double "==" instead of "=" to compare two values. Using "=" You assigning value to variable in this case "somevar"

bognix
  • 201
  • 1
  • 6