Difference between void and undefined in javascript

Source: Internet
Author: User

Undefined

All along, undefined are not JavaScript keywords or reserved words, which means we can modify the value of window.undefined.

Because undefined will be frequently called as operands of the comparison operation, and so on, modify the value of undefined is a disadvantage, so after ES5 specification, window.undefined is defined as not writable, not configurable properties.

Common undefined:

Variables are not assigned after they are declared, the variables are automatically assigned to undefined

Some formal parameters are defined in the function, and if the incoming argument is less than the predefined formal parameter, then some of the formal parameters will not match the argument and will then be automatically assigned to undefined

A function that does not return a value and returns undefined by default

void operator

The void operator is followed by an expression that, regardless of the content of the expression, is invoked to execute after void, and the void operator returns undefined after execution:

void (expression)

In short, there are three uses of the void operator:

Generate undefined

Let the function execute immediately

Acting as javascript: URI of protocol
Generate undefined

The practice of using void 0 to generate undefined has long been to reduce the time it took to find window.undefined on the prototype chain and to avoid misuse of the modified undefined.

function Checklogin (loginName) {
if (loginName = = void 0) {
Console.log (' wrong! ');
}
}

Checklogin ();
=> ' wrong! '
Execute function now

Operators and functions, so that functions can be executed immediately. In fact, there are many operators that allow functions to execute immediately, such as + 、-、!、 ~ and the void operator mentioned in this article. In addition, there are the most common parentheses (not operators, which change precedence):

(function () {
Console.log (' immediate execution ');
})();
=> ' immediate execution '

Acting as javascript: URI of protocol

For the Web page of the point of Praise, Favorites and other buttons, if the use of a tag to achieve, often used to href= "javascript:void (0)" and other similar code, the role of the code click on the link to make the page does not jump.

In href= "javascript:void (0)", a URI that begins with a javascript: protocol is used, and the browser defaults to the code after the colon, and then displays the result on the new page, except in one case, if the result is undefined, The browser will not refresh the page rendering new values.

In fact, there are a number of ways to achieve the effect of a Hack, a collection that doesn't need to be in such a way-though it's really handy.

JavaScript: The protocol declares that the body of the URL is any JavaScript code that is compiled by the JavaScript interpreter, typically using Javascript:url to execute some code that does not change the current page document, and to do this, you must ensure that the URL's last A statement does not return a value, such as using void 0.

Summarize

Undefined represents the value of a variable declaration but not initialized.
Null indicates that the object is ready to be saved, and that no value is actually saved. From a logical point of view, a null value represents an empty object pointer.

There are 5 basic types of JavaScript (ECMAScript Standard): Undefined, Null, Boolean, number, String, and a complex type object. You can see that null and undefined are different types, and uninitialized values are detected by typeof as "undefined" (strings), while null values are detected by TypeOf as "Object" (String).

It is not recommended to explicitly set a variable to undefined at any time, but it should be set to NULL if the object's variables are not actually saved.
In fact, the undefined value is derived from a null value, and the ECMAScript standard stipulates that the test for equality between the two returns true, that is,
alert (null==undefined); True

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.