There are two values in JavaScript that denote "null", respectively, undefined and null;
(1) Undefined
Defines a global variable whose value is undefined
that this variable is also called undefined
. But this variable is not a constant, nor is it a keyword. This means that its value can be easily overwritten.
ES5 Tip: in the strict mode of ECMAScript 5, it undefined
is no longer writable. But its name can still be hidden, such as defining a function named undefined
.
The following conditions return a undefined
value:
- Access to unmodified global variables
undefined
.
- A function that does not have an
return
expression defined implicitly returns.
return
An expression does not explicitly return anything.
- Accesses a property that does not exist.
- The function parameter has not been explicitly passed the value.
- Any variable that is set to a
undefined
value.
null
The use of
The undefined
usage scenario in JavaScript is similar to nullin other languages, which is actually a null
different type of data in JavaScript.
It has some usage scenarios inside JavaScript, such as declaring the end of a prototype chainFoo.prototype = null,又比如在删除DOM0级方法指定的事件处理程序时,
将事件处理程序属性的值设置为null即可,btn.onclick=null
), but in most cases it can be used undefined
instead.
JS Judgment Undefinde
typeof returns a string with six possibilities: "Number", "string", "Boolean", "Object", "function", "undefined"
The following are the correct uses:
var exp = undefined;
if (typeof (exp) = = "undefined")
{
Alert ("undefined");
}
JS to judge Null
This function is not isNull in JavaScript.
The following are the correct uses:
var exp = null;
if (!exp && typeof (exp)! = "undefined" && exp!=0)
{
Alert ("is null");
}
Front-end Interview Preparation 2----undefined and null summary in JavaScript