Sep 9, 2021 JavaScript
How to check if a value is base64 encoded in JavaScript

In this Article we will go through how to check if a value is base64 encoded 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 isBase64 = value => /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value);

Sep 9, 2021 JavaScript
How to check if all array elements are equal to a given value in JavaScript

In this Article we will go through how to check if all array elements are equal to a given value 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 isEqual = (arr, value) => arr.every(item => item === value);

Sep 9, 2021 JavaScript
How to check if an array contains a value matching some criterias in JavaScript

In this Article we will go through how to check if an array contains a value matching some criterias 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 contains = (arr, criteria) => arr.some(v => criteria(v));

Sep 9, 2021 JavaScript
How to check if an object is an array in JavaScript

In this Article we will go through how to check if an object is an array 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 isArray = obj => Array.isArray(obj);

Sep 9, 2021 JavaScript
How to check if an object is empty in JavaScript

In this Article we will go through how to check if an object is empty 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 isEmpty = obj => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object;

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 get the first date in the month of a date in JavaScript

In this Article we will go through how to get the first date in the month of 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 getFirstDate = (d = new Date()) => new Date(d.getFullYear(), d.getMonth(), 1);

Sep 9, 2021 JavaScript
How to get the last date in the month of a date in JavaScript

In this Article we will go through how to get the last date in the month of 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 getLastDate = (d = new Date()) => new Date(d.getFullYear(), d.getMonth() + 1, 0);

Sep 9, 2021 JavaScript
How to get the number of days in given month in JavaScript

In this Article we will go through how to get the number of days in given month 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 daysInMonth = (month, year) => new Date(year, month, 0).getDate();

Sep 9, 2021 JavaScript
How to get the weekday of a date in JavaScript

In this Article we will go through how to get the weekday of 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 getWeekday = date => ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];