Categories
JavaScript Answers

How to convert a DOM node list to an array in JavaScript?

Sometimes, we want to convert a DOM node list to an array in JavaScript.

In this article, we’ll look at how to convert a DOM node list to an array in JavaScript.

How to convert a DOM node list to an array in JavaScript?

To convert a DOM node list to an array in JavaScript, we can use the spread operator or the Array.from method.

For instance, we write

const elements = [...nodeList];

or

const elements = Array.from(nodeList);

to use the spread operator or the Array.from method to convert the nodeList node list to an array.

Conclusion

To convert a DOM node list to an array in JavaScript, we can use the spread operator or the Array.from method.

Categories
JavaScript Answers

How to check if a query string value is present via JavaScript?

Sometimes, we want to check if a query string value is present via JavaScript.

In this article, we’ll look at how to check if a query string value is present via JavaScript.

How to check if a query string value is present via JavaScript?

To check if a query string value is present via JavaScript, we can use the URLSearchParams constructor.

For instance, we write

const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const product = urlParams.get("product");

to get the query string from the current URL with window.location.search.

Then we pass queryString into the URLSearchParams constructor to create the urlParams object.

Finally, we get the value of the query parameter with key 'product' with urlParams.get.

Conclusion

To check if a query string value is present via JavaScript, we can use the URLSearchParams constructor.

Categories
React Answers

How to fix ESLint with React giving no-unused-vars errors?

Sometimes, we want to fix ESLint with React giving no-unused-vars errors.

In this article, we’ll look at how to fix ESLint with React giving no-unused-vars errors.

How to fix ESLint with React giving no-unused-vars errors?

To fix ESLint with React giving no-unused-vars errors, we can use the eslint-plugin-react plugin.

To install it, we run

npm install --save-dev eslint-plugin-react

Then we add it to .eslintrc.json by writing

{
  //...
  "extends": ["plugin:react/recommended"]
  //...
}

to add the "plugin:react/recommended" rules from the eslint-plugin-react plugin to our project.

Conclusion

To fix ESLint with React giving no-unused-vars errors, we can use the eslint-plugin-react plugin.

Categories
JavaScript Answers

How to disable browsers vertical and horizontal scrollbars with JavaScript?

Sometimes, we want to disable browsers vertical and horizontal scrollbars with JavaScript.

In this article, we’ll look at how to disable browsers vertical and horizontal scrollbars with JavaScript.

How to disable browsers vertical and horizontal scrollbars with JavaScript?

To disable browsers vertical and horizontal scrollbars with JavaScript, we can set the overflow style to 'hidden'.

For instance, we write

document.documentElement.style.overflow = "hidden";

to hide the scrollbars on the page by setting document.documentElement.style.overflow to 'hidden'.

We can restore the scrollbars with

document.documentElement.style.overflow = "auto";

Conclusion

To disable browsers vertical and horizontal scrollbars with JavaScript, we can set the overflow style to 'hidden'.

Categories
JavaScript Answers

How to add 0 to number if less than 10 in JavaScript?

Sometimes, we want to add 0 to number if less than 10 in JavaScript.

In this article, we’ll look at how to add 0 to number if less than 10 in JavaScript.

How to add 0 to number if less than 10 in JavaScript?

To add 0 to number if less than 10 in JavaScript, we can call the string padStart method.

For instance, we write

const n1 = (1).toString().padStart(2, "0");
const n2 = (11).toString().padStart(2, "0");

to call padStart with 2 and '0' to prepend '0' to the number convert to a string if it has less than 2 characters.

Therefore, n1 is '01' and n2 is '11'.

Conclusion

To add 0 to number if less than 10 in JavaScript, we can call the string padStart method.