Categories
JavaScript Answers

How to detect if browser is running on an Android or iOS device with JavaScript?

Sometimes, we want to detect if browser is running on an Android or iOS device with JavaScript.

In this article, we’ll look at how to detect if browser is running on an Android or iOS device with JavaScript.

How to detect if browser is running on an Android or iOS device with JavaScript?

To detect if browser is running on an Android or iOS device with JavaScript, we check the user agent.

For instance, we write

const isMobile = {
  android() {
    return /Android/i.test(navigator.userAgent);
  },
  iOS() {
    return /iPhone|iPad|iPod/i.test(navigator.userAgent);
  },
};

to check if the page is loaded in a browser on Android with /Android/i.test(navigator.userAgent);.

And we check if the page is loaded in a browser on Android with /iPhone|iPad|iPod/i.test(navigator.userAgent).

navigator.userAgent has the user agent string.

Conclusion

To detect if browser is running on an Android or iOS device with JavaScript, we check the user agent.

Categories
JavaScript Answers

How to create an array of integers from 1 to 20 in JavaScript?

Sometimes, we want to create an array of integers from 1 to 20 in JavaScript.

In this article, we’ll look at how to create an array of integers from 1 to 20 in JavaScript.

How to create an array of integers from 1 to 20 in JavaScript?

To create an array of integers from 1 to 20 in JavaScript, we use the fill and map methods.

For instance, we write

const arr = Array(20)
  .fill()
  .map((x, i) => i + 1);

to call Array with 20 to create an array with 20 empty slots.

Then we call fill to fill them with undefined.

And then we call map to map the entries to index i plus 1 and return the array with the mapped values.

Conclusion

To create an array of integers from 1 to 20 in JavaScript, we use the fill and map methods.

Categories
JavaScript Answers

How to use the spread operator to update an object value with JavaScript?

Sometimes, we want to use the spread operator to update an object value with JavaScript.

In this article, we’ll look at how to use the spread operator to update an object value with JavaScript.

How to use the spread operator to update an object value with JavaScript?

To use the spread operator to update an object value with JavaScript, we use the spread operator in a new object.

For instance, we write

const o2 = { ...o1, a: "updated a" };
console.log(o2);

to spread the properties of o1 into a new object.

Then we add a or override the existing value of a with a: "updated a".

The new object is assigned to o2.

Conclusion

To use the spread operator to update an object value with JavaScript, we use the spread operator in a new object.

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.