Categories
JavaScript Answers

How to Assign Multiple Variables to the Same Value with JavaScript?

Spread the love

Sometimes, we want to assign multiple variables to the same value with JavaScript.

In this article, we’ll look at how to assign multiple variables to the same value with JavaScript.

Assign Multiple Variables to the Same Value with JavaScript

To assign multiple variables to the same value with JavaScript, we can use the destructuring syntax.

For instance, we can write:

const [
  moveUp,
  moveDown,
  moveLeft,
  moveRight,
  mouseDown,
  touchDown
] = Array(6).fill(false);

console.log(
  moveUp,
  moveDown,
  moveLeft,
  moveRight,
  mouseDown,
  touchDown
);

to destructure the values of the array that we created with Array(6).fill(false) .

Then we assign each value in the array to each variable on the left side.

Therefore, from the console log, we can see:

false false false false false false

logged.

Conclusion

To assign multiple variables to the same value with JavaScript, we can use the destructuring syntax.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *