I have trouble understanding this paragraph in the page https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/void:
This operator allows inserting expressions that produce side effects into places where an expression that evaluates to undefined is desired.
What exactly are expressions that produce side effects ?
those expressions that, besides calculating a value, correspond to an operation on memory, such as an assignment (simple or combined) or an increment. We call these expressions-with-side-effect. Recall that we use the term side-effect to indicate a modification of the state (i.e., the memory) of the program.
A side effect is when a function relies on, or modifies, something outside its parameters to do something. For example, a function which reads or writes from a variable outside its own arguments, a database, a file, or the console can be described as having side effects.
From Wikipedia: "In computer science, a function or expression is said to have a side effect if, in addition to returning a value, it also modifies some state or has an observable interaction with calling functions or the outside world." – user40980. Jul 3, 2015 at 13:53.
Reading an object designated by a volatile glvalue (3.10), modifying an object, calling a library I/O function, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment.
A function does two things typically: Perform something and return a value. Some functions only do one of those things, some do both. For instance the function:
function square(x) {
return x * x;
}
Is side-effect free since all it does is return a value and its invocation can always replaced by its return value. On the other hand, something like alert()
is only called for its side-effects (alerting a user) and never for its return value.
So what void
operator does is it makes JavaScript ignore the return value and state that all you're interested is the function's side-effects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With