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.

Categories
JavaScript Answers

How to remove first and last element in array with JavaScript?

Sometimes, we want to remove first and last element in array with JavaScript.

In this article, we’ll look at how to remove first and last element in array with JavaScript.

How to remove first and last element in array with JavaScript?

To remove first and last element in array with JavaScript, we can use the array slice method.

For instance, we write

const newFruits = fruits.slice(1, -1);

to call fruits.slice with 1 and -1 to return a new array without the first and last entries of fruits.

Then we assign the returned array to newFruits.

Conclusion

To remove first and last element in array with JavaScript, we can use the array slice method.