JavaScript Program to Get the Dimensions of an Image

Example: Get Dimensions of an Image

// program to get the dimensions of an image

const img = new Image();

// get the image
img.src = '//cdn.programiz.com/sites/tutorial2program/files/cover-artwork.png';

// get height and width
img.onload = function() {
  console.log('width ' + this.width)
  console.log('height '+ this.height);
}

Output

width 1040
height 922

In the above program, new Image() constructor is used to create an image object.

The Image() constructor creates a new image element instance.

img.src is then used to add the image using an image URL source.

The img.onload() function is used to access the height and width of the image.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges