How to get an image natural height with JavaScript?

Sometimes, we want to get an image natural height with JavaScript.

In this article, we’ll look at how to get an image natural height with JavaScript.

How to get an image natural height with JavaScript?

To get an image natural height with JavaScript, we can use the naturalHeight property.

For instance, we write:

<img src='https://picsum.photos/200/300'>

to add an img element.

Then we write:

const image = document.querySelector('img')
console.log(image.naturalHeight)

to select the img element with querySelector.

And then we use the image.naturalHeight property to get the natural height of the image.

Conclusion

To get an image natural height with JavaScript, we can use the naturalHeight property.