Categories
JavaScript Answers

How to break a string across more than one line of code in JavaScript?

Sometimes, we want to break a string across more than one line of code in JavaScript.

In this article, we’ll look at how to break a string across more than one line of code in JavaScript.

How to break a string across more than one line of code in JavaScript?

To break a string across more than one line of code in JavaScript, we can use template strings.

For instance, we write

alert(`Please select file   
to delete`);

to show an alert box with

Please select file   
to delete

in it.

The whitespaces and new lines are kept by template strings.

Conclusion

To break a string across more than one line of code in JavaScript, we can use template strings.

Categories
JavaScript Answers

How to format numbers and strings with thousand separator with JavaScript?

Sometimes, we want to format numbers and strings with thousand separator with JavaScript.

In this article, we’ll look at how to format numbers and strings with thousand separator with JavaScript.

How to format numbers and strings with thousand separator with JavaScript?

To format numbers with thousand separator with JavaScript, we can use the number toLocaleString method.

For instance, we write

const a = (1234567.89).toLocaleString('en')  
const b = parseFloat("1234567.89").toLocaleString('en')

to call toLocaleString with the locale of the returned formatted number string.

We convert the number string with parseFloat before we call toLocaleString on it since toLocaleString is a number method.

Conclusion

To format numbers with thousand separator with JavaScript, we can use the number toLocaleString method.

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.

For instance, we write

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

to call text.replace with a regex to get the first letter of each word with regex /(^\w|\s\w)/g.

Then we use the callback to return the matches converted to upper case with toUpperCase to do the replacement.

^\w matches the first letter of the first word and \s\w matches the first letter of other words.

Conclusion

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

Categories
JavaScript Answers

How to conditionally import an ES6 module with JavaScript?

Sometimes, we want to conditionally import an ES6 module with JavaScript.

In this article, we’ll look at how to conditionally import an ES6 module with JavaScript.

How to conditionally import an ES6 module with JavaScript?

To conditionally import an ES6 module with JavaScript, we can use the import function.

For instance, we write

const myModule = await import(moduleName);

in an async function to call import with the moduleName string to import the module named moduleName.

And then we get the module returned by the promise returned by import with await.

Conclusion

To conditionally import an ES6 module with JavaScript, we can use the import function.

Categories
JavaScript Answers

How to refresh part of page with JavaScript?

Sometimes, we want to refresh part of page with JavaScript.

In this article, we’ll look at how to refresh part of page with JavaScript.

How to refresh part of page with JavaScript?

To refresh part of page with JavaScript, we can set the innerHTML property of the element we want to update to the content we want to show.

For instance, we write

const url =
  "https://server.test-cors.org/server?id=2934825&enable=true&status=200&credentials=false&methods=GET";

async function refresh() {
  btn.disabled = true;
  dynamicPart.innerHTML = "Loading...";
  dynamicPart.innerHTML = await (await fetch(url)).text();
}

to create the refresh function that makes a GET request to the url with fetch.

And then we get the text content from the response and set the innerHTML property of the dynamicPart element to what we want.

The dynamicPart element may be an element like

<div id="dynamicPart">Dynamic part</div>

We can select it with

const dynamicPart = document.getElementById('dynamicPart')

Conclusion

To refresh part of page with JavaScript, we can set the innerHTML property of the element we want to update to the content we want to show.