Categories
JavaScript Answers

How to parse string to an int in JavaScript?

Sometimes, we want to parse string to an int in JavaScript.

In this article, we’ll look at how to parse string to an int in JavaScript.

How to parse string to an int in JavaScript?

To parse string to an int in JavaScript, we can use a regex to extract the digits from the string.

Then we can use parseInt to convert the string to a number.

For instance, we write:

const s = 'abc123';
const str = s.match(/\d+$/);
const number = parseInt(str, 10);
console.log(number)

to call s.match with a regex to return the digits part of s.

Then we call parseInt with str and 10 to return a number converted from str.

We pass in 10 to return a decimal number.

Therefore, number is 123.

Conclusion

To parse string to an int in JavaScript, we can use a regex to extract the digits from the string.

Then we can use parseInt to convert the string to a number.

Categories
JavaScript Answers

How to check that a date is greater than 30 days from today’s date with JavaScript?

Sometimes, we want to check that a date is greater than 30 days from today’s date with JavaScript.

In this article, we’ll look at how to check that a date is greater than 30 days from today’s date with JavaScript.

How to check that a date is greater than 30 days from today’s date with JavaScript?

To check that a date is greater than 30 days from today’s date with JavaScript, we can compare the timestamps of both dates.

For instance, we write:

const today = new Date()
const d = new Date(2022, 10, 10, 10)
const greaterThan30Days = +d > +today + 30 * 24 * 60 * 60 * 1000
console.log(greaterThan30Days)

to create 2 dates, today and d.

And then we check if d is more than 30 days after today by converting them both to timestamps in milliseconds with +

Then we add 30 days in milliseconds with 30 * 24 * 60 * 60 * 1000 to today.

And then we use > to do the comparison.

Therefore, greaterThan30Days is true.

Conclusion

To check that a date is greater than 30 days from today’s date with JavaScript, we can compare the timestamps of both dates.

Categories
JavaScript Answers

How to fix incorrect date shown in new Date() in JavaScript?

Sometimes, we want to fix incorrect date shown in new Date() in JavaScript.

In this article, we’ll look at how to fix incorrect date shown in new Date() in JavaScript.

How to fix incorrect date shown in new Date() in JavaScript?

To fix incorrect date shown in new Date() in JavaScript, we should include the time in the date string when we use it as an argument for Date.

For instance, we write:

const d = new Date('2022-09-05 00:00')
console.log(d)

to create date d with '2022-09-05 00:00' as the argument of the Date constructor.

As a result, d is Mon Sep 05 2022 00:00:00 GMT-0700 (Pacific Daylight Time).

Conclusion

To fix incorrect date shown in new Date() in JavaScript, we should include the time in the date string when we use it as an argument for Date.

Categories
JavaScript Answers

How to use setTimeout in an async function with JavaScript?

Sometimes, we want to use setTimeout in an async function with JavaScript.

In this article, we’ll look at how to use setTimeout in an async function with JavaScript.

How to use setTimeout in an async function with JavaScript?

To use setTimeout in an async function with JavaScript, we can create a promise that calls setTimeout.

For instance, we write:

const wait = delay => new Promise(resolve => setTimeout(resolve, delay));

const f = async () => {
  console.log(1)
  await wait(2000);
  console.log(2)
}
f()

to define the wait function that returns a promise which calls setTimeout with resolve as the callback and after delay milliseconds.

Once resolve is called, the promise finishes running.

Next, we define the function f that calls wait between the 2 console logs.

Therefore, we see 2 logged after 2 seconds after 1.

Conclusion

To use setTimeout in an async function with JavaScript, we can create a promise that calls setTimeout.

Categories
JavaScript Answers

How to call a JavaScript function when iframe finished loading?

Sometimes, we want to call a JavaScript function when iframe finished loading.

In this article, we’ll look at how to call a JavaScript function when iframe finished loading.

How to call a JavaScript function when iframe finished loading?

To call a JavaScript function when iframe finished loading, we can add an event listener for the load event.

For instance, we write:

<iframe src='https://example.com'></iframe>

to add an iframe.

Then we write:

const iframe = document.querySelector('iframe')
iframe.onload = () => {
  console.log('loaded')
}

to select the iframe with querySelector.

Then we set the iframe.onload property to a function that logs 'loaded'.

As a result, we see 'loaded' logged when the iframe is loaded.

Conclusion

To call a JavaScript function when iframe finished loading, we can add an event listener for the load event.