homepage Welcome to WebmasterWorld Guest from 54.211.7.174
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Assigning variables during conditional statement?
pillsbur




msg:3754784
 7:28 pm on Sep 29, 2008 (gmt 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

 

Dabrowski




msg:3754921
 7:59 pm on Sep 29, 2008 (gmt 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]

Fotiman




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

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




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

Yeah, I meant expression!

Fotiman




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

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




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

Thanks a lot guys. Very helpful!

Glenn

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved