[thelist] Some confusions about JavaScript

Joshua Olson joshua at alphashop.net
Mon Mar 4 14:53:00 CST 2002


----- Original Message -----
From: "Syed Zeeshan Haider" <zeeshan_paki at yahoo.com>
Subject: [thelist] Some confusions about JavaScript

> I have some confusions about JavaScript.
> 1.    I many times observe following situation in JS written by others:
> if(!x){
> file://then do whatever
> }
> Here x is a variable declared by var. "!x" is confusing. Where do
> JavaScripters use such syntax of JS? Which situations need things like
> "!x"?

The ! operator means NOT.  If x is true, then !x is false.  So, if I want to
execute a block of code if x is false, then I would use the example code you
provided.

> 2.    I often use "while" instead of "for" in JavaScript, because "for"
> is not very friendly to me. Consider this example:
> for(i=0;i<10;i++)
> {
> // do whatever
> }
> Will the loop between {} start at i=0 or it will start at i=1?
> In the same way, when will the loop end? At i=10 or i=9?

The loop will execute fore values of i between 0 and 9, inclusive.  The
increment/decrement happens after the block is executed.  So, the first time
the block is executed, i will be 0, and the last time it is executed, it
will be 9.  If the boolean operator was <=, then the last time the block
executed it would be 10.

Does this help?

-joshua




More information about the thelist mailing list