Categories
JavaScript Answers

How to get a number of random elements from an array with JavaScript?

Sometimes, we want to get a number of random elements from an array with JavaScript.

In this article, we’ll look at how to get a number of random elements from an array with JavaScript.

How to get a number of random elements from an array with JavaScript?

To get a number of random elements from an array with JavaScript, we call array sort to shuffle the array.

Then we get a subarray from the shuffled array with slice.

For instance, we write

const shuffled = array.sort(() => 0.5 - Math.random());
const selected = shuffled.slice(0, n);

to call array.sort with a callback that returns a number between -0.5 and 0.5 to return a shuffled version of array.

And then we call shuffled.slice with 0 and n to get the first n elements.

Conclusion

To get a number of random elements from an array with JavaScript, we call array sort to shuffle the array.

Then we get a subarray from the shuffled array with slice.

Categories
JavaScript Answers

How to change the value of a global variable inside of a function with JavaScript?

Sometimes, we want to change the value of a global variable inside of a function with JavaScript.

In this article, we’ll look at how to change the value of a global variable inside of a function with JavaScript.

How to change the value of a global variable inside of a function with JavaScript?

To change the value of a global variable inside of a function with JavaScript, we can assign the global variable to a new value.

For instance, we write

a = 10;

const myFunction = () => {
  a = 20;
};

myFunction();

to set the global variable a to 20 in the myFunction function.

Conclusion

To change the value of a global variable inside of a function with JavaScript, we can assign the global variable to a new value.

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.