I'm so confused about how Void
operator (in javascript) works and what is it's role. as i read, Void
is an operator that evaluate an expression without returning a value, but by executing that script for example:
<html>
<head>
</head>
<body>
<a href="javascript:void(alert(1+1))">Click me!</a>
</body>
</html>
After clicking on the link, the browser shows the value 2, which mustn't be shown since the evaluated expression (alert(1+1)
) shouldn't return a value due to being an operand of the Void
operator.
Can someone clear it up for me please?.
void
method says to ignore any returned value from the inner expression (e.g.,alert(1+1)
). That doesn't stop it from being processed. – Adam Zuckerman Jun 29 at 0:11void
as the return type only prevents something from being returned from the Javascript function; it doesn't prevent thealert
expression from being evaluated. – Robert Harvey Jun 29 at 1:29alert(1+1)
returns. – Robert Harvey Jun 29 at 3:00Window.alert()
is a method. It doesn't return anything. – Robert Harvey Jun 29 at 15:06