Referential Transparency in JavaScript

Introduction

In JavaScript, functional programming is a model in which we create functions. That are work out its logic by reliant only on its input. This confirms that a function, when called many times, is successful to return the similar result. The function likewise won’t change any data in the external world, important to cachable and testable codebase.

As all the functions return the similar value for the same input. So, this property of a function is named a Referential transparency. In this post, we will talk about Referential transparency in details.

Description

The referential transparency is used in many domains for example mathematics, logic, linguistics, philosophy and programming. It has quite changed meanings in each of these domains.

In mathematics, referential transparency is the stuff of expressions. That may be substituted by other expressions. Those have the similar value deprived of changing the result in any way.

Referential transparency in programming relates to programs. By way of programs are composed of subprograms that are programs themselves. It relates to those subprograms, also. Subprograms can be signified by methods.

For better understanding the referential transparency see the below example.

var identity = (i) => { return i }
  • We have defined a simple function named identity in the above code snippet.
  • This function return whatsoever we‘re passing as its input.
  • That is, if we passes 10, it returns back the value 10.
  • As the function is only acts as a mirror and identity.
  • Our function does work only on the incoming argument ‘i’.
  • There is no global reference inside our function.

Substitution model

  • At the present conceive this function is used between other function calls like this:
sum(4,5) + identity(1)
  • By means of our Referential Transparency definition we may change the above statement into this:
sum(4,5) + 1
  • At this moment this process is called a Substitution model.
  • By way of we can directly substitute the result of the function as is with its value.
  • Generally due to the function doesn’t rely on other global variables for its logic.
  • This leads to equivalent code and caching.
  • We may easily run the above function with several threads deprived of even the need of synchronizing.
  • The motive for synchronizing comes from the detail that threads shouldn’t do global data when running equivalent.
  • Functions that follow Referential Transparency depends only on inputs from its argument.
  • Therefore, threads are allowed to run without any locking mechanism.
  • Meanwhile the function returns the same value for the given input.
  • We may, actually cache it. For instance, see there is a function called factorial.
  • Calculates the factorial of the given number.
  • Factorial takes the input as its argument for which the factorial needs to be calculated.
  • We all recognize the factorial of 5 going to be 120.
  • What would be if the user calls the factorial of 5 a second time?
  • We see that the result is to be 120 if the factorial function follows Referential transparency as before.
  • It only be contingent on the input argument.
  • We may cache the values of our factorial function by keeping in mind these characters.
  • Thus if a factorial is called for the second time with the input as ‘5’, we can return the cached value instead of calculating once again.