SgtWilliams SgtWilliams - 1 year ago 80
Javascript Question

Call a variable in an if-statement inside a function

Hello I've got the problem that I can't call the variable

in my if-statement of the
function. My Question is do I need to give my variable another scope to call it or what did I do wrong?

var imgNotSet = true;

function openImage() {
if (imgNotSet) {
var lightViewImg = document.createElement("IMG");
var imgNotSet = false;
} else {
console.log("Img already set");

Answer Source

My Question is do I need to give my variable a special scope to call it

No. You are doing that already and that is the problem.

var imgNotSet = false;

You've created another variable of the same name, but locally scoped to the function, here. Don't do that. Remove the var.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download