Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
JavaScript Program to Get the Dimensions of an Image
In this example, you will learn to write a JavaScript program that will 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 = '/sites/default/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?