yes basically img is not defined at first, so either
- initialize
imginsetupby assiging one of the images - or skip
image()ifimgis not defined
The latter is what Chrisir suggested, but strictly speaking, when a variable is not defined yet, it is undefined so you should test like img !== undefined