JavaScript Pure Functions for OOP developers

Martin Novak
Frontend Weekly
Published in
5 min readMar 18, 2019

--

Concepts of functional programming in JavaScript are increasingly more popular. One of the basic concepts relates to pure functions and side effects but maybe it will surprise you that it is connected to some best practices already available in object-oriented programming.

If you need to refresh basic nomenclature, please find 6 fundamental terms in functional JavaScript.

What are pure functions and side effects?

A pure function is a function in which output depends only on its input without side effects.

const myFunction = input => input + 1;

That means that if you run the same function a hundred times in different places of the system it will always produce the same predictable output.

Side effects are anything outside of the function that either influences it or is influenced by it.

let state = 0;const yourFunction = () => state = state + 1;

--

--

Martin Novak
Frontend Weekly

Martin is a product manager at work, a software developer in his free time, and an entrepreneur at heart.