Categories
JavaScript Answers

How to convert a JavaScript string variable to decimal or money?

Sometimes, we want to convert a JavaScript string variable to decimal or money.

In this article, we’ll look at how to convert a JavaScript string variable to decimal or money.

How to convert a JavaScript string variable to decimal or money?

To convert a JavaScript string variable to decimal or money, we can use the Intl.NumberFormat constructor.

For instance, we write

const formatter = new Intl.NumberFormat("en", {
  style: "currency",
  currency: "GBP",
});

console.log(formatter.format(1234.5));

to create the Intl.NumberFormat object by calling it with the locale string and an object with the formatting options.

'currency' formats the string as a currency number.

And currency is set to 'GBP' so the number is formatted into a GBP value.

Then we call formatter.format with the number to format to return a string with the currency number.

Conclusion

To convert a JavaScript string variable to decimal or money, we can use the Intl.NumberFormat constructor.

Categories
JavaScript Answers

How to convert date to UTC using moment.js and JavaScript?

Sometimes, we want to convert date to UTC using moment.js and JavaScript.

In this article, we’ll look at how to convert date to UTC using moment.js and JavaScript.

How to convert date to UTC using moment.js and JavaScript?

To convert date to UTC using moment.js and JavaScript, we can use the moment’s utc method.

For instance, we write

const utcStart = new moment("2022-06-24T09:00", "YYYY-MM-DDTHH:mm").utc();

to create a moment object with the datetime with moment.

Then we call utc to return a moment object with the same date in UTC.

Conclusion

To convert date to UTC using moment.js and JavaScript, we can use the moment’s utc method.

Categories
JavaScript Answers

How to mark a field invalid from JavaScript?

Sometimes, we want to mark a field invalid from JavaScript.

In this article, we’ll look at how to mark a field invalid from JavaScript.

How to mark a field invalid from JavaScript?

To mark a field invalid from JavaScript, we use the field element’s setCustomValidity method.

For instance, we write

field.setCustomValidity("Invalid field.");

to call field.setCustomValidity with a validation error message string to mark it as invalid.

We write

field.setCustomValidity("");

to call field.setCustomValidity with an empty string to mark it as valid.

field is an element like input or select

Conclusion

To mark a field invalid from JavaScript, we use the field element’s setCustomValidity method.

Categories
JavaScript Answers

How to select element that does not have specific class with JavaScript?

Sometimes, we want to select element that does not have specific class with JavaScript.

In this article, we’ll look at how to select element that does not have specific class with JavaScript.

How to select element that does not have specific class with JavaScript?

To select element that does not have specific class with JavaScript, we can use the document.querySelector method with the :not pseudoclass.

For instance, we write

const li = document.querySelector("li:not(.completed)");

to select the first li element that doesn’t have the completed class.

We can have multiple :not pseudoclasses in the same selector.

For instance, we write

const li = document.querySelector("li:not(.completed):not(.selected)");

to select the first li element that doesn’t have the completed or the selected class.

Conclusion

To select element that does not have specific class with JavaScript, we can use the document.querySelector method with the :not pseudoclass.

Categories
JavaScript Answers

How to beautify JavaScript code using command line?

Sometimes, we want to beautify JavaScript code using command line.

In this article, we’ll look at how to beautify JavaScript code using command line.

How to beautify JavaScript code using command line?

To beautify JavaScript code using command line, we can use prettier.

For instance, we run

npx prettier --single-quote --write --trailing-comma all "js/**/*.js"

to run prettier with npx to beautify JavaScript code in the js folder, including all child directories in it.

The --single-quote option formats strings with single quotes.

--write saves the formatting changes.

--trailing-comma add trailing commas to places that can have them like in the end of arrays and object literals.

Conclusion

To beautify JavaScript code using command line, we can use prettier.