Categories
JavaScript Answers

How to truncate or round whole number in JavaScript?

Sometimes, we want to truncate or round whole number in JavaScript.

In this article, we’ll look at how to truncate or round whole number in JavaScript.

How to truncate or round whole number in JavaScript?

To truncate or round whole number in JavaScript, we can use the Math.trunc method.

For instance, we write

const num = -2147483649.536;
const result = Math.trunc(num);

to call Math.trunc with num to return a number with the decimal part removed.

Conclusion

To truncate or round whole number in JavaScript, we can use the Math.trunc method.

Categories
JavaScript Answers

How to set a value in CKEditor with JavaScript?

To set a value in CKEditor with JavaScript, we use the setData method.

For instance, we write

CKEDITOR.instances["fieldName"].setData("your data");

to get the CKEditor instance with CKEDITOR.instances["fieldName"].

Then we call setData with "your data" to set the field’s value to "your data".

Conclusion

To set a value in CKEditor with JavaScript, we use the setData method.

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.