Functional Programming
With JavaScript
Deepankar Chopra
Senior Software Engineer
Ticketmaster
» What is Functional Programming?
» Mutations
» Side-Effects
» Pure Functions
» Higher Order Functions
» Composition
» Advantages & Disadvantages of FP
» How does FP differ from Object-Oriented programming
2Overview
What is Functional Programming?
Functional programming (often abbreviated FP) is the process of
building software by
» composing pure functions,
» avoiding
» shared state,
» mutable data
» side-effects.
» Managing state flows through composition
3
Mutations
Mutable is a type of variable that can be changed after it is created.
In JavaScript, only objects and arrays are mutable, not primitive
values.
(You can make a variable name point to a new value, but the
previous value is still held in memory.)
4
Mutations 5
let a = { foo: 'bar' };
let b = a;
a.foo = 'test’;
console.log(a,b);
//{foo: "test"} {foo: "test"}
// avoiding mutation
let a = { foo: 'bar' };
let b = Object.assign({}, a);
a.foo = 'test’;
console.log(a,b);
//{foo: "test"} {foo: "bar"}
Side Effects
If a program / function modifies the state of something else
(outside its own scope), then it is producing a side effect.
This could effectively include something like "display a character on
the screen", or it could be changing the value stored in some
arbitrary RAM location, or anything similar.
6
Side Effects include
» Modifying any external variable or object property (e.g., a global
variable, or a variable in the parent function scope chain)
» Logging to the console
» Writing to the screen
» Writing to a file
» Writing to the network
» Triggering any external process
» Calling any other functions with side-effects
7
Pure Functions
» Must return a value
» Can’t produce any side-effects
» Must return the same output for a given input
8
Pure Functions Example 9
let a = 10;
function impure1(x) {
return x + a;
}
function impure2(x) {
a = 30;
return x + a;
}
console.log(impure1(10));//20
a+=10;
console.log(impure1(10));//30
console.log(impure2(10));//40
function pure(x, y) {
return x + y;
}
console.log(pure(10,10));
Composition
Function Composition is a mathematical concept, by which the
result of one function becomes the input of the next, and so on.
Composing functions together is like putting together a series of
pipes for our data to flow through.
10
Composition Example
function addOne(x) {
return x + 1;
}
function timesTwo(x) {
return x * 2;
}
console.log(addOne(timesTwo(3))); //7
11
Higher Order Functions
A higher-order function is a function that does at least one of the
following:
» takes one or more functions as arguments
» returns a function as its result.
12
Higher Order Functions Example
function greaterThan(n) {
return function(m) { return m > n; };
}
let greaterThan10 = greaterThan(10);
let greaterThan11 = greaterThan(11);
console.log(greaterThan10(11));//true
console.log(greaterThan11(11));//false
13
Advantages of FP
» Pure functions are easier to reason about
» Testing is easier, and pure functions lend themselves well to
techniques like property-based testing
» Debugging is easier
» Programs are more bulletproof
» Programs are written at a higher level, and are therefore easier
to comprehend
» Parallel/concurrent programming is easier
14
Disadvantages of FP
» Not suitable in every situation
» Needs to be clubbed with Object oriented programming in some
cases.
» Makes heavy use of recursion
15
How does FP differ from OOP 16
Functional Programming
» Primary Manipulation Unit is
“Function”
» Stateless Programming Model
» Functions with No-Side Effects
» Order of execution is less
importance
Object Oriented Programming
» Primary Manipulation Unit is
Objects(Instances of Classes)
» Stateful Programming Model
» Methods with Side Effects
» Order of execution is more
important.
Any questions?
You can mail me at
deepankar.chopra@gmail.com
17THANKS!

More Related Content

PDF
03 function overloading
PPTX
Working with functions in matlab
PPTX
functions of C++
PPT
Lecture#6 functions in c++
PPTX
Function overloading
PPTX
C++ programming function
PPT
Function overloading(C++)
03 function overloading
Working with functions in matlab
functions of C++
Lecture#6 functions in c++
Function overloading
C++ programming function
Function overloading(C++)

What's hot (20)

PPTX
Learning C++ - Functions in C++ 3
PPTX
Anonymous and Inline Functions in MATLAB
PPTX
Functions in C++
PPT
Review functions
PPTX
C++ lecture 03
PPT
Functions in C++
PPT
Lecture#7 Call by value and reference in c++
PPTX
User defined Functions in MATLAB Part 1
PPT
C++ Function
PPTX
Call by value or call by reference in C++
PPT
Functions in C++
PPTX
Call by value
PPT
Functions in C++
PPTX
Functions in C
PPT
C++ overloading
PPSX
Functions in c
DOCX
18 dec pointers and scope resolution operator
PPTX
Function C++
PPTX
C and C++ functions
PDF
Functions in C++
Learning C++ - Functions in C++ 3
Anonymous and Inline Functions in MATLAB
Functions in C++
Review functions
C++ lecture 03
Functions in C++
Lecture#7 Call by value and reference in c++
User defined Functions in MATLAB Part 1
C++ Function
Call by value or call by reference in C++
Functions in C++
Call by value
Functions in C++
Functions in C
C++ overloading
Functions in c
18 dec pointers and scope resolution operator
Function C++
C and C++ functions
Functions in C++
Ad

Similar to Functional Programming with Javascript (20)

PDF
Introduction to Functional Programming (w/ JS)
PDF
379008-rc217-functionalprogramming
PDF
Functional JavaScript Fundamentals
ODP
Functional programming
PPTX
Introduction to functional programming with JavaScript
PDF
Introduction to Functional Programming
PPTX
Основы функционального JS
PPTX
Functional Programming in Javascript - IL Tech Talks week
PDF
Introduction to functional programming
PDF
Functional programming 101
PPTX
Thinking Functionally with JavaScript
PPTX
Functional Programming in JavaScript by Luis Atencio
PPTX
Why functional programming in C# & F#
PPTX
When life gives you functions make functional programs!
PPTX
Introduction to Functional Programming
PDF
Functional programming
PPTX
A Skeptics guide to functional style javascript
PPTX
An Introduction to Functional Programming with Javascript
PDF
Christian Gill ''Functional programming for the people''
PDF
Fp for the oo programmer
Introduction to Functional Programming (w/ JS)
379008-rc217-functionalprogramming
Functional JavaScript Fundamentals
Functional programming
Introduction to functional programming with JavaScript
Introduction to Functional Programming
Основы функционального JS
Functional Programming in Javascript - IL Tech Talks week
Introduction to functional programming
Functional programming 101
Thinking Functionally with JavaScript
Functional Programming in JavaScript by Luis Atencio
Why functional programming in C# & F#
When life gives you functions make functional programs!
Introduction to Functional Programming
Functional programming
A Skeptics guide to functional style javascript
An Introduction to Functional Programming with Javascript
Christian Gill ''Functional programming for the people''
Fp for the oo programmer
Ad

Recently uploaded (20)

PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PPTX
Configure Apache Mutual Authentication
PPTX
The various Industrial Revolutions .pptx
PDF
STKI Israel Market Study 2025 version august
PPT
Geologic Time for studying geology for geologist
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PPTX
TEXTILE technology diploma scope and career opportunities
PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
PDF
sustainability-14-14877-v2.pddhzftheheeeee
DOCX
Basics of Cloud Computing - Cloud Ecosystem
PDF
Flame analysis and combustion estimation using large language and vision assi...
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
UiPath Agentic Automation session 1: RPA to Agents
PPTX
Benefits of Physical activity for teenagers.pptx
PPT
What is a Computer? Input Devices /output devices
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PDF
Statistics on Ai - sourced from AIPRM.pdf
OpenACC and Open Hackathons Monthly Highlights July 2025
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
Configure Apache Mutual Authentication
The various Industrial Revolutions .pptx
STKI Israel Market Study 2025 version august
Geologic Time for studying geology for geologist
A review of recent deep learning applications in wood surface defect identifi...
The influence of sentiment analysis in enhancing early warning system model f...
TEXTILE technology diploma scope and career opportunities
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
sustainability-14-14877-v2.pddhzftheheeeee
Basics of Cloud Computing - Cloud Ecosystem
Flame analysis and combustion estimation using large language and vision assi...
Module 1.ppt Iot fundamentals and Architecture
UiPath Agentic Automation session 1: RPA to Agents
Benefits of Physical activity for teenagers.pptx
What is a Computer? Input Devices /output devices
Credit Without Borders: AI and Financial Inclusion in Bangladesh
Statistics on Ai - sourced from AIPRM.pdf

Functional Programming with Javascript

  • 1. Functional Programming With JavaScript Deepankar Chopra Senior Software Engineer Ticketmaster
  • 2. » What is Functional Programming? » Mutations » Side-Effects » Pure Functions » Higher Order Functions » Composition » Advantages & Disadvantages of FP » How does FP differ from Object-Oriented programming 2Overview
  • 3. What is Functional Programming? Functional programming (often abbreviated FP) is the process of building software by » composing pure functions, » avoiding » shared state, » mutable data » side-effects. » Managing state flows through composition 3
  • 4. Mutations Mutable is a type of variable that can be changed after it is created. In JavaScript, only objects and arrays are mutable, not primitive values. (You can make a variable name point to a new value, but the previous value is still held in memory.) 4
  • 5. Mutations 5 let a = { foo: 'bar' }; let b = a; a.foo = 'test’; console.log(a,b); //{foo: "test"} {foo: "test"} // avoiding mutation let a = { foo: 'bar' }; let b = Object.assign({}, a); a.foo = 'test’; console.log(a,b); //{foo: "test"} {foo: "bar"}
  • 6. Side Effects If a program / function modifies the state of something else (outside its own scope), then it is producing a side effect. This could effectively include something like "display a character on the screen", or it could be changing the value stored in some arbitrary RAM location, or anything similar. 6
  • 7. Side Effects include » Modifying any external variable or object property (e.g., a global variable, or a variable in the parent function scope chain) » Logging to the console » Writing to the screen » Writing to a file » Writing to the network » Triggering any external process » Calling any other functions with side-effects 7
  • 8. Pure Functions » Must return a value » Can’t produce any side-effects » Must return the same output for a given input 8
  • 9. Pure Functions Example 9 let a = 10; function impure1(x) { return x + a; } function impure2(x) { a = 30; return x + a; } console.log(impure1(10));//20 a+=10; console.log(impure1(10));//30 console.log(impure2(10));//40 function pure(x, y) { return x + y; } console.log(pure(10,10));
  • 10. Composition Function Composition is a mathematical concept, by which the result of one function becomes the input of the next, and so on. Composing functions together is like putting together a series of pipes for our data to flow through. 10
  • 11. Composition Example function addOne(x) { return x + 1; } function timesTwo(x) { return x * 2; } console.log(addOne(timesTwo(3))); //7 11
  • 12. Higher Order Functions A higher-order function is a function that does at least one of the following: » takes one or more functions as arguments » returns a function as its result. 12
  • 13. Higher Order Functions Example function greaterThan(n) { return function(m) { return m > n; }; } let greaterThan10 = greaterThan(10); let greaterThan11 = greaterThan(11); console.log(greaterThan10(11));//true console.log(greaterThan11(11));//false 13
  • 14. Advantages of FP » Pure functions are easier to reason about » Testing is easier, and pure functions lend themselves well to techniques like property-based testing » Debugging is easier » Programs are more bulletproof » Programs are written at a higher level, and are therefore easier to comprehend » Parallel/concurrent programming is easier 14
  • 15. Disadvantages of FP » Not suitable in every situation » Needs to be clubbed with Object oriented programming in some cases. » Makes heavy use of recursion 15
  • 16. How does FP differ from OOP 16 Functional Programming » Primary Manipulation Unit is “Function” » Stateless Programming Model » Functions with No-Side Effects » Order of execution is less importance Object Oriented Programming » Primary Manipulation Unit is Objects(Instances of Classes) » Stateful Programming Model » Methods with Side Effects » Order of execution is more important.