javascript how to check if image exists

JavaScript
// create an XHR object
const xhr = new XMLHttpRequest();

// listen for `onload` event
xhr.onload = () => {
    if (xhr.status == 200) {
        console.log('Image exists.');
    } else {
        console.log('Image does not exist.');
    }
};

// create a `HEAD` request
xhr.open('HEAD', '/img/bulb.svg');

// send request
xhr.send();

Source

Also in JavaScript: