Can I set variables to undefined or pass undefined as an argument?

后端 未结 10 1887
北荒
北荒 2020-11-30 15:53

I’m a bit confused about JavaScript’s undefined and null values.

What does if (!testvar) actually do? Does it test for u

10条回答
  •  失恋的感觉
    2020-11-30 16:39

    The best way to check for a null values is

    if ( testVar !== null )
    {
        // do action here
    }
    

    and for undefined

    if ( testVar !== undefined )
    {
        // do action here
    }
    

    You can assign a avariable with undefined.

    testVar = undefined;
    //typeof(testVar) will be equal to undefined.
    

提交回复
热议问题