Categories
JavaScript Answers

How to check for not null with JavaScript?

Sometimes, we want to check for not null with JavaScript.

In this article, we’ll look at how to check for not null with JavaScript.

How to check for not null with JavaScript?

To check for not null with JavaScript, we can use the !== operator.

For instance, we write

if (val !== null) {
  //...
}

to check if val isn’t equal to null with

val !== null

Conclusion

To check for not null with JavaScript, we can use the !== operator.

Categories
JavaScript Answers

How to remove padding or margins from Google Charts and JavaScript?

Sometimes, we want to remove padding or margins from Google Charts and JavaScript.

In this article, we’ll look at how to remove padding or margins from Google Charts and JavaScript.

How to remove padding or margins from Google Charts and JavaScript?

To remove padding or margins from Google Charts and JavaScript, we can set the width and height options.

For instance, we write

const options = {
  title: "How Much I Ate Last Night",
  width: 350,
  height: 400,
  chartArea: { width: "100%", height: "80%" },
  legend: { position: "bottom" },
};

to set the width and height in pixels.

And then we set the chartArea to fill 100% of the width and height to remove the margins and padding.

Conclusion

To remove padding or margins from Google Charts and JavaScript, we can set the width and height options.

Categories
JavaScript Answers

How to modify a query string without reloading the page with JavaScript?

Sometimes, we want to modify a query string without reloading the page with JavaScript.

In this article, we’ll look at how to modify a query string without reloading the page with JavaScript.

How to modify a query string without reloading the page with JavaScript?

To modify a query string without reloading the page with JavaScript, we can use the history.pushState method.

For instance, we write

const newUrl =
  window.location.protocol +
  "//" +
  window.location.host +
  window.location.pathname +
  "?myNewUrlQuery=1";
window.history.pushState({ path: newUrl }, "", newUrl);

to create the newUrl URL string.

Then we call pushState with { path: newUrl } and newUrl to navigate to newUrl without reloading the page.

We get the current URL data from window.location.

We get the protocol with window.location.protocol.

host has the hostname.

pathname has the path before the query string.

Conclusion

To modify a query string without reloading the page with JavaScript, we can use the history.pushState method.

Categories
JavaScript Answers

How to add paste image from clipboard functionality with JavaScript?

Sometimes, we want to add paste image from clipboard functionality with JavaScript.

In this article, we’ll look at how to add paste image from clipboard functionality with JavaScript.

How to add paste image from clipboard functionality with JavaScript?

To add paste image from clipboard functionality with JavaScript, we can listen to the document paste event and get the image from the paste event handler.

For instance, we write

document.onpaste = (event) => {
  const items = (event.clipboardData || event.originalEvent.clipboardData)
    .items;
  console.log(JSON.stringify(items));
  let blob = null;
  for (let i = 0; i < items.length; i++) {
    if (items[i].type.indexOf("image") === 0) {
      blob = items[i].getAsFile();
    }
  }

  if (blob !== null) {
    const reader = new FileReader();
    reader.onload = (event) =>{
      console.log(event.target.result); 
    };
    reader.readAsDataURL(blob);
  }
};

to set document.onpaste function to a function that gets the image data.

In it, we get the clipboard data with

const items = (event.clipboardData || event.originalEvent.clipboardData)
    .items;

Then we create the blob with a for loop that gets the item from the items array with type having the substring 'image' in it.

Then we use the FileReader object’s readAsDataURL to read the blob as a base64 image URL string.

And we get the image data with event.target.result in reader.onload.

Conclusion

To add paste image from clipboard functionality with JavaScript, we can listen to the document paste event and get the image from the paste event handler.

Categories
JavaScript Answers

How to reset Jest mock functions calls count before every test with JavaScript?

Sometimes, we want to reset Jest mock functions calls count before every test with JavaScript.

in this article, we’ll look at how to reset Jest mock functions calls count before every test with JavaScript.

How to reset Jest mock functions calls count before every test with JavaScript?

To reset Jest mock functions calls count before every test with JavaScript, we can call mockClear on the mocked function or clearAllMocks to clear all mocks.

For instance, we write

afterEach(() => {
  local.getData.mockClear();
});

to call local.getData.mockClear to clear the mocked local.getData method after each test by calling it in the afterEach callback.

We can use

afterEach(() => {
  jest.clearAllMocks();
});

to call jest.clearAllMocks to clear all mocks after each test.

Conclusion

To reset Jest mock functions calls count before every test with JavaScript, we can call mockClear on the mocked function or clearAllMocks to clear all mocks.