Categories
JavaScript Answers

How to check if a number is between two values in JavaScript?

Sometimes, we want to check if a number is between two values in JavaScript.

In this article, we’ll look at how to check if a number is between two values in JavaScript.

How to check if a number is between two values in JavaScript?

To check if a number is between two values in JavaScript, we can use comparison operators.

For instance, we write

if (windowSize > 100 && windowSize < 200) {
  // ...
}

to check if windowSize is between 100 and 200 exclusive with

windowSize > 100 && windowSize < 200

It returns true if windowSize is between 100 and 200 excluding the end numbers.

Conclusion

To check if a number is between two values in JavaScript, we can use comparison operators.

Categories
JavaScript Answers

How to change mock implementation on a per single test basis with Jest and JavaScript?

Sometimes, we want to change mock implementation on a per single test basis with Jest and JavaScript.

In this article, we’ll look at how to change mock implementation on a per single test basis with Jest and JavaScript.

How to change mock implementation on a per single test basis with Jest and JavaScript?

To change mock implementation on a per single test basis with Jest and JavaScript, we call mockImplementation.

For instance, we write

import { funcToMock } from "./somewhere";
jest.mock("./somewhere");

beforeEach(() => {
  funcToMock.mockImplementation(() => {
    //...
  });
});

test("test", () => {
  funcToMock.mockImplementation(() => {
    //...
  });
});

to call funcToMock.mockImplementation with a function that has the implementation of the funcToMock function we’re mocking.

Conclusion

To change mock implementation on a per single test basis with Jest and JavaScript, we call mockImplmentation.

Categories
JavaScript Answers

How to extract a string using JavaScript regex?

Sometimes, we want to extract a string using JavaScript regex.

In this article, we’ll look at how to extract a string using JavaScript regex.

How to extract a string using JavaScript regex?

To extract a string using JavaScript regex, we can use the regex exec method.

For instance, we write

const extractSummary = (iCalContent) => {
  const rx = /\nSUMMARY:(.*)\n/g;
  const arr = rx.exec(iCalContent);
  return arr[1];
};

to define the extractSummary function that gets the part of the iCalContent string that has the 'SUMMARY:' and some text after it.

Then we get the match with arr[1].

Conclusion

To extract a string using JavaScript regex, we can use the regex exec method.

Categories
JavaScript Answers

How to save JavaScript objects in sessionStorage?

Sometimes, we want to save JavaScript objects in sessionStorage.

In this article, we’ll look at how to save JavaScript objects in sessionStorage.

How to save JavaScript objects in sessionStorage?

To save JavaScript objects in sessionStorage, we convert the object to a string before saving it.

For instance, we write

const user = { name: "jane" };
sessionStorage.setItem("user", JSON.stringify(user));
const obj = JSON.parse(sessionStorage.user);

to call JSON.stringify with user to convert user to a JSON string.

Then we call setItem to save the string with key 'user'.

And then we can get the value and parse it back to an object with

JSON.parse(sessionStorage.user)

Conclusion

To save JavaScript objects in sessionStorage, we convert the object to a string before saving it.

Categories
JavaScript Answers

How to capture iframe load complete event with JavaScript?

Sometimes, we want to capture iframe load complete event with JavaScript.

In this article, we’ll look at how to capture iframe load complete event with JavaScript.

How to capture iframe load complete event with JavaScript?

To capture iframe load complete event with JavaScript, we can set the onload property of the iframe to a function that runs when the iframe content finishes loading.

For instance, we write

const iframe = document.createElement("iframe");
iframe.onload = () => {
  console.log("iframe loaded");
};
iframe.src = "...";
document.body.appendChild(iframe);

to select the iframe with createElement.

Then we set its onload property to a function that runs when the iframe content is loaded.

And then we set the src property of the iframe to load it.

Conclusion

To capture iframe load complete event with JavaScript, we can set the onload property of the iframe to a function that runs when the iframe content finishes loading.