Meaning of void (0) in JavaScript

Source: Internet
Author: User

meaning of void (0) in JavaScript

Overview

The void operator executes the statement passed to it, and then returns undefined.

Grammar

void expression

The void operator is usually used only to get The original value of the undefined, usually with void (0) (equivalent to void 0 ).

Why don't we just use undefined ?

Undefined is not a reserved word ( A global variable in some environments of JavaScript). This means that undefined is an available variable name and you can assign it any value. For example:

// alerts "undefined" var undefined = "New value"//Alerts "New Value "

Let's look at an example used in a link:

Javascript:uri executes the code in the URI and replaces the current page content with the returned value, unless the return value is undefined.

 <  a  href  = "javascript:void (0);"  >  //TAP no action  </ a  >  <  a  href  = "javascript:void (document.body.style.backgroundcolor= ' green ');"  >  //Click the background to turn green  </ a  >  

Therefore, you cannot safely use undefined, and the value it returns is not necessarily what you expected. void is not overwritten and is always returned to undefined.

Why is void 0, why is 0 others not?

Other values are also available, such as 1,"Hello World". The use of 0 is because it is relatively short and concise and conforms to the language habit.

Reference:

MDN void Opter

Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void

StackOverflow 's Answer

What does ' void 0 ' mean?

Http://stackoverflow.com/questions/7452341/what-does-void-0-mean

Meaning of void (0) 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.