Categories
JavaScript Answers

How to get the device width in JavaScript?

Sometimes, we want to get the device width in JavaScript.

In this article, we’ll look at how to get the device width in JavaScript.

How to get the device width in JavaScript?

To get the device width in JavaScript, we use the window.innerWidth or screen.width properties.

For instance, we write

const width = window.innerWidth > 0 ? window.innerWidth : screen.width;

to check if window.innerWidth is bigger than 0.

If it is, then we return that as the width.

Otherwise, we return screen.width.

window.innerWidth is available in the desktop.

And screen.width is available on mobile devices.

Conclusion

To get the device width in JavaScript, we use the window.innerWidth or screen.width properties.

Categories
JavaScript Answers

How to monitor the memory usage of Node.js?

Sometimes, we want to monitor the memory usage of Node.js.

In this article, we’ll look at how to monitor the memory usage of Node.js.

How to monitor the memory usage of Node.js?

To monitor the memory usage of Node.js, we use the process.memoryUsage method.

For instance, we write

const formatMemoryUsage = (data) =>
  `${Math.round((data / 1024 / 1024) * 100) / 100} MB`;

const memoryData = process.memoryUsage();

const memoryUsage = {
  rss: formatMemoryUsage(memoryData.rss),
  heapTotal: formatMemoryUsage(memoryData.heapTotal),
  heapUsed: formatMemoryUsage(memoryData.heapUsed),
  external: formatMemoryUsage(memoryData.external),
};

console.log(memoryUsage);

to get an object that various properties.

rss is the Resident Set Size, which is the total memory allocated for the process execution.

heapTotal is the total size of the allocated heap.

heapUsed is the amount of memory used.

external is the V8 external memory amount.

All values are in bytes.

Conclusion

To monitor the memory usage of Node.js, we use the process.memoryUsage method.

Categories
JavaScript Answers

How to show alternate image if source image is not found with JavaScript?

Sometimes, we want to show alternate image if source image is not found with JavaScript.

In this article, we’ll look at how to show alternate image if source image is not found with JavaScript.

How to show alternate image if source image is not found with JavaScript?

To show alternate image if source image is not found with JavaScript, we can listen to the error event of the img element.

For instance, we write

const img = document.getElementById("myImage");
img.onerror = () => {
  img.src = "alternate.png";
};

to get the img element with getElementById.

Then we set img.onerror to a function that runs when the error event is triggered by the img element.

The event will be triggered if the image fails to load.

In the function, we set the src property of the img element to an alternative image URL.

Conclusion

To show alternate image if source image is not found with JavaScript, we can listen to the error event of the img element.

Categories
JavaScript Answers

How to obtain the query string from the current URL with JavaScript?

Sometimes, we want to obtain the query string from the current URL with JavaScript.

In this article, we’ll look at how to obtain the query string from the current URL with JavaScript.

How to obtain the query string from the current URL with JavaScript?

To obtain the query string from the current URL with JavaScript, we can use the URL constructor.

For instance, we write

const params = new URL(document.location).searchParams;
const name = params.get("name");

to get the current URL with document.location to get a URL object from the current location object.

Then we use the searchParams property to get the query string from the current URL.

Next, we call get with the query parameter key to return the value of the query parameter with the given key.

Conclusion

To obtain the query string from the current URL with JavaScript, we can use the URL constructor.

Categories
JavaScript Answers

How to generate random SHA1 hash to use as ID in Node.js and JavaScript?

Sometimes, we want to generate random SHA1 hash to use as ID in Node.js and JavaScript.

In this article, we’ll look at how to generate random SHA1 hash to use as ID in Node.js and JavaScript.

How to generate random SHA1 hash to use as ID in Node.js and JavaScript?

To generate random SHA1 hash to use as ID in Node.js and JavaScript, we can use the cryto.randomBytes method.

For instance, we write

const id = crypto.randomBytes(20).toString("hex");

to call crypto.randomBytes to create a 20 bytes buffer.

Then we call toString with 'hex' on the bytes buffer to convert it to a hex string.

Conclusion

To generate random SHA1 hash to use as ID in Node.js and JavaScript, we can use the cryto.randomBytes method.