Damian Łabas
Frontend Developer
Functional Programming in JS
Numer telefonu:
+48 788 266 229
Adres e-mail:
damian.labas@codibly.com
Meetup: JS dla zaawansowanych!
Autor: Damian Łabas
Functional Programming in JS
www.codibly.com
JavaScript
Procedural Programming
Object-Oriented Programming
Functional Programming
4
What is Functional Programming?
Imperative vs Declarative
In Imperative Programming, you write down how to do it.
In Declarative Programming, you write down what to do it.
6
What is in JS?
• First class functions
• Anonymous functions
• Closures
www.codibly.com
What is missing in JS?
• Recursion
• Immutability
• Purity
www.codibly.com
9
First class function
• They can by passed around
• They can be assigned to variables
• They can be stored in more complex data
structure, like array or objects
www.codibly.com
11
Closures
Closures - example
www.codibly.com
function grandParent (g1, g2) {
const g3 = 3;
return function parent (p1, p2) {
const p3 = 33;
return function child(c1, c2) {
const c3 = 333;
return g1 + g2 + g3 + p1 + p2 + p3 + c1 + c2 + c3
}
}
}
Closures - usage
www.codibly.com
const parentFunc = grandParent(1, 2); // return parent()
const childFunc = parentFunc(11, 22); // return child()
console.log(childFunc(111, 222)); // return 738
14
Recursion
Purity
Pure function - examples
www.codibly.com
function coin () {
return Math.random() < 0.5 ? 'heads' : 'tails'
}
let firstName = 'Damian';
function uppercaseName (lastName) {
return `${firstName.toUpperCase()} ${lastName.toUpperCase()}`
}
function calculatePrice (unitPrce, noOfUnits, couponValue = 0) {
return unitPrce * noOfUnits - couponValue;
}
17
Why pure function are better than impure?
Advantages of pure functions
• They are easier to read
• They are easier to test
• They can by more performant
www.codibly.com
19
20
Side Effects
• Modifying any external variable or object property
• Logging to the console
• Writing to file
• Call to API
www.codibly.com
22
Immutability
Immutability in JS
www.codibly.com
const freezeObject = Object.freeze({
foo: 'Hello',
bar: 'world',
baz: '!'
});
const freezeObject = Object.freeze({
foo: { greeting: 'Hello' },
bar: 'world',
baz: '!'
});
24
Higher-order Functions
Higher-order Functions either take functions as parameters, return functions
or both
Higher-order Function
• Abstract or isolate actions
• Async flow control using callback functions
• Promises
• Create utilities which can act on a wide variety
of data types
www.codibly.com
Higher-order Function - own map implementation
www.codibly.com
const arr1 = [1, 2, 3];
function mapForEach (arr, fn) {
const newArray = [];
for (let i = 0; i < arr.length; i++) {
newArray.push(fn(arr[i]))
}
return newArray;
}
const result = mapForEach(arr1,function (item) {
return item * 2
});
27
Function composition
Function composition - example
www.codibly.com
const users = [
{name: 'Damian', age: 23},
{name: 'Andrzej', age: 14},
{name: 'Dominika', age: 18}
];
const filter = (fn, arr) => arr.filter(fn);
const map = (fn, arr) => arr.map(fn);
map(user => user.name, filter(user => user.age >= 18)); // ['Damian,
'Dominika']
Higher-order function - compose and pipe
www.codibly.com
const compose = (...functions) => data =>
functions.reduceRight((value, fn) => fn(value), data);
const pipe = (...functions) => data =>
functions.reduce((value, fn) => fn(value), data);
30
Currying
Currying transforms a function into a sequence of functions each taking a
single argument of the function
Currying - example
www.codibly.com
function multiply (a, b, c) {
return a * b * c
}
function multiply (a) {
return function (b) {
return function (c) {
return a * b * c;
}
}
}
What is a partial application
or partial application function
33
What is a difference?
Is currying useful?
Partial application and currying - example
www.codibly.com
function discount (price, discount) {
return price * discount
}
function discount (discount) {
return function (price) {
return price * discount;
}
}
const tenPercentDiscunt = discount(0.1);
36
Questions?
Thank you
for your time!
www.codibly.com
/in/codibly
/codibly
/codibly
/codibly

More Related Content

PDF
Functional programming
PDF
Intro to functional programming
PDF
Kleisli Composition
PDF
07 java collection
PDF
JavaScript guide 2020 Learn JavaScript
PPT
Advanced Javascript
PPTX
Jsp Introduction Tutorial
PPTX
JavaScript Basic
Functional programming
Intro to functional programming
Kleisli Composition
07 java collection
JavaScript guide 2020 Learn JavaScript
Advanced Javascript
Jsp Introduction Tutorial
JavaScript Basic

What's hot (20)

PDF
Left and Right Folds - Comparison of a mathematical definition and a programm...
PPTX
Javascript functions
PPTX
JS Event Loop
PPTX
React Hooks
PPT
JavaScript & Dom Manipulation
PDF
Important React Hooks
PPT
JavaScript - An Introduction
PDF
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
PDF
An introduction to React.js
PDF
Swift Programming Language
PPTX
Javascript 101
PDF
ES6 presentation
PPTX
React state
PDF
Introduction into ES6 JavaScript.
PPTX
PHP FUNCTIONS
PPT
Javascript built in String Functions
ODP
Datatype in JavaScript
PDF
Introduction to thymeleaf
PPTX
React.js - The Dawn of Virtual DOM
PDF
Javascript essentials
Left and Right Folds - Comparison of a mathematical definition and a programm...
Javascript functions
JS Event Loop
React Hooks
JavaScript & Dom Manipulation
Important React Hooks
JavaScript - An Introduction
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
An introduction to React.js
Swift Programming Language
Javascript 101
ES6 presentation
React state
Introduction into ES6 JavaScript.
PHP FUNCTIONS
Javascript built in String Functions
Datatype in JavaScript
Introduction to thymeleaf
React.js - The Dawn of Virtual DOM
Javascript essentials
Ad

Similar to Functional Programming In JS (20)

PPTX
Getting Started with MongoDB and NodeJS
PPT
17javascript.ppt17javascript.ppt17javascript.ppt
PPT
Java Script Programming on Web Application
PPT
17javascript.ppt
PPT
17javascript.ppt
PPTX
Java Script Overview
PPTX
Things about Functional JavaScript
PPTX
Building High Perf Web Apps - IE8 Firestarter
KEY
Exciting JavaScript - Part I
KEY
JavaScript Growing Up
PDF
Functional Web Development
KEY
Exciting JavaScript - Part II
PPTX
Single Page Applications with AngularJS 2.0
PPTX
Functional programming with Immutable .JS
PDF
"JS: the right way" by Mykyta Semenistyi
PDF
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
PPTX
Advanced JavaScript
PDF
HTML5 for the Silverlight Guy
PPTX
ECMAScript 6 and the Node Driver
PPTX
Intro to Javascript
Getting Started with MongoDB and NodeJS
17javascript.ppt17javascript.ppt17javascript.ppt
Java Script Programming on Web Application
17javascript.ppt
17javascript.ppt
Java Script Overview
Things about Functional JavaScript
Building High Perf Web Apps - IE8 Firestarter
Exciting JavaScript - Part I
JavaScript Growing Up
Functional Web Development
Exciting JavaScript - Part II
Single Page Applications with AngularJS 2.0
Functional programming with Immutable .JS
"JS: the right way" by Mykyta Semenistyi
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
Advanced JavaScript
HTML5 for the Silverlight Guy
ECMAScript 6 and the Node Driver
Intro to Javascript
Ad

Recently uploaded (20)

PPT
What is a Computer? Input Devices /output devices
PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
DOCX
Basics of Cloud Computing - Cloud Ecosystem
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
STKI Israel Market Study 2025 version august
PDF
UiPath Agentic Automation session 1: RPA to Agents
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
PPTX
TEXTILE technology diploma scope and career opportunities
PDF
Flame analysis and combustion estimation using large language and vision assi...
PPTX
Modernising the Digital Integration Hub
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
PDF
Enhancing plagiarism detection using data pre-processing and machine learning...
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
DOCX
search engine optimization ppt fir known well about this
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
Consumable AI The What, Why & How for Small Teams.pdf
PPTX
Build Your First AI Agent with UiPath.pptx
What is a Computer? Input Devices /output devices
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
Basics of Cloud Computing - Cloud Ecosystem
A contest of sentiment analysis: k-nearest neighbor versus neural network
STKI Israel Market Study 2025 version august
UiPath Agentic Automation session 1: RPA to Agents
The influence of sentiment analysis in enhancing early warning system model f...
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
TEXTILE technology diploma scope and career opportunities
Flame analysis and combustion estimation using large language and vision assi...
Modernising the Digital Integration Hub
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
Enhancing plagiarism detection using data pre-processing and machine learning...
Zenith AI: Advanced Artificial Intelligence
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
search engine optimization ppt fir known well about this
Benefits of Physical activity for teenagers.pptx
Consumable AI The What, Why & How for Small Teams.pdf
Build Your First AI Agent with UiPath.pptx

Functional Programming In JS