2

In auditing the Javascript version of 'verb', (a NURB library,) I happened across this method:

HxOverrides.cca = function(s,index) {
  var x = s.charCodeAt(index);
  if(x != x) return undefined;
  return x;
};

I'm puzzled by the condition,

if(x != x)

When is this ever True?

Jack
  • 2,229
  • 2
  • 23
  • 37
  • Though this question deals with the same specification detail, the questions are different: This question asks a specific question. In order to arrive at the "duplicate" questions, one would have to already have deduced that NaN inequality is at the heart of the issue. Also, the first "duplicate" is not even a question. – Jack Oct 06 '20 at 04:32
  • As long as the answers sufficiently answers your question I don't see a problem with the questions themselves being more specific. – glennsl Oct 06 '20 at 06:46
  • If that is a good library, it should contain test cases for such a case. You could check out that library containing its test suite, change the condition, and let the tests run – Nico Haase Oct 06 '20 at 06:59
  • My point is a student might encounter this case, and, like me, have no idea it relates to the NaN type, i.e. we shouldn't expect the person who is asking the question to already know the answer. – Jack Oct 12 '20 at 19:45

1 Answers1

2

In further reading, I discovered the Javascript method, "s.charCodeAt(index)" returns the Unicode value of the (index)th character in string 's'. Specifically:

If index is out of range, charCodeAt() returns NaN.

In the console, I tested:

NaN == NaN

I found this to be false. Therefore, as to the question of:

"When does x not equal x?"

the answer (at least in Javascript,) is:

"x != x when x is NaN (not a number)".

Jack
  • 2,229
  • 2
  • 23
  • 37