InterviewHubPRO
Section Overview
Q451

Question 451 of 521

Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

87%
JavaScript Internals (521 Qs)•Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

What are the possible side-effects in javascript?

A side effect is the modification of the state through the invocation of a function or expression. These side effects make our function impure by default. Below are some side effects which make function impure,

  • Making an HTTP request. Asynchronous functions such as fetch and promise are impure.
  • DOM manipulations
  • Mutating the input data
  • Printing to a screen or console: For example, console.log() and alert()
  • Fetching the current time
  • Math.random() calls: Modifies the internal state of Math object

Practical Code Example

javascript
// Common Side-effects in JavaScript:
// 1. Mutating an input parameter
function mutateUser(user) { user.updated = true; }

// 2. Making an API/Network call
// 3. Writing to DOM or console.log
// 4. Writing to localStorage or cookies