How do you check that a number is NaN in JavaScript?

前端 未结 30 2637
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 06:19

I’ve only been trying it in Firefox’s JavaScript console, but neither of the following statements return true:

parseFloat(\'geoff\') == NaN;

parseFloat(\'ge         


        
相关标签:
30条回答
  • 2020-11-22 06:48

    As far as a value of type Number is to be tested whether it is a NaN or not, the global function isNaN will do the work

    isNaN(any-Number);
    

    For a generic approach which works for all the types in JS, we can use any of the following:

    For ECMAScript-5 Users:

    #1
    if(x !== x) {
        console.info('x is NaN.');
    }
    else {
        console.info('x is NOT a NaN.');
    }
    

    For people using ECMAScript-6:

    #2
    Number.isNaN(x);
    

    And For consistency purpose across ECMAScript 5 & 6 both, we can also use this polyfill for Number.isNan

    #3
    //Polyfill from MDN
    Number.isNaN = Number.isNaN || function(value) {
        return typeof value === "number" && isNaN(value);
    }
    // Or
    Number.isNaN = Number.isNaN || function(value) {     
        return value !== value;
    }
    

    please check This Answer for more details.

    0 讨论(0)
  • 2020-11-22 06:49

    NaN is a special value that can't be tested like that. An interesting thing I just wanted to share is this

    var nanValue = NaN;
    if(nanValue !== nanValue) // Returns true!
        alert('nanValue is NaN');
    

    This returns true only for NaN values and Is a safe way of testing. Should definitely be wrapped in a function or atleast commented, because It doesnt make much sense obviously to test if the same variable is not equal to each other, hehe.

    0 讨论(0)
  • 2020-11-22 06:49
    NaN === NaN;        // false
    Number.NaN === NaN; // false
    isNaN(NaN);         // true
    isNaN(Number.NaN);  // true
    

    Equality operator (== and ===) cannot be used to test a value against NaN.

    Look at Mozilla Documentation The global NaN property is a value representing Not-A-Numbe

    The best way is using 'isNaN()' which is buit-in function to check NaN. All browsers supports the way..

    0 讨论(0)
  • 2020-11-22 06:50

    marksyzm's answer works well, but it does not return false for Infinity as Infinity is techinicly not a number.

    i came up with a isNumber function that will check if it is a number.

    function isNumber(i) {
        return !isNaN(i && i !== true ? Number(i) : parseFloat(i)) && [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY].indexOf(i) === -1;
    }
    
    console.log(isNumber(Infinity));
    console.log(isNumber("asdf"));
    console.log(isNumber(1.4));
    console.log(isNumber(NaN));
    console.log(isNumber(Number.MAX_VALUE));
    console.log(isNumber("1.68"));

    UPDATE: i noticed that this code fails for some parameters, so i made it better.

    function isNumber(i) {//function for checking if parameter is number
    if(!arguments.length) {
    throw new SyntaxError("not enough arguments.");
    	} else if(arguments.length > 1) {
    throw new SyntaxError("too many arguments.");
    	} else if([Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY].indexOf(i) !== -1) {
    throw new RangeError("number cannot be \xB1infinity.");
    	} else if(typeof i === "object" && !(i instanceof RegExp) && !(i instanceof Number) && !(i === null)) {
    throw new TypeError("parameter cannot be object/array.");
    	} else if(i instanceof RegExp) {
    throw new TypeError("parameter cannot be RegExp.");
    	} else if(i == null || i === undefined) {
    throw new ReferenceError("parameter is null or undefined.");
    	} else {
    return !isNaN(i && i !== true ? Number(i) : parseFloat(i)) && (i === i);
    	}
    }
    console.log(isNumber(Infinity));
    console.log(isNumber(this));
    console.log(isNumber(/./ig));
    console.log(isNumber(null));

    0 讨论(0)
  • 2020-11-22 06:53

    Simply convert the result to String and compare with 'NaN'.

    var val = Number("test");
    if(String(val) === 'NaN') {
       console.log("true");
    }
    
    0 讨论(0)
  • 2020-11-22 06:54

    function isNotANumber(n) {
      if (typeof n !== 'number') {
        return true;
      } 
      return n !== n;
    }

    0 讨论(0)
提交回复
热议问题