Sep 9, 2021 JavaScript
How to swap two array items in JavaScript

In this Article we will go through how to swap two array items 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 swapItems = (a, i, j) => a[i] && a[j] && [...a.slice(0, i), a[j], ...a.slice(i + 1, j), a[i], ...a.slice(j + 1)] || a;

Sep 9, 2021 JavaScript
How to unzip an array of arrays in JavaScript

In this Article we will go through how to unzip an array of arrays 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 unzip = arr => arr.reduce((acc, c) => (c.forEach((v, i) => acc[i].push(v)), acc), Array.from({ length: Math.max(...arr.map(a => a.length)) }, (_) => []));

Sep 9, 2021 JavaScript
How to zip multiple arrays in JavaScript

In this Article we will go through how to zip multiple arrays 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 zip = (...arr) => Array.from({ length: Math.max(...arr.map(a => a.length)) }, (_, i) => arr.map(a => a[i]));

Sep 9, 2021 JavaScript
How to add am pm suffix to an hour in JavaScript

In this Article we will go through how to add am pm suffix to an hour 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 suffixAmPm = h => `${h % 12 === 0 ? 12 : h % 12}${h < 12 ? 'am' : 'pm'}`;

Sep 9, 2021 JavaScript
How to calculate the number of difference days between two dates in JavaScript

In this Article we will go through how to calculate the number of difference days between two dates 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 diffDays = (date, otherDate) => Math.ceil(Math.abs(date - otherDate) / (1000 * 60 * 60 * 24));

Sep 9, 2021 JavaScript
How to calculate the number of months between two dates in JavaScript

In this Article we will go through how to calculate the number of months between two dates 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 monthDiff = (startDate, endDate) => Math.max(0, (endDate.getFullYear() - startDate.getFullYear()) * 12 - startDate.getMonth() + endDate.getMonth());

Sep 9, 2021 JavaScript
How to compare two dates in JavaScript

In this Article we will go through how to compare two dates 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 compare = (a, b) => a.getTime() > b.getTime();

Sep 9, 2021 JavaScript
How to convert a date to yyyy-mm-dd format in JavaScript

In this Article we will go through how to convert a date to yyyy-mm-dd format 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 formatYmd = date => date.toISOString().slice(0, 10);

Sep 9, 2021 JavaScript
How to convert seconds to hh:mm:ss format in JavaScript

In this Article we will go through how to convert seconds to hh:mm:ss format 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 formatSeconds = s => new Date(s * 1000).toISOString().substr(11, 8);

Sep 9, 2021 JavaScript
How to extract year, month, day, hour, minute, second and millisecond from a date in JavaScript

In this Article we will go through how to extract year, month, day, hour, minute, second and millisecond from a date 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 extract = date => date.toISOString().split(/[^0-9]/).slice(0, -1);