Sunday, January 6, 2008

First Post on blogger.

The blog on jewishmusic.net is down for a bit, and I figured if I don't write stuff down they'll be forgotten, so we'll start with a thought on evaluations:

When evaluating terms inside a parenthesis, JS (or any other language) looks for a true or false value. For example, in the following conditional, the browser will alert 'true':
if (true) alert ('true');
Where the value is not a Boolean, JS must convert it to one. Generally, anything besides false, 0, and an undefined variable evaluates to true.
so (4), (-1), and ('potatoes') are the same as (true).

There is a common mis-belief that when given a statement, JS will return true if it successfully carries out the statement, and false if it does not. This is NOT correct. Instead JS will carry out the statement, and than evaluate the results.

So that in truth (2+4) will return true, since it is the same as (6).
(4-4) will return false, since it is the same as (0).

if (val = false) will evaluate to false and not run, since it is read the same as:
val = false; if(val)...
JS first assigns false to the val variable, and then evaluates the val variable.

Many, many a programmer gets stuck on this, so file it somewhere useful, when the code acts unexpectedly.

1 comment:

AGG said...

I am trying to but some music on jewishmusic.net but when I try and create an account I get an error message. Can you help?