Categories
JavaScript Answers

How to remove whitespaces with a regular expression and JavaScript?

Sometimes, we want to remove whitespaces with a regular expression and JavaScript.

In this article, we’ll look at how to remove whitespaces with a regular expression and JavaScript.

How to remove whitespaces with a regular expression and JavaScript?

To remove whitespaces with a regular expression and JavaScript, we can use the string replace method.

For instance, we write:

const str = "    bob is a good      boy     "
const s = str.replace(/^\s+|\s+$|\s+(?=\s)/g, "");
console.log(s)

to call str.replace with the /^\s+|\s+$|\s+(?=\s)/g regex.

The regex matches whitespaces at the start of a string with ^\s+.

It matches whitespaces at the end of a string with \s+$.

And it matches multiple whitespaces within the string with \s+(?=\s).

The g flag returns all matches in the string.

And then we replace them all with an empty string.

Therefore, s is 'bob is a good boy'.

Conclusion

To remove whitespaces with a regular expression and JavaScript, we can use the string replace method.

Categories
JavaScript Answers

How to convert strings to bigInt in JavaScript?

Sometimes, we want to convert strings to bigInt in JavaScript.

In this article, we’ll look at how to convert strings to bigInt in JavaScript.

How to convert strings to bigInt in JavaScript?

To convert strings to bigInt in JavaScript, we can use the BigInt function.

For instance, we write:

const string = '8795485488564835575723945398498489398434434';
const bigInt = BigInt(string);
console.log(bigInt)

to call BigInt with string to convert string to a bigint.

Therefore, bigInt is 8795485488564835575723945398498489398434434n .

Conclusion

To convert strings to bigInt in JavaScript, we can use the BigInt function.

Categories
JavaScript Answers

How to increment a numeric string by +1 with JavaScript?

Sometimes, we want to increment a numeric string by +1 with JavaScript.

In this article, we’ll look at how to increment a numeric string by +1 with JavaScript.

How to increment a numeric string by +1 with JavaScript?

To increment a numeric string by +1 with JavaScript, we can convert it to a number before incrementing it.

For instance, we write:

let pageID = '1'
pageID = parseInt(pageID) + 1;
console.log(pageID)

to call parseInt with pageID to convert the number string into a number.

Then we add 1 to it and assign the returned value back to pageID.

Therefore, pageID is 2 according to the console log.

Conclusion

To increment a numeric string by +1 with JavaScript, we can convert it to a number before incrementing it.

Categories
JavaScript Answers

How to compare 2 dates in format DD/MM/YYYY with JavaScript?

Sometimes, we want to compare 2 dates in format DD/MM/YYYY with JavaScript.

In this article, we’ll look at how to compare 2 dates in format DD/MM/YYYY with JavaScript.

How to compare 2 dates in format DD/MM/YYYY with JavaScript?

To compare 2 dates in format DD/MM/YYYY with JavaScript, we can convert each date string to a date.

For instance, we write:

const date1 = '25/02/1985';
const date2 = '26/02/1985';

const convertToDate = (d) => {
  const [day, month, year] = d.split("/");
  return new Date(year, month - 1, day);
}

console.log(convertToDate(date1) > convertToDate(date2))

to define the convertToDate function that takes the date string d.

In it, we call split with '/' to split the string by the slashes.

And then we destructure the date parts from the returned array and put them into the Date constructor.

We’ve to subtract month by 1 since JavaScript date’s months are zero-based.

Next, we call convertToDate with date1 and date2 and compare the 2 values.

Since date1 is earlier than date2, the console log should log false.

Conclusion

To compare 2 dates in format DD/MM/YYYY with JavaScript, we can convert each date string to a date.

Categories
JavaScript Answers

How to make an image load synchronously with JavaScript?

Sometimes, we want to make an image load synchronously with JavaScript.

In this article, we’ll look at how to make an image load synchronously with JavaScript.

How to make an image load synchronously with JavaScript?

To make an image load synchronously with JavaScript, we can make a promise that loads the image.

For instance, we write:

const loadImage = (url) => {
  const img = new Image()
  return new Promise(resolve => {
    img.onload = () => {
      resolve(img)
    }
    img.src = url
  })
}

const urls = [
  'https://i.picsum.photos/id/879/200/300.jpg?hmac=07llkorYxtpw0EwxaeqFKPC5woveWVLykQVnIOyiwd8',
  'https://i.picsum.photos/id/35/200/300.jpg?hmac=No1hMogzX_PUqgWDfLRCc4wGPYTIeviBhJbzjqskoMA',
  'https://i.picsum.photos/id/56/200/300.jpg?hmac=XmVgSk2B8hc9Smojh4o14JnHBHBM8Gj0ePS78sxZbzI'
]

const load = async () => {
  for (const u of urls) {
    const img = await loadImage(u)
    document.body.appendChild(img)
  }
}
load()

We define the loadImage functino that returns a promise with the img element.

We create the img element with the Image constructor.

And we call resolve with img once the image has been loaded by setting the img.src to the image url.

Next, we define the load function that loops through each image URL in urls and call loadImage on each entry.

And then we call document.body.appendChild with img to add the image as a child of the body element.

Conclusion

To make an image load synchronously with JavaScript, we can make a promise that loads the image.