Categories
JavaScript Answers

How to make a promise from setTimeout with JavaScript?

Sometimes, we want to make a promise from setTimeout with JavaScript.

In this article, we’ll look at how to make a promise from setTimeout with JavaScript.

How to make a promise from setTimeout with JavaScript?

To make a promise from setTimeout with JavaScript, we can use the Promise constructor.

For instance, we write

const later = (delay) => {
  return new Promise((resolve) => {
    setTimeout(resolve, delay);
  });
};

to create the later function which returns a promise we create with the Promise constructor.

We create the promise by calling Promise with a callback that calls setTimeout with resolve as its callback.

The delay is in milliseconds.

The promise will resolve after delay milliseconds as a result.

Conclusion

To make a promise from setTimeout with JavaScript, we can use the Promise constructor.

Categories
JavaScript Answers

How to check whether HTML element has scrollbars with JavaScript?

Sometimes, we want to check whether HTML element has scrollbars with JavaScript.

In this article, we’ll look at how to check whether HTML element has scrollbars with JavaScript.

How to check whether HTML element has scrollbars with JavaScript?

To check whether HTML element has scrollbars with JavaScript, we check if the scrollHeight of the element is bigger than its clientHeight.

And we check if scrollWidth of the element is bigger than its clientWidth.

For instance, we write

const div = document.getElementById("div-id");

const hasHorizontalScrollbar = div.scrollWidth > div.clientWidth;
const hasVerticalScrollbar = div.scrollHeight > div.clientHeight;

to select the element with getElementById.

Then we check if the body element’s scrollHeight is bigger than its clientHeight to check if the body element has a vertical scrollbar.

Likewise, we check if the body element’s scrollWidth is bigger than its clientWidth to check if the body element has a horizontal scrollbar.

Conclusion

To check whether HTML element has scrollbars with JavaScript, we check if the scrollHeight of the element is bigger than its clientHeight.

And we check if scrollWidth of the element is bigger than its clientWidth.

Categories
JavaScript Answers

How to add characters to a string in JavaScript?

Sometimes, we want to add characters to a string in JavaScript.

In this article, we’ll look at how to add characters to a string in JavaScript.

How to add characters to a string in JavaScript?

To add characters to a string in JavaScript, we can use the += operator.

For instance, we write

let text = "";
for (let l of list) {
  text += l;
}

to loop through the characters in the list array and concatenate them to text with += .

Conclusion

To add characters to a string in JavaScript, we can use the += operator.

Categories
JavaScript Answers

How to create a case insensitive regex in JavaScript?

Sometimes, we want to create a case insensitive regex in JavaScript.

In this article, we’ll look at how to create a case insensitive regex in JavaScript.

How to create a case insensitive regex in JavaScript?

To create a case insensitive regex in JavaScript, we can use the i flag.

For instance, we write

const value = "some text with dAtE";
const hasDate = /date/i.test(value);

to create the /date/i regex which matches 'date' with the letters in any case.

And then we call test on it with value to match 'dAtE'.

Conclusion

To create a case insensitive regex in JavaScript, we can use the i flag.

Categories
JavaScript Answers

How to do deep copy in ES6 using the spread syntax?

Sometimes, we want to do deep copy in ES6 using the spread syntax.

In this article, we’ll look at how to do deep copy in ES6 using the spread syntax.

How to do deep copy in ES6 using the spread syntax?

To do deep copy in ES6 using the spread syntax, we use JSON.stringify and JSON.parse.

For instance, we write

const oldObject = {
  name: "A",
  address: {
    street: "Station Road",
    city: "Pune",
  },
};
const newObject = JSON.parse(JSON.stringify(oldObject));

to call JSON.stringify with oldObject to return the JSON string version of oldObject.

Then we call JSON.parse to parse the JSON string back to an object to return a deep clone of oldObject.

Conclusion

To do deep copy in ES6 using the spread syntax, we use JSON.stringify and JSON.parse.