Front-end Interview Preparation 2----undefined and null summary in JavaScript

Source: Internet
Author: User

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.
    • returnAn 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.
  nullThe 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.