Categories
JavaScript Answers

How to get minimum and maximum date in JavaScript?

Sometimes, we want to get minimum and maximum date in JavaScript.

In this article, we’ll look at how to get minimum and maximum date in JavaScript.

How to get minimum and maximum date in JavaScript?

To get minimum and maximum date in JavaScript, the can use the timestamps for the min and max date in milliseconds.

For instance, we write

let maxDate = new Date(8640000000000000);
let minDate = new Date(-8640000000000000);

to use 8640000000000000 milliseconds since epoch as the max date timestamp.

Then we can pass that to the Date constructor to create a date object.

Likewise, we use -8640000000000000 milliseconds since epoch as the max date timestamp.

And then we create the Date object from it the same way.

Conclusion

To get minimum and maximum date in JavaScript, the can use the timestamps for the min and max date in milliseconds.

Categories
JavaScript Answers

How to clear this setInterval inside a function with JavaScript?

Sometimes, we want to clear this setInterval inside a function with JavaScript.

In this article, we’ll look at how to clear this setInterval inside a function with JavaScript.

How to clear this setInterval inside a function with JavaScript?

To clear this setInterval inside a function with JavaScript, we can call the clearInterval function.

For instance, we write

let intervalListener;
const someProcess = () => {
  //...
  if (stopIntervalIsTrue) {
    window.clearInterval(intervalListener);
  }
};
intervalListener = self.setInterval(someProcess, 1000);

to call setInterval with someProcess to run someProcess every second.

Then if stopIntervalIsTrue is true in someProcess to call window.clearInterval with intervalListener to clear the timer and stop someProcess from running.

Conclusion

To clear this setInterval inside a function with JavaScript, we can call the clearInterval function.

Categories
JavaScript Answers

How to dynamically update values of a Chart.js chart with JavaScript?

Sometimes, we want to dynamically update values of a Chart.js chart with JavaScript.

In this article, we’ll look at how to dynamically update values of a Chart.js chart with JavaScript.

How to dynamically update values of a Chart.js chart with JavaScript?

To dynamically update values of a Chart.js chart with JavaScript, we set the data set value and update the chart.

For instance, we write

myBarChart.data.datasets[0].bars[2].value = 50;
myBarChart.update();

to set the value we want to update with

myBarChart.data.datasets[0].bars[2].value = 50;

Then we call update to update the chart.

Conclusion

To dynamically update values of a Chart.js chart with JavaScript, we set the data set value and update the chart.

Categories
JavaScript Answers

How to detect if document has loaded with JavaScript?

Sometimes, we want to detect if document has loaded with JavaScript.

In this article, we’ll look at how to detect if document has loaded with JavaScript.

How to detect if document has loaded with JavaScript?

To detect if document has loaded with JavaScript, we can check if document.readystate is set to 'complete'.

For instance, we write

if (document.readyState === "complete") {
  init();
}

to check if document.readystate is 'complete'.

If it is, then document has loaded.

Conclusion

To detect if document has loaded with JavaScript, we can check if document.readystate is set to 'complete'.

Categories
JavaScript Answers

How to create a hash or dictionary object in JavaScript?

Sometimes, we want to create a hash or dictionary object in JavaScript.

In this article, we’ll look at how to create a hash or dictionary object in JavaScript.

How to create a hash or dictionary object in JavaScript?

To create a hash or dictionary object in JavaScript, we can create a plain object.

For instance, we write

const a = {};
a["key1"] = "value1";
a["key2"] = "value2";

to create object a.

Then we add properties to it with

a["key1"] = "value1";
a["key2"] = "value2";

The property names are in the square brackets and the values are after the = sign.

Conclusion

To create a hash or dictionary object in JavaScript, we can create a plain object.