Sep 9, 2021 JavaScript
How to serialize form data in JavaScript

In this Article we will go through how to serialize form data 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 serialize = formEle => Array.from(new FormData(formEle)).reduce((p, [k, v]) => Object.assign({}, p, { [k]: p[k] ? (Array.isArray(p[k]) ? p[k] : [p[k]]).concat(v) : v}), {});

Sep 9, 2021 JavaScript
How to strip html from a given text in JavaScript

In this Article we will go through how to strip html from a given text 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 stripHtml = html => (new DOMParser().parseFromString(html, 'text/html')).body.textContent || '';

Sep 9, 2021 JavaScript
How to toggle an element in JavaScript

In this Article we will go through how to toggle an element 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 toggle = ele => (ele.style.display = (ele.style.display === 'none') ? 'block' : 'none');

Sep 9, 2021 JavaScript
How to get the position of an element relative to the document in JavaScript

In this Article we will go through how to get the position of an element relative to the document 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 getPosition = ele => (r = ele.getBoundingClientRect(), { left: r.left + window.scrollX, top: r.top + window.scrollY });

Sep 9, 2021 JavaScript
How to reload the current page in JavaScript

In this Article we will go through how to reload the current page 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 reload = () => location.reload();

Sep 9, 2021 JavaScript
How to scroll to top of the page in JavaScript

In this Article we will go through how to scroll to top of the page 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 goToTop = () => window.scrollTo(0, 0);

Sep 9, 2021 JavaScript
How to create an object from the pairs of key and value in JavaScript

In this Article we will go through how to create an object from the pairs of key and 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 toObj = arr => Object.fromEntries(arr)

Sep 9, 2021 JavaScript
How to extract values of a property from an array of objects in JavaScript

In this Article we will go through how to extract values of a property from an array of objects 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 pluck = (objs, property) => objs.map(obj => obj[property]);

Sep 9, 2021 JavaScript
How to invert keys and values of an object in JavaScript

In this Article we will go through how to invert keys and values of an object 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 invert = obj => Object.keys(obj).reduce((res, k) => Object.assign(res, {[obj[k]]: k}), {});

Sep 9, 2021 JavaScript
How to shallow copy an object in JavaScript

In this Article we will go through how to shallow copy an object 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 shallowCopy = obj => Object.assign({}, obj);