Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Advanced JavaScript

You're reading from   Advanced JavaScript Speed up web development with the powerful features and benefits of JavaScript

Arrow left icon
Product type Book
Published in Jan 2019
Publisher
ISBN-13 9781789800104
Pages 330 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Zachary Shute Zachary Shute
Author Profile Icon Zachary Shute
Zachary Shute
Arrow right icon
View More author details
Toc

Pure Functions


Pure functions are a key component of Functional Programming. A pure function can be defined as a function that does not have any effect on or make use of any state outside of the function. A function must meet three key criteria to be considered pure:

  • A function must always return the same output, when given the same inputs.

  • A function must have no side effects.

  • A function must have referential transparency.

Same Output Given Same Input

Given a set of input values, a pure function must always return the same value when provided those input values. This sounds much more complicated than it is. Simply put, the output of a pure function cannot change unless the input values are changed. This means that a function's internal code cannot depend on any program state outside of the function. A pure function cannot use any variable from outside the function to make calculations or code path decisions. An example of this is shown in the following snippet:

const state = { prop1: 5 }...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime