0

Question: I confuse the operator explain description below: Is that correct?

var length = "50";

if (length === 50) {...} // It allows for a block of code to fire if the length equals 50 regardless of the data type.

Thanks,

Colin DeClue
  • 2,194
  • 3
  • 26
  • 47
  • 4
    possible duplicate of [Does it matter which equals operator (== vs ===) I use in JavaScript comparisons?](http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons) – Colin DeClue Aug 02 '13 at 19:16
  • 1
    That's what `==` will do - type coercion. `===` enforces that the two operands are the same type (and value, obviously) – Ian Aug 02 '13 at 19:16
  • Nope, that's backwards. `==` allows type coercion (operands can be different but "compatible" types), while `===` is a strict comparison that doesn't do type coercion (operands have to be the exact same type). [Comparison operator docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FOperators%2FComparison_Operators). – ajp15243 Aug 02 '13 at 19:18
  • 1
    Please do some basic research. This is in the first chapter of any manual. –  Aug 02 '13 at 19:20
  • Just read the documentation and you will know whether it is correct or not. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators – Felix Kling Aug 02 '13 at 20:34
  • Not that it will be easy to read/follow at first, but here's the "official" specs on how the two operators should work internally: http://es5.github.io/#x11.9 – Ian Aug 02 '13 at 20:41

2 Answers2

1

== checks if the 2 values are the same. === checks if the 2 values are the same and are of the same type.

Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
0

a == b means check if a is equal to b, and don't worry about what types they are

a === b means check if a is exactly, type and all, equal to b

Samuel Reid
  • 1,756
  • 12
  • 22