JS in isNaN () method parses

The meaning of existence 1. isNaN ()

Because NaN is the only one not equal to its value, unlike other values, you can use the equality operator to determine whether equal to itself, NaN == NaNand NaN === NaNwill return false, so isNaN()was born, and that it is in the end what role it plays, let me see below .

Principle 2. isNaN () judgment

isNaNFunction takes a single parameter, the principle is first to try to convert the numerical parameter, the call is Number()a method, then judge.

Here it is necessary to introduce the Number()method, in fact, Number()the principle of the method is a bit complicated, specific two cases.
Number()The method also takes one parameter:
a parameter data type original
raw data types: numeric, string, Boolean, undefined, null
first call parameter valueOfmethod, then Number()the determination method

    //1. 数值:自然转为数值

    //2. 字符串
    Number('123') // 123
    Number('123abc') // NaN
    Number(' ') // 0

    // 3. 布尔值
    Number(true) // 1
    Number(false) // 0

    // 4. undefined
    Number(undefined) // NaN

    //5. null
    Number(null) // 0

Second, the parameter is an object
prior to the call parameter valueOfmethod, and then the call parameters of toString()the method, with the final Number()judgment method.
The returned parameters when the object is NaNan array that contains parameters unless individual values (arrays are objects)

    Number({a:1}) // NaN
    Number([1, 2, 3]) //    NaN
    Number([1]) // 1

Introduction to Number()after the principle of the method, one can know the isNaN()result of the judgment.

3. Return true when, what time it returns false

Returns to true: NaN, objects (in addition to an array containing a single value), undefined, can not use Number()the method into numeric strings

Returns false: value null, Boolean, use Number()the method into numeric string, comprising an array of individual values

4. isNaN () action

The isNaN()method returns trueor falsemay detect whether the parameters can be like numberthat that can be computed, if not operation, this parameter can be given a default value or other suitable content, it is possible to obtain an implicit conversion parameter values function.

Guess you like

Origin www.cnblogs.com/moqijianqi/p/11413441.html