javascript-void keyword

Source: Internet
Author: User

javascript-void keyword written in front

ECMA-262 defines the keyword (keyword) supported by ECMAScript, and the keyword cannot be used as an identifier for the ECMAScript program (indetifiers). The ECMA-262 standard defines the following keywords:

 Break  Do instanceof typeof Case Else New varCatch finally return voidContinue  for Switch  whileDebugger function  This  withdefault if Throw Deleteinch Try

void

According to the ECMA-262 definition, void is one of 26 keywords in JavaScript, so what is void in JavaScript and what is different from void in other languages? As defined by the ECMA official standard, Void is a unary operator and returns undefined, regardless of what is behind the expression.

The following are described in the ECMA-262 specification:

Void can be any expression (unaryexpression), with a return value of only undefined:

Note: Do not write in this form void 1 + 1, because the void ratio + priority is higher, then it becomes (void 1) + 1

Basic usage 1, void 0

   

function joke () {    var undefined = "Hello world";     // will output "Hello World" }varfunction  (key) {    returnfunction  (obj) {        returnnullvoid 0 : Obj[key];    };};
2, javascript:void (0)

You can use Javascript:void (0) to block links from the default jump behavior, and here is an example of an MDN document:

 do nothing</a><a href= "javascript:void (Alert (' Hello word! '))" >click me!</a>

Note: Regardless of how many usages of void (unaryexpression) Extend, its essence is to perform unaryexpression and return undefined.

Questions

back to the official standard definition of void ECAM-262, since the return value of void is always undefined, then why do you need to call Getvale (expr). In fact, Void is designed to implement the expression behind the execution and ignore the expression results returned undefined, if the result of an expression is a reference, and does not take value, and the value of this process may have side effects (side-effect), So take one more getvalue step.

Reference ECMAScript 5.1MDN

javascript-void keyword

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.