1

As the question says, what exactly is the function of the !== operator? (Exclaimation, Equals, Equals).

Yes there are two equal signs. I found it here.

function endsWith(str, suffix) { 
    return str.indexOf(suffix, str.length - suffix.length) !== -1;  
}

Is it a typo? I doubt it. It is an old post and with so many upvotes, it should surely have been corrected one day or the other.

Or is it a valid operator? If so, what does it do?

Community
  • 1
  • 1
Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52
  • 2
    [MDN - Non-identity / strict inequality (!==)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Non-identity_strict_inequality_(!)) – zerkms Dec 04 '15 at 20:39
  • There are many questions on this topic which can be found on this site. I recommend reading [this](http://bonsaiden.github.io/JavaScript-Garden/#types.equality) about comparisons. – A1rPun Dec 04 '15 at 20:41
  • 3
    [ES2015 - 7.2.13 Strict Equality Comparison](http://www.ecma-international.org/ecma-262/6.0/#sec-strict-equality-comparison) – zerkms Dec 04 '15 at 20:41
  • Ok, I understand it's a duplicate. But I could not find it even by googling. And it's only a partial duplicate. – Fᴀʀʜᴀɴ Aɴᴀᴍ Dec 04 '15 at 20:42
  • And if i don't know about `!==`, how in the world am I expected to search for `===`? Usually I would delete duplicates but not this one. I don't think at all that this question should have been closed. – Fᴀʀʜᴀɴ Aɴᴀᴍ Dec 04 '15 at 20:44
  • 4
    @FᴀʀʜᴀɴAɴᴀᴍ no one blames you - it's tricky to find about operators in the google (but now you know that it's easy to do in the standard) – zerkms Dec 04 '15 at 20:49
  • @zerkms Nor do I think anyone blames me. It's just that duplicate flags dishearten me. :) – Fᴀʀʜᴀɴ Aɴᴀᴍ Dec 04 '15 at 20:55

2 Answers2

2

It's strict inequality: a !== b iff type(a) ≠ type(b) or ab.

Since strict equality (===) is defined as:

  • a === b iff type(a) = type(b) ∧ a = b,

Strict inequality is its negation, so:

  • !(a === b)
  • !(type(a) = type(b) ∧ a = b)
  • ≡ (type(a) ≠ type(b) ∨ ab).

References. The semantics of the comparison are in #strict-equality-comparison of the ES6 spec.

Ack. Thanks to zerkms for the correction: types are evaluated first. That's important because if the types fail to match, the inequality won't even be checked. Also thanks to him for pointing me to the right place in the spec for the semantics of the relation.

Community
  • 1
  • 1
Hunan Rostomyan
  • 2,176
  • 2
  • 22
  • 31
1

Valid. It's basically doing strict non equality comparison. Checking to see if the type is not equal as well as the value.

Quy
  • 1,343
  • 9
  • 11