Get the Dimensions of an Image in JavaScript

This is a preview lesson
Register or sign in to take this lesson.

This tutorial show how to get dimensions of an image. In JavaScript we can achieve this by using the Image class.

Example

const img = new Image();

img.src = 'https://i0.wp.com/scanskill.com/wp-content/uploads/2022/04/cropped-Screen-Shot-2022-04-26-at-10.47.13.png';

img.onload = function() {
    console.log("The width of the image is: ", this.width, " and the height is: ", this.height);
}
Output
The width of the image is: 888 amd the height is: 161