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
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.

Categories
JavaScript Answers

How to get the unique elements in a JavaScript array and sort them?

Sometimes, we want to get the unique elements in a JavaScript array and sort them.

In this article, we’ll look at how to get the unique elements in a JavaScript array and sort them.

How to get the unique elements in a JavaScript array and sort them?

To get the unique elements in a JavaScript array and sort them, we use sets and the array sort method.

For instance, we write

const myData = ["237", "124", "255", "124", "366", "255"];
const uniqueAndSorted = [...new Set(myData)].sort();

to convert the myData array to a set with the Set constructor to remove the duplicate entries.

Then we convert the set back to an array with the spread operator.

Finally, we call sort to return a sorted version of the array.

Conclusion

To get the unique elements in a JavaScript array and sort them, we use sets and the array sort method.

Categories
JavaScript Answers

How to load JSON without a http get with d3 and JavaScript?

Sometimes, we want to load JSON without a http get with d3 and JavaScript.

In this article, we’ll look at how to load JSON without a http get with d3 and JavaScript.

How to load JSON without a http get with d3 and JavaScript?

To load JSON without a http get with d3 and JavaScript, we use the native JSON.parse method.

For instance, we write

const json = JSON.parse(myJson);

to call JSON.parse with the myJson json string to parse the string into a JavaScript object.

Conclusion

To load JSON without a http get with d3 and JavaScript, we use the native JSON.parse method.