Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Expressing yourself in JavaScript

Find out how you can use expressions to make your scripts think on their own

  • Print
  • Feedback

Expressions tell JavaScript what you want to do with information you've provided. An expression consists of two parts: one or more values, called operands, and an operator that tells JavaScript what you want to do with the operands.

This explanation may sound complex, but as you will soon see, an expression is really nothing more than a simple formula for addition. This column addresses the use of expressions in JavaScript, both for defining the content of variables, as well as for creating more elaborate schemes using other JavaScript constructs. Used in this way, expressions provide a way for your scripts to think on their own (although they may seem to act on their own more than you'd like them to!).

Learning JavaScript

This article is part of the JavaWorld technical content archive. You can learn a lot about JavaScript programming by reading articles in the JavaScript series, just keep in mind that some of the information is likely to be outdated. See "Using JavaScript and forms" and "Using JavaScript's built-in objects" for more about programming with JavaScript.

As with all modern programming languages, JavaScript expressions can be divided into several distinct categories: assignment, math, and relational. Assignment expressions assign values to variables; math expressions apply to number values only, with one exception, which we'll get into later on; and relational expressions apply to numbers and, in some cases, strings.

We're going to be examining each of these categories in detail, so we better get started. We'll begin with the assignment expressions and the operators used within these expressions.

Assigning values to variables

The following table describes the assignment operators. You will probably use the = assignment operator for the bulk of your JavaScript programs, but it's nice to know the others are available in case you need them.

Operator Function
= Assigns a value to variable (for example, Var=1)
+= Adds a value to a value already in a variable (for example, Var+=1)
-= Subtracts a value from a value already in variable (for example, Var-=1)
*= Multiplies a value with value already in a variable (for example, Var*=1)
/= Divides a value with a value already in a variable (for example, Var/=1)
%= Divides a value with a value already in a variable; returns the remainder (for example, Var%=1)

Note that JavaScript supports additional operators for bitwise operations. We'll discuss bitwise operators later in this column.

The first operator in the table, the = assignment operator, lets you assign a new value to variable. If the variable previously contained a value, that value is replaced. Take a look at the following examples:

MyStringVar = "This is a string" // assign text to variable 
MyNumverVar = 100 // assign number to variable 
MyObjectVar = document.form[0] // assign document.form[0] object to variable

The remaining operators in the table are called shorthand assignment operators. These operators let you add, subtract, multiply, and divide values to values already in a variable. The most commonly used shorthand assignment operator, +=, can be used in one of two ways:

  • Print
  • Feedback