Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Expressing yourself in JavaScript

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

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
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!).

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:

  • If the value in the variable and the value to append are numbers, += adds the values.
  • If the value in the variable and the value to append are strings, += combines them into one long string.


Here's how the += operator works when the values in question are numbers:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources