Evaluating Expressions

We've already talked about expressions--single lines of script that give values to variables or objects or other data containers, or manipulate those data containers using methods (like a sentence or a noun-adjective combination). When we run those expressions in JavaScript, the software evaluates the expressions and plugs the proper values into the data containers.

For example, if we declare and initialize a variable:

var myAge = 28

the software knows that the value of myAge is 28. When we use that variable, JS evaluates its value and properly uses it. So we can then write:

myAge -10

and the software should correctly evaluate the statement as 18.

Now this is really nifty: Netscape has a special feature where you can evaluate an expression before you load it into an HTML page and run it. In other words, you can write one line of code and then test it without going through the whole process. Here's how to do it:

  1. While in your Netscape browser (not the Editor), pull down to Open Location under File
  2. type javascript: into the textbox
  3. Click on Open in Browser
  4. The screen will split in two, horizontally. In the text box in the bottom, type the expression you want to test, then hit RETURN.
  5. The results (or an error) should appear in the upper window of the screen.

Try it. Go through these steps, and then enter var myAge = 28, hit RETURN, then type myAge - 10 and hit RETURN. You should then see 18 in the upper screen.