Skip to content

Latest commit

 

History

History
27 lines (18 loc) · 689 Bytes

what_is_referential_transparency_.md

File metadata and controls

27 lines (18 loc) · 689 Bytes

What is referential transparency?

A function is referentially transparent if it can be replaced with its output value without changing the program's behavior. It is a key property of pure functions.

Example:

// Referentially Transparent
function add(a, b) {
  return a + b;
}

const result = add(2, 3); // Can replace 'add(2, 3)' with 5
console.log(result); // 5

// Not Referentially Transparent
let count = 0;
function increment() {
  return ++count;
}

Tags: advanced, JavaScript, Functional Programming