Welcome to WebmasterWorld Guest from 54.167.85.221

Forum Moderators: open

Message Too Old, No Replies

Assigning variables during conditional statement?

     

pillsbur

7:28 pm on Sep 29, 2008 (gmt 0)

5+ Year Member



Hi,

This is purely a procedural question regarding the rules of javascript (and programming in general).

In my code I had the following:

if ((bookInfo) ¦¦ (Format = "Electronic Book")) {
// Do something
}

Note the single equals sign. For my script, the single equal sign is a typo; I should have written (Format == "Electronic Book"). However, even with the single equal sign, the script continued to run and did not result in a syntax error. Instead, the value of Format was being changed to "Electronic Book" whenever bookInfo was undefined! I didn't think variables could be assigned new values in the midst of a conditional statement, but that's exactly what was happening.

Can anyone speak to this strangeness? Have you seen this before or used this technique for something?

Thanks,
Glenn

Dabrowski

7:59 pm on Sep 29, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Anything in brackets is a statement. That is, telling the browser to execute that code and return the result.

So in this line, there are 3 statements...

((bookInfo)  (Format = "Electronic Book"))

The first, and ultimate statement is the contents of the entire 'if'. If it returns true the code will be executed.

Now lets break it down...

(bookInfo)  (Format = "Electronic Book")

This is 2 statements, separated by an OR. Essentially this means try them in order until one is true, or the whole statement will be false.

The first part...

(bookInfo)

This will evaluate if bookInfo is true or not. If it is, the OR statement returns true and the second part is not evaluated.

If it returns false, or in your case undefined, the second part is evaluated...

(Format = "Electronic Book")
(with one =)

Remember I said it means 'do this code'. Well, in this case the code is telling it to set Format to the value "Electronic Book". If it sucessfully does that, which it always will in this case, that statement will return true.

That in turn makes the OR statement true, and in turn the IF statement true.

You can see the effect if you draw it up in a table:

bookInfo....Format....OR......IF 
TRUE........N/A.......TRUE....TRUE
FALSE.......TRUE......TRUE....TRUE

You see, it can never return FALSE.

Now, the == operator is as you know a comparison. So that changes the affect of the second statement in the OR to evaluate whether the 2 values are the same or not, and return TRUE or FALSE accordingly.

See how this effects the table:

bookInfo....Format....OR......IF 
TRUE........N/A.......TRUE....TRUE
FALSE.......TRUE......TRUE....TRUE
FALSE.......FALSE.....FALSE...FALSE

I bet that's the most long winded explanation of a simple IF statement, but I hope it helps you understand why your typo affected your code in that way.

[edited by: Dabrowski at 8:05 pm (utc) on Sep. 29, 2008]

Fotiman

7:59 pm on Sep 29, 2008 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



An assignment is in fact an expression. The value of an assignment expression is the value of the right hand side of the assignment. So in your example, you are doing the equivalent of this:

Format = "Electronic Book";
if (bookInfo ¦¦ Format) {
// Do something
}

A non-empty string will evaluate to true, therefore the code inside this conditional block will always execute.

Dabrowski

8:05 pm on Sep 29, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Yeah, I meant expression!

Fotiman

8:07 pm on Sep 29, 2008 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Actually, I had a mistake in my example, and Dabrowski had a mistake in his.

Dabrowski was correct, Format will only be assigned the value of "Electronic Book" if bookInfo evaluates to false (because if it's true, the right hand side of the OR does not get evaluated). So my example where I have Format = "Electronic Book" before the if statement is not technically correct. This would be a better representation:


if (bookInfo) {
// Do something
}
else {
Format = "Electronic Book";
if (Format) {
// Do something
}
}

Now, what Dabrowski got wrong was this:


If it sucessfully does that, which it always will in this case, that statement will return true.

It's important to note that it does not return true, but rather it evaluates to true. If you had assigned an empty string value to Format, the assignment would still be successful, but it would evaluate to false.

pillsbur

10:06 pm on Sep 29, 2008 (gmt 0)

5+ Year Member



Thanks a lot guys. Very helpful!

Glenn

 

Featured Threads

Hot Threads This Week

Hot Threads This Month