Sep 9, 2021 JavaScript
How to flip the arguments of a function in JavaScript

In this Article we will go through how to flip the arguments of a function only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

// Reverse the order of arguments
const flip = fn => (...args) => fn(...args.reverse());

// For binary functions
const flip = fn => (b, a) => fn(a, b);

Sep 9, 2021 JavaScript
How to identity function in JavaScript

In this Article we will go through how to identity function only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const identity = x => x;

Sep 9, 2021 JavaScript
How to logical xor operator in JavaScript

In this Article we will go through how to logical xor operator only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

// returns `true` if one of the arguments is truthy and the other is falsy

const xor = (a, b) => (a && !b) || (!a && b);

Sep 9, 2021 JavaScript
How to memoize a function in JavaScript

In this Article we will go through how to memoize a function only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const memoize = fn => ((cache = {}) => arg => cache[arg] || (cache[arg] = fn(arg)))();

Sep 9, 2021 JavaScript
How to partially apply a function in JavaScript

In this Article we will go through how to partially apply a function only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const partial = (fn, ...a) => (...b) => fn(...a, ...b);

Sep 9, 2021 JavaScript
How to uncurry a function in JavaScript

In this Article we will go through how to uncurry a function only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const uncurry = (fn, n = 1) => (...args) => (acc => args => args.reduce((x, y) => x(y), acc))(fn)(args.slice(0, n));

Sep 9, 2021 JavaScript
How to calculate the angle of a line defined by two points in JavaScript

In this Article we will go through how to calculate the angle of a line defined by two points only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

// In radians
const radiansAngle = (p1, p2) => Math.atan2(p2.y - p1.y, p2.x - p1.x);

// In degrees
const degreesAngle = (p1, p2) => Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI;

Sep 9, 2021 JavaScript
How to calculate the distance between two points in JavaScript

In this Article we will go through how to calculate the distance between two points only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const distance = (p1, p2) => Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));

Sep 9, 2021 JavaScript
How to calculate the linear interpolation between two numbers in JavaScript

In this Article we will go through how to calculate the linear interpolation between two numbers only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const lerp = (a, b, amount) => (1 - amount) * a + amount * b;

Sep 9, 2021 JavaScript
How to calculate the midpoint between two points in JavaScript

In this Article we will go through how to calculate the midpoint between two points only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const midpoint = (p1, p2) => [(p1.x + p2.x) / 2, (p1.y + p2.y) / 2];