Functional JavaScript First Steps, v2

Pure Functions Overview

Anjana Vakil
Software Engineer & Educator
Functional JavaScript First Steps, v2

Lesson Description

The "Pure Functions Overview" Lesson is part of the full, Functional JavaScript First Steps, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Anjana introduces pure functions. A function is considered pure if the return values are identical for identical arguments and the function has no side effects like mutation of local static variables, non-local variables, or references to outside dependencies.

Preview
Close

Transcript from the "Pure Functions Overview" Lesson

[00:00:00]
>> Anjana Vakil: Functional programming boils down to this one idea, pure functions, so when I say a pure function, what am I talking about? A pure function, one way to think about it is something that like all functions that we're used to writing, it takes inputs and it returns output.

[00:00:17]
The difference between a regular function option of the kind that we're used to working with every day in JavaScript and a pure function. Is that in a pure function, all that this function looks at in order to decide what its computation is gonna be, are the inputs that it receives.

[00:00:35]
So it only takes its inputs in, it doesn't take in any other information about the state of the world, the weather today, the time it is, etc, only its input arguments. And then all that it does is do whatever computation it needs to do and return its output.

[00:00:55]
That is, it doesn't do anything else, and we'll talk about what I need, anything else might be in a moment. So a pure function is essentially a transformation from inputs to outputs, period, done. So when we think about functions in JavaScript, we usually think about all kinds of other stuff that we can do, like talk to the browser, or make Fetch requests or write something to files or what have you.

[00:01:23]
Essentially talk to the rest of the world from within the function. In functional programming, that is not allowed. All the function looks at is its inputs, and all that it does is give it an output, and that's it. It doesn't look at any of the rest of the world, and it doesn't have any effect on the rest of the world, its only effect is its return value.

[00:01:45]
So for example, we have a very simple function here called greet, where we just have one line and it's a console log. So can anybody help me break down what is not pure about this Greek function on the left side, yeah.
>> Speaker 2: Well, it's using name that is not coming in as an argument, it's just using it from the higher scope.

[00:02:16]
>> Anjana Vakil: Exactly, so one thing that it's doing is it's looking at data in the outside world, like in this case, the value of the variable name, that is not part of its inputs. So that's not allowed, great. What about anything else? Yeah?
>> Speaker 3: It's logging to the console, which is not a pure output.

[00:02:36]
>> Anjana Vakil: Yes, exactly, logging to the console is not the same as, is returning a value, what does this function return?
>> Speaker 4: Void.
>> Anjana Vakil: Yeah, undefined, right?. It doesn't have a explicit return, and in JavaScript, that means it returns undefined. So if it's not returning its output value, and it's a pure, if it's not returning an output value, it definitely can't be a pure function, because it's doing something else, right.

[00:03:03]
And that means that if something changes for example, the value of that name, and I run the function again with the exact same inputs, in this case, no inputs, I might get a different value. It also means that I don't know the side effect of vlogging to the console that might work differently in different situations, different environments.

[00:03:23]
And so we might get a different side effect different times that I run this function. Now, a pure way of structuring this would be to make name an input to the function, so that way, we're only looking at that data in order to make our computation. And then to return the output instead of logging it to the console.

[00:03:45]
So this is a very, very simple, but I hope illustrative example of what we want to not do [LAUGH] In our functional programs.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Start a 7-Day Free Trial