Welcome to WebmasterWorld Guest from 54.147.63.124

Forum Moderators: open

Message Too Old, No Replies

Assigning variables during conditional statement?

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

Junior Member

10+ Year Member

joined:Jan 26, 2006
posts: 41
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Mar 29, 2007
posts:1147
votes: 0


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]

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

Senior Member from US 

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

joined:Oct 17, 2005
posts:4966
votes: 10


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.

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

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Mar 29, 2007
posts:1147
votes: 0


Yeah, I meant expression!
8:07 pm on Sept 29, 2008 (gmt 0)

Senior Member from US 

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

joined:Oct 17, 2005
posts:4966
votes: 10


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.
10:06 pm on Sept 29, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 26, 2006
posts: 41
votes: 0


Thanks a lot guys. Very helpful!

Glenn