A method for determining whether a value is undefined in JavaScript _javascript tips

Source: Internet
Author: User

Objective

I'm sure you all know that when you declare a variable and do not assign a value, its initial value is undefined. But in JavaScript, how do you check if a value is undefined?

In short, in modern browsers, you can safely compare whether a variable is undefined

if (name = = = undefined) {...}

Some people object to using the undefined variable directly for comparison because the value in the old browser allows it to be assigned a value, such as the following:

undefined = "Test"

Using the undefined directive will not correctly detect whether a variable is assigned after being assigned a value.

However, this behavior was repaired in the 2009 ECMAScript 5.

15.1.1.3 undefined the
value of undefined is undefined (8.1). This property has the attributes {[[writable]]: false, [[Enumerable]]: false, [[configurable]]: false}.

In modern browsers, the value of undefined will not be overridden

What do we need to support IE8 or older browsers?

Usually the undefined instructions are safe. There is no reason to modify the value of undefined in the application.

Thomas's answer, using persuasive reasoning, demonstrates this.

I don ' t hear people telling I-I shouldn ' t use settimeout because someone can
I don't listen to people telling me that I shouldn't be using settimeout because someone like that :
Window.settimeout = function () {
  alert ("Got you now!");

The following line, "It can be assigned again", raw === undefined returns False

If you still care, there are two ways to check whether a value is undefined, even if the global window.undefined has been overridden

if (name = = void (0)) {...}

In this example 0 does not have any practical meaning, you want to use also does not 1 or function(){} matter. void(anything)will be counted to get undefiend.

Alternatively, you can use the TypeOf operator to safely check whether you have been assigned a value. You can check whether the type of a value is "undefined" instead of the global undefined comparison.

if (typeof name = = "undefined") {...}

Note that the second option is slightly different from the previous one. Although name is not declared, TypeOf will return him as undefined. If you use name directly you undefinedor void(0) will get ReferenceError an unusual error.

But do not use void (0) directives

Avoid void (0) in your code or typeof x === "undefined" , you can use isUndefined function the method to wrap them up so that you don't have to specify them when you use them.

function isundefined (value) {
  //get undefined to ensure that it has not been assigned
  var undefined = void (0);
  return value = = undefined;
}

Some tool libraries have already deployed this method, for example: _.isUndefined , the underscore isUndefined methods in

Summarize

The above is the entire content of this article, I hope to be able to learn or work to bring certain help, if you have questions you can message exchange.

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.