Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with defining JavaScript variables within if blocks?

I have some code like this:

if (condition) {
var variable = blah;
}

if (differentcondition) {
var variable = blah;
}

Is this correct?

I assume that the variable wouldn't be assigned if the condition doesn't return true.

JSLint keeps on telling me, variable already defined.

Am I doing this wrong?

Thanks.

OK, Here's my actual usecase, i'm doing event delegation like this:

$("#container").click(function (event){ 

    if ($(event.target).is('img.class1')) {
        var imagesrc = $(event.target).attr('src');
        // Do something with imagesrc
    }

    if ($(event.target).is('img.class2')) {
        var imagesrc = $(event.target).attr('src');
        // Do something with imagesrc
    }

    // This condition is mutually exclusive to the above 2
    if ($(event.target).is('img.class3')) {
        var imagesrc = $(event.target).attr('src');
        // Do something with imagesrc
    }

    // This condition is mutually exclusive to 1 and 2 but not to 3
    if ($(event.target).is('img.class4')) {
        var imagesrc = $(event.target).attr('src');
        // Do something with imagesrc
    }

});

Actually these 2 classes are not mutually exclusive.

This works for me, but is it correct?

The answers were very informative, but I still don't understand how I should set up the variables here.

Actually I also want to say that certain conditions are mutually exclusive, and certain conditions are not.

How should I structure this?

I probably should've used this example from the beginning.

like image 898
Mark Avatar asked Jul 22 '09 01:07

Mark


People also ask

Can you define a variable inside an if statement JavaScript?

Can you define variables inside an IF statement? What I did there, was re-define a JavaScript variable inside the IF statement. But you can create a new variable right inside the IF statement parenthesis.

What is the correct way to define a variable in JavaScript?

You declare a JavaScript variable with the var or the let keyword: var carName; or: let carName; After the declaration, the variable has no value (technically it is undefined ).

What are block variables in JavaScript?

Block scoped variables: A block scoped variable means that the variable defined within a block will not be accessible from outside the block. A block can reside inside a function, and a block scoped variable will not be available outside the block even if the block is inside a function.

What happens when you don't declare a variable in JavaScript?

If you attempt to read the value of an undeclared variable, JavaScript generates an error. If you assign a value to a variable that you have not declared with var , JavaScript implicitly declares that variable for you.


1 Answers

Because javascript has something called "Hoisting" which makes your code do things it doesn't look like it should be doing. Basically, that means a javascript interpreter will move all var declarations, regardless of where they are in the body of a function, to the top of the function body. Then it will move all function definitions to the top, just under all the vars. Then it will finish compiling the function.

Putting a var inside an if statement is not against "the rules" of the language, but it means that, because of var hoisting, that var will be defined regardless of whether the if statement's condition is satisfied.

Keep in mind also that hoisting does not include the assignment, so as others have pointed out, the var declarations will be moved to the top, and left undefined until they're assigned later.

This is an example of a feature that must have seemed like a good idea at the time, but it has turned out to be more confusing than helpful.

like image 76
Breton Avatar answered Sep 20 '22 05:09

Breton