Categories
JavaScript Answers

How to add elements to the DOM given plain text HTML using only pure JavaScript?

Sometimes, we want to add elements to the DOM given plain text HTML using only pure JavaScript.

In this article, we’ll look at how to add elements to the DOM given plain text HTML using only pure JavaScript.

How to add elements to the DOM given plain text HTML using only pure JavaScript?

To add elements to the DOM given plain text HTML using only pure JavaScript, we call the insertAdjancentHTML methods.

For instance, we write

document.body.insertAdjacentHTML("beforeend", theHTMLToInsert);

to call document.body.insertAdjacentHTML to insert the content of the theHTMLToInsert before the end of the body element.

Conclusion

To add elements to the DOM given plain text HTML using only pure JavaScript, we call the insertAdjancentHTML methods.

Categories
JavaScript Answers

How to add a window.onload event handler with JavaScript?

Sometimes, we want to add a window.onload event handler with JavaScript.

In this article, we’ll look at how to add a window.onload event handler with JavaScript.

How to add a window.onload event handler with JavaScript?

To add a window.onload event handler with JavaScript, we call window.addEventListener.

For instance, we write

window.addEventListener("load", () => {
  //...
});

to call window.addEventListener with 'load' to add a load event handler.

Conclusion

To add a window.onload event handler with JavaScript, we call window.addEventListener.

Categories
React Native Answers

How to render a shadow with React Native and JavaScript?

Sometimes, we want to render a shadow with React Native and JavaScript.

In this article, we’ll look at how to render a shadow with React Native and JavaScript.

How to render a shadow with React Native and JavaScript?

To render a shadow with React Native and JavaScript, we can use a View and set its elevation.

For instance, we write

<View elevation={5}>...</View>

to add a View and set its elevation prop to 5 to add some shadows to it.

Conclusion

To render a shadow with React Native and JavaScript, we can use a View and set its elevation.

Categories
JavaScript Answers

How to format a date DD/MM/YYYY into format in Moment.js and JavaScript?

Sometimes, we want to format a date DD/MM/YYYY into format in Moment.js and JavaScript.

In this article, we’ll look at how to format a date DD/MM/YYYY into format in Moment.js and JavaScript.

How to format a date DD/MM/YYYY into format in Moment.js and JavaScript?

To format a date DD/MM/YYYY into format in Moment.js and JavaScript, we use the format method.

For instance, we write

const searchDate = moment(new Date()).format("DD/MM/YYYY");

to call moment with a date to convert it to a moment object.

Then we call format with the "DD/MM/YYYY" format string to return a date string in the DD/MM/YYYY format.

Conclusion

To format a date DD/MM/YYYY into format in Moment.js and JavaScript, we use the format method.

Categories
JavaScript Answers

How to efficiently check if variable is Array or Object with Node.js and JavaScript?

Sometimes, we want to efficiently check if variable is Array or Object with Node.js and JavaScript.

In this article, we’ll look at how to efficiently check if variable is Array or Object with Node.js and JavaScript.

How to efficiently check if variable is Array or Object with Node.js and JavaScript?

To efficiently check if variable is Array or Object with Node.js and JavaScript, we can use the util.isArray method.

For instance, we write

const util = require("util");

const isArray = util.isArray([]);
const isArray2 = util.isArray({});

to call util.isArray with an array and an object.

isArray returns true is its argument is an array.

So isArray is true and isArray2 is false.

Conclusion

To efficiently check if variable is Array or Object with Node.js and JavaScript, we can use the util.isArray method.