How to Open an Image in JavaScript

Charles Jul 17, 2026

Handling images in JavaScript is a common task, often employed to display images dynamically or manipulate them using various libraries and APIs. Knowing how to open an image in JavaScript is a fundamental skill that enables a multitude of web development use cases.

Create 360 View Image Rotation Slider Using HTML CSS and JavaScript
Create 360 View Image Rotation Slider Using HTML CSS and JavaScript

In this guide, we'll delve into the intricacies of working with images in JavaScript, exploring the JavaScript Image object, and demonstrating how to use the key methods to open and display images. Let's get started!

Tips for css style Animation
Tips for css style Animation

The JavaScript Image Object

The JavaScript Image object is used to display images on a web page. It's created using the HTML tag or the new Image() constructor.

Javascript Add Text To Image
Javascript Add Text To Image

Let's first create an Image object using the JavaScript Image() constructor:

<script>
  let img = new Image();
</script>

Setting Image Sources

CSS image slider
CSS image slider

Once you've created an Image object, you can set the image source using the `src` property.

Here's how to set an image source in JavaScript:

<script>
  let img = new Image();
  img.src = 'path/to/my/image.jpg';
</script>

Loading and Error Handling

The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had

When you set the `src` property, the image starts to load asynchronously. If there's an error during this process, you won't know about it unless you add an event listener for the `onerror` event.

Here's how to handle loading and error events:

<script>
  let img = new Image();
  img.src = 'path/to/my/image.jpg';

  img.onload = function() {
    console.log('Image loaded successfully');
  };

  img.onerror = function() {
    console.error('Error loading image');
  };
</script>

Displaying Images

a computer screen with an image of a person flying through the air in front of stars
a computer screen with an image of a person flying through the air in front of stars

Once you've created an Image object and set its source, how do you display it on the web page?

You can use the `appendChild()` method of the DOM object to add the image to the document.

Javascript layer animation
Javascript layer animation
Learn JavaScript in 30 Days
Learn JavaScript in 30 Days
JavaScript Tips to Change Border Radius & Background Color
JavaScript Tips to Change Border Radius & Background Color
Learn JavaScript Step by Step
Learn JavaScript Step by Step
JavaScript Basics | Chapter 1 📖 | Part 1
JavaScript Basics | Chapter 1 📖 | Part 1
Learn JavaScript in the Right Order 🚀
Learn JavaScript in the Right Order 🚀
Embedding Images in HTML using <img>
Embedding Images in HTML using <img>
Image Hover Animation in HTML & CSS
Image Hover Animation in HTML & CSS
JavaScript Fundamental
JavaScript Fundamental

Using the canvas Element

You can also use the canvas element to draw images dynamically. Here's how:

<script>
  let canvas = document.getElementById('myCanvas');
  let ctx = canvas.getContext('2d');

  let img = new Image();
  img.src = 'path/to/my/image.jpg';

  img.onload = function() {
    ctx.drawImage(img, 0, 0);
  };
</script>

Using Data URLs

Data URLs allow you to embed images directly in your JavaScript code. Here's how to use a Data URL to create an image:

<script>
  let img = new Image();
  img.src = 'data:image/png;base64,iVBORw0KG...';
</script>

Data URLs are useful when you want to display small, simple images, but they can quickly inflate the size of your JavaScript bundle for large or complex images.

Conclusion

Understanding how to open an image in JavaScript is the first step towards working with images in web development. Whether you're drawing graphs, displaying user-uploaded images, or creating interactive visualizations, the JavaScript Image object is a powerful tool at your disposal.

Now that you've mastered the basics, you're ready to delve into more advanced topics like image manipulation with libraries like canvas and pixi.js. Happy coding!