javascript:void is a usage format of the void operator in Javascript; the void operator specifies to calculate an expression but does not return a value; developers can use void to prevent a tag from jumping when clicked , you can also use void0 to take undefined.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
void in Javascript is an operator that specifies to evaluate an expression but not return a value. You can use void to prevent the a tag from jumping when clicked. You can also use void0 to take undefined.
void Operator Usage
javascript:void (expression) javascript:void expression
expression is a Javascript standard expression to be evaluated. The parentheses outside the expression are optional, but are a good practice to write.
You can use the void operator to specify a hyperlink. The expression will be evaluated but nothing will be loaded into the current document. The above code creates a hyperlink and nothing happens to the user later. Nothing happens on Javascript when the user links.
Description in the ECMAScript 262 specification
The void Operator The production UnaryExpression : void UnaryExpression is evaluated as follows: Let expr be the result of evaluating UnaryExpression. Call GetValue(expr). Return undefined. NOTE: GetValue must be called even though its value is not used because it may have observable side-effects.
It can be understood from the specification that no matter what the expression after void is, the void operator will return undefined
[Recommended learning: js basic tutorial]
Why use void
undefined is not a reserved word or keyword in Javascript
function isUndefined(param) { let undefined = 'hello world'; return undefined === param; }
We can define a variable called undefined in the context of a function. At this time, if we want to access the real undefined, we can only access undefined through the global scope.
window.undefined; GLOBAL.undefined;
Unfortunately, window and GLOBAL can still be defined in a function context, so taking undefined from them is not a safe and reliable method
function test() { let undefined = 'hello world', window = { undefined: 'joke' }, f = {} ; console.log(undefined); console.log(window.undefined); console.log(f.a === undefined); console.log(f.a === void 0); }
Judge undefined
function isUndefined(param) { return param === void 0; } 另外一种方式 function getUndefined() { return; }; function isUndefined(param) { return param === getUndefined(); }
This method is feasible because a function that does not specify a return value will By default, undefined is returned. The disadvantage is that in order to determine undefined, a function must be declared, which will cause a loss in performance.
Priority
void Priority is second only to. [] ()
Summary:
It is safer to use void 0 to get undefined than to use literal undefined. Void 0 should be used first.
Fill in the href of to ensure that no page jump occurs when clicked. Avoid adding click events to the a tag and use return false to organize the default behavior.
The above is the detailed content of What does javascript:void mean?. For more information, please follow other related articles on the PHP Chinese website!