Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly are expressions that produce *side effects*?

Tags:

javascript

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 ?

like image 961
Li Juan Avatar asked Jul 01 '11 01:07

Li Juan


People also ask

What expression is side effect?

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.

What are functions with side effects?

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.

When we say that a function produces a side effect that usually means?

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.

What operations in C++ have side effects?

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.


1 Answers

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.

like image 135
yan Avatar answered Sep 20 '22 06:09

yan