Skip to content

Instantly share code, notes, and snippets.

@bhaveshdaswani93
Created December 24, 2019 03:36
Show Gist options
  • Save bhaveshdaswani93/892a5c92c9403e1c092ada74c472dd72 to your computer and use it in GitHub Desktop.
Save bhaveshdaswani93/892a5c92c9403e1c092ada74c472dd72 to your computer and use it in GitHub Desktop.
Understand Referential transparency with example
let a = (num1,num2) => {
return num1+num2;
}
let b = (num) => {
return num*2;
}
console.log(b(a(3,4))) //output will be 14
// here i can replace a(3,4) expression with value 7 value and this will not effect to the result of the program because its return
// value is 7
// so i can replace console.log(b(a(3,4))) to console.log(b(7)) as a function is refrencially transparent
let c = (num1,num2) => {
console.log(`Value of num1:${num1} and value of num2:${num2}`);
return num1+num2;
}
console.log(b(c(3,4))) //output will be 14
// here i cannot replace expression c(3,4) with value 7 as it effect the result of the program
// function c has console.log() which is one type of side effect so it is not referentially transparent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment