[thelist] Some confusions about JavaScript

Erik Mattheis gozz at gozz.com
Mon Mar 4 15:07:00 CST 2002


>if(!x){
>//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"?

! is the "not" and will return a boolean value the opposeite of
whatever is being negated ... like\

(!true) == false
(!false == true)

Also for null or undefined values ... if x is not defined, you can say

if (!x) {
   //code here runs if the value of x is 'undefined' or null.
}

>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?

Using while is fine, if that makes it easier to read the code you
write ... but answers:

i starts at 0
the greatest value of i in the loop will be 9
after it exits the loop the value of i will be 10.

Danny Goodman's JavaScript Bible is really good for getting your head
wrapped around JavaScript, and is a good reference as well. Dunno,
I've been recommending that book for years and everyone seems to like
it, but there might be something better now.
--

__________________________________________
- Erik Mattheis

(612) 377 2272
http://goZz.com/

__________________________________________



More information about the thelist mailing list