Richard G Baldwin (512) 223-4758,,

JavaScript Programming, Expressions

Java/Web Programming, Lecture Notes # 2110, Revised 05/23/98.


The material was prepared as part of a new tutorial that I plan to publish on Web programming in general.  It is not part of the Java academic curriculum at ACC.

The purpose of this series of lessons is to teach you how to program in JavaScript.  The goal is to provide a programming tutorial that will be accessible to persons with no programming experience and will be equally useful to persons who already have programming experience using JavaScript or some other language.



In JavaScript and many other programming languages as well, literal values, variables, function calls, and operators are combined to create expressions.  Expressions are combined to create statements.  Statements are combined to create programs.

Earlier lessons have discussed function calls, literal values, and variables.  Now we need to take a closer look at how they may be combined with operators to create expressions.

This lesson will concentrate on expressions.  A subsequent lesson will concentrate on operators.


An expression is any valid combination of literals, variables, operators, function calls and sub-expressions that evaluates to a single value.

The resulting value can be a number, a string, or a logical (boolean) value.

Expressions can be very simple or very complex, depending on the need.

Some expressions are used to assign a value to a variable.  For example, the following box shows two expressions that assign a value to the variable named myVar.  The first expression is a simple assignment which assigns a literal value to a variable.  The second expression is much more complex.
myVar = 10

myVar = ((6 + x) + myFunction(z))/Q
Sometimes the result of evaluating an expression is not assigned to a variable.  These might be referred to an anonymous expressions in keeping with the general use of the term in other areas of programming.  An anonymous expression simply evaluates to a value and returns that value to be used in the larger overall expression in which it is included.  Since it is never assigned to a variable, it is never given a name; hence the use of the term anonymous.

For example, the second expression from above is repeated below.
myVar = ((6 + x) + myFunction(z))/Q
This overall expression includes the anonymous expression highlighted in boldface. The anonymous expression will be evaluated and its value will be used to evaluate the larger overall expression, but the value obtained by evaluating the anonymous expression won't be saved anywhere.  As soon as the value is used, it will cease to exist.

JavaScript supports three types of expressions which match the three types of data described in an earlier lesson.

  1. Arithmetic expressions evaluate to a numeric value such as 6.5.
  2. String expressions evaluate to a string such as "Dick Baldwin" or "78759".
  3. Logical expressions evaluate to the boolean type with possible values of  true or false.
It is important to note that strings containing digits are not the same as numeric values.  The above example shows a zip code as a string.  You can do arithmetic with numeric values.  You cannot do arithmetic with strings even if they consist only of the digit characters.

According to Netscape, there are a couple of subtle issues involving expressions.

The special keyword null denotes a null value. This is in contrast to variables that have not been assigned a value.  Such variables are undefined and will cause a runtime error if used as numbers or as numeric variables.

On the other hand, array elements that have not been assigned a value evaluate to the boolean value false.  We will discuss this in more detail when we discuss arrays.

Most of the lessons in this tutorial will contain one or more sample scripts that illustrate the material in this lesson.  However, we will forego the sample script in this lesson because virtually every other sample script in every other lesson will contain one or more expressions.  There will be no shortage of sample scripts containing expressions.