Categories
JavaScript Answers

How to overwrite a file using fs in Node.js and JavaScript?

Sometimes, we want to overwrite a file using fs in Node.js and JavaScript.

In this article, we’ll look at how to overwrite a file using fs in Node.js and JavaScript.

How to overwrite a file using fs in Node.js and JavaScript?

To overwrite a file using fs in Node.js and JavaScript, we call writeFileSync or writeFile.

For instance, we write

fs.writeFileSync(path, content, { encoding: "utf8", flag: "w" });

to write the content to the path.

The file will be overwritten with content by default.

Conclusion

To overwrite a file using fs in Node.js and JavaScript, we call writeFileSync or writeFile.

Categories
JavaScript Answers

How to wait for dynamically loaded script with JavaScript?

Sometimes, we want to wait for dynamically loaded script with JavaScript.

In this article, we’ll look at how to wait for dynamically loaded script with JavaScript.

How to wait for dynamically loaded script with JavaScript?

To wait for dynamically loaded script with JavaScript, we listen to the script element’s load event.

For instance, we write

<script
  id="hljs"
  async
  src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/highlight.min.js"
></script>

to add a script tag.

Then we write

const script = document.querySelector("#hljs");

script.addEventListener("load", () => {
  hljs.initHighlightingOnLoad();
});

to select the script tag with querySelector.

And we call addEventListener to listen its load event.

The callback is called when the script is loaded.

Conclusion

To wait for dynamically loaded script with JavaScript, we listen to the script element’s load event.

Categories
JavaScript Answers

How to detect if user changes tab with JavaScript?

Sometimes, we want to detect if user changes tab with JavaScript.

In this article, we’ll look at how to detect if user changes tab with JavaScript.

How to detect if user changes tab with JavaScript?

To detect if user changes tab with JavaScript, we listen to a few events.

For instance, we write

window.onfocus = (ev) => {
  console.log("gained focus");
};

window.onblur = (ev) => {
  console.log("lost focus");
};

to set window.onfocus to a function that’s called when the tab is in focus.

And we set window.onblur to a function that’s called when the tab loses focus.

Conclusion

To detect if user changes tab with JavaScript, we listen to a few events.

Categories
JavaScript Answers

How to set bar width with Chart.js 2 and JavaScript?

Sometimes, we want to set bar width with Chart.js 2 and JavaScript.

In this article, we’ll look at how to set bar width with Chart.js 2 and JavaScript.

How to set bar width with Chart.js 2 and JavaScript?

To set bar width with Chart.js 2 and JavaScript, we set the barThickness and maxBarThickness properties.

For instance, we write

const options = {
  type: "bar",
  data: [
    //...
  ],
  options: {
    scales: {
      xAxes: [
        {
          barThickness: 6,
          maxBarThickness: 8,
        },
      ],
    },
  },
};

to set the barThickness to the bar thickness in pixels.

And we set the maxBarThickness to the max bar thickness in pixels.

Conclusion

To set bar width with Chart.js 2 and JavaScript, we set the barThickness and maxBarThickness properties.

Categories
JavaScript Answers

How to truncate a string in JavaScript?

Sometimes, we want to truncate a string in JavaScript.

In this article, we’ll look at how to truncate a string in JavaScript.

How to truncate a string in JavaScript?

To truncate a string in JavaScript, we use the string substring method.

For instance, we write

const truncateString = (str, length) => {
  return str.length > length ? str.substring(0, length - 3) + "..." : str;
};

to define the truncateString function.

In it, we str.substring if str.length is bigger than length to return a truncated substring.

Otherwise, we return str.

Conclusion

To truncate a string in JavaScript, we use the string substring method.