3

I have been trying to check the value and type of NaN for equality but I am getting errors every time. It seems like there must be a simpler solution. Here are four possible solutions I have tried so far that do not work. Another thing is that the underscore in the first solution is not recognized on edabit or codepen:

solution 1

function checkEquality(a, b) {
    if((typeof a === typeof b) ||
          (_.isNaN(a) === true) &&
           (_.isNaN(b) === true)) {
      return true;
  }
}

solution 2

function checkEquality(a, b) {
  let divisionByZod = 42 / "General Zod";
  let valueIsNaN = (divisionByZod !== divisionByZod);

  if((typeof a === typeof b) || (42 / 'General Zod' === true)) {

    return true;
  }
}

solution 3

function checkEquality(a, b) {
  if(((typeof a === typeof b) || (isNaN(parseInt(NaN)))))  {
    return true;
  }
}

solution 4

function checkEquality(a, b) {
  let c = NaN;
  if(((typeof a !== typeof b) || (isNaN(parseInt(c) !== NaN))))  {
    return false;
  }
}
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Henry A
  • 47
  • 2
  • 3
    javaScript has the [isNaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN) function built in. – admcfajn Jan 27 '20 at 18:29
  • 6
    Does this answer your question? [How do you check that a number is NaN in JavaScript?](https://stackoverflow.com/questions/2652319/how-do-you-check-that-a-number-is-nan-in-javascript) – admcfajn Jan 27 '20 at 18:30

4 Answers4

2

The best option to check if a variable is not a number is the isNan() function.

https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/isNaN

Is good to observe that :

Unlike all other possible values in JavaScript, it is not possible to rely on the equality operators (== and ===) to determine whether a value is NaN or not, because both NaN == NaN and NaN === NaN evaluate to false. Hence, the necessity of an isNaN function.

I don't know your use case but take in consideration that it is not possible to rely on the equality operators (== and ===) to determine whether two objects are equal or not, to this purpose the best is use Object.is() function

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is

Observe that :

Object.is() determines whether two values are the same value. Two values are the same if one of the following holds:

both undefined both null both true or both false both strings of the same length with the same characters in the same order both the same object (means both object have same reference) both numbers and both +0 both -0 both NaN or both non-zero and both not NaN and both have the same value This is not the same as being equal according to the == operator. The == operator applies various coercions to both sides (if they are not the same Type) before testing for equality (resulting in such behavior as "" == false being true), but Object.is doesn't coerce either value.

This is also not the same as being equal according to the === operator. The === operator (and the == operator as well) treats the number values -0 and +0 as equal and treats Number.NaN as not equal to NaN.

Knowing that I think that Object.is(a,b) is the solution for your problem.

const nan1 = 'a string' / 2
const nan2 = 'another string' / 2
const comparation = Object.is(nan1,nan2)
console.log(comparation) // true
1
if (isNaN(x)) {
return 'Not a Number!';
}

isNaN is the function you are looking for

Dylan
  • 2,161
  • 2
  • 27
  • 51
1

Try function isNaN(). Returns true if value NaN

dimastr90
  • 44
  • 2
0

Try to use the Object.is() method. It determines whether two values are the same value. Two values are the same if one of the following holds:

  • both undefined

  • both null

  • both true or both false

  • both strings of the same length with the same characters in the same order

  • both the same object (means both object have same reference)

  • both numbers and

  • both +0

  • both -0

  • both NaN

or both non-zero and both not NaN and both have the same value

Refer to the following documentation which

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is

backdoor
  • 891
  • 1
  • 6
  • 18