Categories
JavaScript Answers

How to clear local storage, session storage and cookies in JavaScript?

Sometimes, we want to clear local storage, session storage and cookies in JavaScript.

In this article, we’ll look at how to clear local storage, session storage and cookies in JavaScript.

How to clear local storage, session storage and cookies in JavaScript?

To clear local storage, session storage and cookies in JavaScript, we use the clear method to clear local and session storage and update the cookie string.

For instance, we write

localStorage.clear();

to clear local storage.

We write

sessionStorage.clear();

to clear session storage.

And to clear cookies, we write

const cookies = document.cookie;

for (const myCookie of cookies.split(";")) {
  const pos = myCookie.indexOf("=");
  const name = pos > -1 ? myCookie.substr(0, pos) : myCookie;
  document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}

to split the document.cookie string into individual key-value pairs with split.

We loop through each item in the array returned by split with a for-of loop.

Then we get the index of '=' in each string with indexOf.

And then we get the name value by using substr if pos is bigger than -1 or use myCookie itself otherwise.

Then we set document.cookie to an expiry date that’s before the current date and time to make it expire.

Conclusion

To clear local storage, session storage and cookies in JavaScript, we use the clear method to clear local and session storage and update the cookie string.

Categories
JavaScript Answers

How to replace dash or hyphen with a space in JavaScript?

Sometimes, we want to replace dash or hyphen with a space in JavaScript.

In this article, we’ll look at how to replace dash or hyphen with a space in JavaScript.

How to replace dash or hyphen with a space in JavaScript?

To replace dash or hyphen with a space in JavaScript, we call replace with a regex.

For instance, we write

const str = "This-is-a-news-item-";
const newStr = str.replace(/-/g, " ");

to call str.replace with /-/g and an empty string to replace all dashes or hyphens with an empty string.

The g flag ensures that all matches are replaced.

A new string with the replacements done is returned.

Conclusion

To replace dash or hyphen with a space in JavaScript, we call replace with a regex.

Categories
JavaScript Answers

How to check if a text is all white space characters in JavaScript?

Sometimes, we want to check if a text is all white space characters in JavaScript.

In this article, we’ll look at how to check if a text is all white space characters in JavaScript.

How to check if a text is all white space characters in JavaScript?

To check if a text is all white space characters in JavaScript, we use the replace method.

For instance, we write

const trimmedUserText = userText.replace(/^\s+/, "").replace(/\s+$/, "");
if (trimmedUserText === "") {
  // ...
} else {
  // ...
}

to call replace to replace all whitespaces at the start of the string with empty strings with

userText.replace(/^\s+/, "")

Then we call replace again with /\s+$/ and '' to replace the rest of the whitespaces with empty strings.

And then a new string is returned with the replacements.

If trimmedUserText is an empty string, that means userText is all whitespace.

Conclusion

To check if a text is all white space characters in JavaScript, we use the replace method.

Categories
JavaScript Answers

How to use Chrome Fullscreen API with JavaScript?

Sometimes, we want to use Chrome Fullscreen API with JavaScript.

In this article, we’ll look at how to use Chrome Fullscreen API with JavaScript.

How to use Chrome Fullscreen API with JavaScript?

To use Chrome Fullscreen API with JavaScript, we use a few properties in an element.

For instance, we write

const toggleFullScreen = () => {
  if (!document.fullscreenElement) {
    document.documentElement.requestFullscreen();
  } else {
    document.exitFullscreen();
  }
};

to define the toggleFullScreen function.

In it, we check if the document isn’t full screen with document.fullscreenElement.

If it’s false, we request it to be full screen with document.documentElement.requestFullscreen.

Otherwise, we call document.exitFullscreen to exit full screen mode.

Conclusion

To use Chrome Fullscreen API with JavaScript, we use a few properties in an element.

Categories
JavaScript Answers

How to push to an array only if value doesn’t exist with JavaScript?

Sometimes, we want to push to an array only if value doesn’t exist with JavaScript.

In this article, we’ll look at how to push to an array only if value doesn’t exist with JavaScript.

How to push to an array only if value doesn’t exist with JavaScript?

To push to an array only if value doesn’t exist with JavaScript, we use a set.

For instance, we write

const s = new Set();

s.add("hello");
s.add("world");
s.add("hello");
s.delete("world");

const array = Array.from(s);

to use the Set constructor to create a set.

Then we call add to add entries to it.

And we call delete to delete entries from it.

Next, we call Array.from to convert set s to an array.

Conclusion

To push to an array only if value doesn’t exist with JavaScript, we use a set.