Categories
JavaScript Answers

How to create a JavaScript ES6 promise for loop?

Sometimes, we want to create a JavaScript ES6 promise for loop.

In this article, we’ll look at how to create a JavaScript ES6 promise for loop.

How to create a JavaScript ES6 promise for loop?

To create a JavaScript ES6 promise for loop, we use a for-of loop.

For instance, we write

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

(async function loop() {
  for (const d of delays) {
    await delay(d);
  }
})();

to create a loop function that runs a for-of loop to loop through the delays array.

In the loop, we call delay with d in delays which returns a promise.

And we wait for delay to finish running with await in each iteration.

Conclusion

To create a JavaScript ES6 promise for loop, we use a for-of loop.

Categories
JavaScript Answers

How to format JavaScript moment.js date in 24h format?

Sometimes, we want to format JavaScript moment.js date in 24h format.

In this article, we’ll look at how to format JavaScript moment.js date in 24h format.

How to format JavaScript moment.js date in 24h format?

To format JavaScript moment.js date in 24h format, we use the "HH:mm:ss" format string.

For instance, we write

const t = moment("01:15:00 PM", "h:mm:ss A").format("HH:mm:ss");

to call moment to parse the "01:15:00 PM" time string with "h:mm:ss A" into a moment object.

Then we call format with "HH:mm:ss" to return a string with 24 hour hour format instead of 12 hour format with AM or PM at the end.

Conclusion

To format JavaScript moment.js date in 24h format, we use the "HH:mm:ss" format string.

Categories
JavaScript Answers

How to capitalize the first letter of each word in a string using JavaScript?

Sometimes, we want to capitalize the first letter of each word in a string using JavaScript.

In this article, we’ll look at how to capitalize the first letter of each word in a string using JavaScript.

How to capitalize the first letter of each word in a string using JavaScript?

To capitalize the first letter of each word in a string using JavaScript, we can use the string replace method with a regex.

For instance, we write

const s = text.replace(/(^\w|\s\w)/g, (m) => m.toUpperCase());

to call replace on the string text with a regex to match the start of each word in the string with (^\w|\s\w).

We use the g flag to get all matches.

Then we call it with a callback to return the matches converted to upper case with toUpperCase.

Conclusion

To capitalize the first letter of each word in a string using JavaScript, we can use the string replace method with a regex.

Categories
JavaScript Answers

How to dynamically load a JavaScript file?

Sometimes, we want to dynamically load a JavaScript file.

In this article, we’ll look at how to dynamically load a JavaScript file.

How to dynamically load a JavaScript file?

To dynamically load a JavaScript file, we can call the import function to import a module dynamically.

For instance, we write

(async () => {
  const bar = await import("./synth/BubbleSynth.js");
})();

to call import to import ./foo/bar.js dynamically.

It returns a promise with the module.

And we use await to get the content of the module.

Conclusion

To dynamically load a JavaScript file, we can call the import function to import a module dynamically.

Categories
JavaScript Answers

How to get datetime in JavaScript?

Sometimes, we want to get datetime in JavaScript.

In this article, we’ll look at how to get datetime in JavaScript.

How to get datetime in JavaScript?

To get datetime in JavaScript, we can use the Date constructor.

For instance, we write

new Date().toLocaleString()

to create a Date object and call toLocaleString on it to return a human readable string of the current date time formatted to the user’s current locale.

Conclusion

To get datetime in JavaScript, we can use the Date constructor.