52

Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?

I asked another question here and received a great answer as follows:

$(document).on("keydown", function (e) {
  if (e.which === 8 && !$(e.target).is("input, textarea") || $(e.target).is('[readonly]')) {
      e.preventDefault();
  }
}); 

Notice the three equal signs === in the if-statement. I have always thought you only needed two equal signs == for a javascript/jQuery if-statement. Is there any reason for the three?

UPDATE

Sorry for the duplicate question - I searched but didn't find any good questions. I guess I was using the wrong search terms.

Community
  • 1
  • 1
FastTrack
  • 8,810
  • 14
  • 57
  • 78
  • 3
    I am guessing its checking absolute equality. Meaning, not just equal, but the same – Grigor Jun 27 '12 at 20:47
  • This other question has a lot of information on the subject (http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use) – MrOBrian Jun 27 '12 at 20:48
  • 2
    Stack Overflow's search feature cannot handle characters such as `=`, unfortunately. But Google does work: http://www.google.com/search?q=site:stackoverflow.com+javascript+%3D%3D%3D+vs+%3D%3D – Rob W Jun 27 '12 at 20:48

2 Answers2

78

The triple equal sign in javascript means equality without type coercion.

For example:

1=="1"     // true, automatic type coercion
1==="1"    // false, not the same type.
reza
  • 3
  • 4
Jeshurun
  • 22,940
  • 6
  • 79
  • 92
  • JS true, false and equal in details: https://gist.github.com/romankierzkowski/9024908 – RKI Feb 16 '14 at 03:21
  • +1 I think in terms of strongly typed languages - so the second example would throw an error. Still not used to this and been using javascript for a while now – Radmation Sep 12 '17 at 18:48
14

Three equal signs indicates both the value and type are equal.

Justin
  • 6,373
  • 9
  • 46
  • 72
  • 4
    I started to answer just like that, then figured this had to be a dupe, see [this](http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use) – TecBrat Jun 27 '12 at 20:48