Categories
JavaScript Answers

How to set boolean values in LocalStorage with JavaScript?

To set boolean values in LocalStorage with JavaScript, we can parse the string value into a boolean with JSON.parse.

For instance, we write

const value = "true";
console.log(JSON.parse(value) === true);

to parse 'true' back into true with JSON.parse.

Then the console log should log true since the left side returns true.

Categories
JavaScript Answers

How to unpack array into separate variables in JavaScript?

Sometimes, we want to unpack array into separate variables in JavaScript.

In this article, we’ll look at how to unpack array into separate variables in JavaScript.

How to unpack array into separate variables in JavaScript?

To unpack array into separate variables in JavaScript, we use the array destructuring syntax.

For instance, we write

const [x, y] = ["foo", "bar"];
console.log(x);
console.log(y);

to assign 'foo' to x and 'bar' to y.

Conclusion

To unpack array into separate variables in JavaScript, we use the array destructuring syntax.

Categories
JavaScript Answers

How to check if mouse button down with JavaScript?

Sometimes, we want to check if mouse button down with JavaScript.

In this article, we’ll look at how to check if mouse button down with JavaScript.

How to check if mouse button down with JavaScript?

To check if mouse button down with JavaScript, we listen for the mousedown and mouseup events.

For instance, we write

let mouseDown = false;
document.body.onmousedown = () => {
  mouseDown = true;
};
document.body.onmouseup = () => {
  mouseDown = false;
};

if (mouseDown) {
  // ....
}

to listen for the mousedown event on the body element with

document.body.onmousedown = () => {
  mouseDown = true;
};

We set mouseDown to true in document.body.onmousedown since the mouse button is down.

Likewise, we listen to the mouseup event with

document.body.onmouseup = () => {
  mouseDown = false;
};

And we set mouseDown to false.

Then we do something is mouseDown is true with

if (mouseDown) {
  // ....
}

Conclusion

To check if mouse button down with JavaScript, we listen for the mousedown and mouseup events.

Categories
JavaScript Answers

How to run only one test with Cypress and JavaScript?

Sometimes, we want to run only one test with Cypress and JavaScript.

In this article, we’ll look at how to run only one test with Cypress and JavaScript.

How to run only one test with Cypress and JavaScript?

To run only one test with Cypress and JavaScript, we can set the --space option or use the it.only method.

For instance, we run

cypress run --spec path/to/file.spec.js

to run the tests in path/to/file.spec.js.

In our test, we write

it.only("only run this one", () => {
  // ...
});

it("not this one", () => {});

to run only the first test by calling creating the test with it.only.

Conclusion

To run only one test with Cypress and JavaScript, we can set the --space option or use the it.only method.

Categories
JavaScript Answers

How to share variables between files in Node.js?

Sometimes, we want to share variables between files in Node.js.

In this article, we’ll look at how to share variables between files in Node.js.

How to share variables between files in Node.js?

To share variables between files in Node.js, we export the variables and then import them where we want to use them.

For instance, we write

module.js

const name = "foobar";

exports.name = name;

to export the name variable with

exports.name = name;

Then we import module.js and use name in main.js with

const myModule = require("./module");

const name = myModule.name;

Conclusion

To share variables between files in Node.js, we export the variables and then import them where we want to use them.