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.

Categories
JavaScript Answers

How to format a JavaScript string using placeholders and an object of substitutions?

Sometimes, we want to format a JavaScript string using placeholders and an object of substitutions.

In this article, we’ll look at how to format a JavaScript string using placeholders and an object of substitutions.

How to format a JavaScript string using placeholders and an object of substitutions?

To format a JavaScript string using placeholders and an object of substitutions, we use template literals.

For instance, we write

const str = `My name is ${replacements.name} and my age is ${replacements.age}.`;

to interpolate the value of replacements.name and replacements.age into the string.

Conclusion

To format a JavaScript string using placeholders and an object of substitutions, we use template literals.

Categories
JavaScript Answers

How to add href attribute to a link dynamically using JavaScript?

Sometimes, we want to add href attribute to a link dynamically using JavaScript.

In this article, we’ll look at how to add href attribute to a link dynamically using JavaScript.

How to add href attribute to a link dynamically using JavaScript?

To add href attribute to a link dynamically using JavaScript, we can set the link’s href property.

For instance, we write

const a = document.getElementById("yourlinkId");
a.href = "http://example.com";

to select the link with getElementById.

Then we set the href attribute of the link by settings its href property.

Conclusion

To add href attribute to a link dynamically using JavaScript, we can set the link’s href property.

Categories
JavaScript Answers

How to get current time in milliseconds with moment.js and JavaScript?

Sometimes, we want to get current time in milliseconds with moment.js and JavaScript.

In this article, we’ll look at how to get current time in milliseconds with moment.js and JavaScript.

How to get current time in milliseconds with moment.js and JavaScript?

To get current time in milliseconds with moment.js and JavaScript, we use the valueOf method.

For instance, we write

const timeInMilliseconds = moment().valueOf();

to call moment to return an moment object with the current datetime.

And then we call valueOf on it to return the timestamp of the current datetime in milliseconds.

Conclusion

To get current time in milliseconds with moment.js and JavaScript, we use the valueOf method.

Categories
JavaScript Answers

How to check element CSS display with JavaScript?

Sometimes, we want to check element CSS display with JavaScript.

In this article, we’ll look at how to check element CSS display with JavaScript.

How to check element CSS display with JavaScript?

To check element CSS display with JavaScript, we use the window.getComputedStyle method.

For instance, we write

const { display } = window.getComputedStyle(element, null);

to call window.getComputedStyle with the element that we want to get the styles for.

Then we get the value of the display CSS property computed by the browser with the display property from the returned object.

Conclusion

To check element CSS display with JavaScript, we use the window.getComputedStyle method.