ES6 Fundamentals: Functional Programming

Hiten Pratap Singh
TechCret Software
Published in
3 min readMay 21, 2020

JavaScript has always been a functional programming language. ES6 added new features to it like Arrow functions, Iterator etc. We will look into these new features in this blog post in details.

ES6 Functional Programming

Arrow Functions

Arrow functions provides us a way to write functions in JavaScript in a concise and compact way. Arrow functions — also called “fat arrow” functions, from CoffeeScript (a transcompiled language) — are a more concise syntax for writing function expressions. They utilize a new token, =>, that looks like a fat arrow.

Arrow Functions Example

Iterators

In JavaScript an iterator is an object which defines a sequence and potentially a return value upon its termination. Specifically, an iterator is any object which implements the Iterator protocol by having a next() method that returns an object with two properties: value and done. We can get the values from a collection by calling next() repeatedly and get to know that it’s the end of the collection when we get true in done property.

Iterator Example

for of

JavaScript has always had a for in construct that we could use for loop through over an array. for in works by enumerating the properties of an object. It means for in iterate over an array because it works with the available indexes inside an array. Sometimes we just want to iterate over values and not to worry about keys or indexes. It’s where for of comes very handy to accomplish the said task.

for of example

Generator Function

A Generator Function is a function which generates an Iterator. A Generator Function doesn’t require lots of code but it does require a new keyword which is called yield keyword and a special function syntax. Generator functions are written using the function* syntax. When called, generator functions do not initially execute their code. Instead, they return a special type of iterator, called a Generator. When a value is consumed by calling the generator’s next method, the Generator function executes until it encounters the yield keyword. The function can be called as many times as desired, and returns a new Generator each time. Each Generator may only be iterated once.

Generator Function Example

Well, it marks the end of this post in the ES6 blog series.

You can find the previous posts here:

  1. ES6 Fundamentals: Introduction
  2. ES6 Fundamentals: Variables and Parameters
  3. ES6 Fundamentals: Classes

--

--