<body>
<div id='heroImage'><img src='' alt=''></div>
<button id='newHero'>Get New Hero</button>
<script src="script.js"></script>
</body>
const img=''
let getSuperHero=(id)=>{
const newHeroDiv=document.getElementById('newHero')
const heroImageDiv=document.getElementById('heroImage')
let img;
fetch(`https://superheroapi.com/api.php/5930076870393724/${id}`)
.then(response => response.json())
.then(json => {
img=json.image.url
console.log(img)
})
}
getSuperHero(300)
document.querySelector('body').innerHTML += `<img src= '${img}' height=300 width=300>`
This outputs a broken images.I know it works if I write the last line inside getSuperHero() function.But as 'img' is declared outside of the function shouldnt it work here as well.