SPS Home    >   Dgreath    >   JavaScript    >   Expressions

JAVASCRIPT EXPRESSIONS

The following information describes JavaScript expressions.

JAVASCRIPT EXECUTION BLOCKS
(BLOCK)

JavaScript statements consist of one, two, or possibly three properly formed expressions. An expression consists of an operator and one or two operands and perform either assignment, comparison, or computation tasks. Operands can be either literal values or variables. Literals and variable values can be Boolean, numerical, or string types. Boolean types have truth values of 'true' or 'false'. Numerical types can be positive, negative, or zero either real or integer. String types are essentially an array of displayable characters capable of being displayed as a group or parsed out individually by the use of String functions.

Since JavaScript is not a typed language, it is imperative to ensure that appropriate operands are used in each expression. So, for example, a number can be added to a number but if the number is quoted, it becomes a string. Since a string cannot be added to a number, the expression will fail. However, a number can be compared to a string representation of the number. One of the  most common problems in JavaScript is type mismatching.

An expression can also be a complete statement as well thus providing the capability to nest statements within statements.

Assignment Expressions:
Assignment expressions are used to get a value into a variable, whether numerical, string, or Boolean. JavaScript supports two assignment scopes--public and private. When a variable is declared with a var statement, its scope is private to the function. This means that it can only be referenced by expressions within the current set of curly braces. When it is declared without the var statement, its scope is considered public and can be accessed by expressions both within and outside the current set of curly braces. Curly braces are JavaScript's scope delimiter. Function arguments and return values work differently, please see the article of function definition. Variable scoping errors are another common problem that crops up and takes one of two forms. First, an external function needs to access a private variable and can't or second, two functions reference the same public variable.

Numerical Expressions:
Numerical expressions can operate on two literals (i.e 2 + 3), two variables (i.e. myVar + yourVar) or a literal and a variable (i.e. myVar + 4). When working with variables, be sure that the value is truly a number especially when user or external input is involved. The JavaScript NaN value can be used in a conditional expression to test for non numerical values to throw an error. Also, the isNaN() method will detect non-numeric values, the Number() function converts a object's value to a number, the parseFloat() method returns a floating point number from a string, and the parseInt() method returns an integer from a string. 

Statement Expressions:
Any valid and contextually appropriate JavaScript statement construct can be used as an expression in another statement to provide statement nesting.


JAVASCRIPT CONDITION BLOCKS
(CONDITION)

Conditional Expressions:
Conditional expressions are almost always enclosed in parenthesis (i.e. (a == b) ) and evaluate to a Boolean truth value. The logical operators can be combined with the conditional operators to construct complex conditions.


JAVASCRIPT LABEL BLOCKS
[label]
JavaScript allows statements to be labeled as targets for break and continue statements used in for and while loops. To label a statement, precede the statement by the label and a colon (i.e. begin:somestatement).  The colon (:) operator delimits the label from the statement. Labels must conform to the general rules for JavaScript identifiers.

JAVASCRIPT SCOPE
Scope:
The scope of a JavaScript expression is delimited by the opening curly brace ( "{"  )and closing curly brace ( "}" ). Statements within a scope are executed as block. The semi-colon ( ";" ) character is used as a separator between statements coded within a block regardless of whether the block is written horizontally or vertically (see below). Variables declared with a var statement are private to the containing scope and can only be referenced within that scope, whereas those declared without the var statement are public with respect to the scope and can be reference both within and without the scope.

The following examples illustrate the proper stylistic use of scope in both horizontal or vertical formatted code. Note that when whitespace is ignored, either method presents the identical code to the compiler/assembler.

Zero Statements (empty scope):
Horizontal:
{ }
Vertical:
{
}
Single Statements:
Horizontal:
{ stmt1 }
Vertical:
{
  stmt1
}
Two Statements:
Horizontal:
{ stmt1 ; stmt2 }
Vertical:
{
  stmt1;
  stmt2
}
Three Statements:
Horizontal:
{ stmt1 ; stmt2 ; stmt3 }
Vertical:
{
  stmt1;
  stmt2; 
  stmt3
}
Nested Statements:
Horizontal:
{ stmt1 ; stmt2{stmtA ; stmtB ; stmtC} ; stmt3 }
Vertical:
{
  stmt1;
  stmt2
  {
    stmtA;
    stmtB;
    stmtC
  };
  stmt3
}