Categories
JavaScript Answers

How to toggle an element’s class in pure JavaScript?

Sometimes, we want to toggle an element’s class in pure JavaScript.

In this article, we’ll look at how to toggle an element’s class in pure JavaScript.

How to toggle an element’s class in pure JavaScript?

To toggle an element’s class in pure JavaScript, we can use the classList.toggle method.

For instance, we write

const menu = document.querySelector(".menu");
menu.classList.toggle("hidden-phone");

to select the element with class menu with querySelector.

And then we call menu.classList.toggle to toggle the hidden-phone class on the menu element.

Conclusion

To toggle an element’s class in pure JavaScript, we can use the classList.toggle method.

Categories
JavaScript Answers

How to use optional chaining with array or functions with JavaScript?

Sometimes, we want to use optional chaining with array or functions with JavaScript.

In this article, we’ll look at how to use optional chaining with array or functions with JavaScript.

How to use optional chaining with array or functions with JavaScript?

To use optional chaining with array or functions with JavaScript, we use ?. in place of ..

For instance, we write

const a = myArray.filter((x) => x.testKey === myTestKey)?.[0];

to call myArray.filter with a callback to find the entry with the testKey property equal to myTestKey.

Then we use ?. before [0] to return undefined if there’re no entries returned by filter instead of throwing an error.

?. is the optional chaining operator.

Conclusion

To use optional chaining with array or functions with JavaScript, we use ?. in place of ..

Categories
JavaScript Answers

How to correctly iterate through getElementsByClassName with JavaScript?

Sometimes, we want to correctly iterate through getElementsByClassName with JavaScript.

In this article, we’ll look at how to correctly iterate through getElementsByClassName with JavaScript.

How to correctly iterate through getElementsByClassName with JavaScript?

To correctly iterate through getElementsByClassName with JavaScript, we spread the node list into returned by getElementsByClassName into an array.

For instance, we write

[...document.querySelectorAll(".edit")].forEach((button) => {
  // ...
});

to select all elements with class edit with querySelectorAll and return a node list with them.

Then we use ... to spread the node list items into an array.

We then call forEach with a callback to loop through the elements.

We get the element from the button parameter.

Conclusion

To correctly iterate through getElementsByClassName with JavaScript, we spread the node list into returned by getElementsByClassName into an array.

Categories
JavaScript Answers

How to iterate over range in a functional way with JavaScript?

Sometimes, we want to iterate over range in a functional way with JavaScript.

In this article, we’ll look at how to iterate over range in a functional way with JavaScript.

How to iterate over range in a functional way with JavaScript?

To iterate over range in a functional way with JavaScript, we use the array keys method.

For instance, we write

const arr = [...Array(20).keys()].map((i) => i * i);

to create an array with 20 empty slots with Array(20).

Then we call keys to return an array of indexes of the array.

And then we call map with a callback to map the index i to i squared.

Conclusion

To iterate over range in a functional way with JavaScript, we use the array keys method.

Categories
JavaScript Answers

How to check if a value is within a range of numbers with JavaScript?

Sometimes, we want to check if a value is within a range of numbers with JavaScript.

In this article, we’ll look at how to check if a value is within a range of numbers with JavaScript.

How to check if a value is within a range of numbers with JavaScript?

To check if a value is within a range of numbers with JavaScript, we use comparison operators.

For instance, we write

const between = (x, min, max) => {
  return x >= min && x <= max;
};

if (between(x, 0.001, 0.009)) {
  // ...
}

to define the between function that checks if x is between min and max with

x >= min && x <= max

Conclusion

To check if a value is within a range of numbers with JavaScript, we use comparison operators.