Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JavaScript, what does the term 'side-effect' refer to?

Tags:

javascript

Does it mean, exclusively, that a variable is being written?

like image 399
ppecher Avatar asked Dec 02 '22 01:12

ppecher


1 Answers

This isn't a formally defined term in JavaScript, but I see it most commonly used to refer to some change in state outside of the immediate context. For example, the following code will cause no changes in state after execution, so it would be considered "side-effect free":

(function() {
    // no side-effects, foo won't exist once this function is done executing
    var foo = 'bar';
})();

... whereas in the following code there are side-effects, because a global variable is introduced:

(function() {
    // no var keyword, so global variable created
    foo = 'bar';
})();
like image 188
jmar777 Avatar answered Dec 10 '22 12:12

jmar777