Export (0) Print
Expand All

Writing JavaScript Code

Like many other programming languages, JavaScript is organized into statements, blocks consisting of related sets of statements, and comments. Within a statement you can use variables, strings, numbers, and expressions.

A JavaScript program is a collection of statements. JavaScript statements combine expressions in such a way that they carry out one complete task.

A statement consists of one or more expressions, keywords, or operators (symbols). Typically, a statement is written on a single line, although a statement can be written over two or more lines. Also, two or more statements can be written on the same line by separating them with semicolons. In general, each new line begins a new statement. It is a good idea to terminate your statements explicitly. You do this with the semicolon (;), which is the JavaScript statement termination character.

Here are two examples of JavaScript statements. The sentences after the // characters are comments, which are explanatory remarks in the program.

aBird = "Robin"; // Assign the text "Robin" to the variable aBird.
var today = new Date(); // Assign today's date to the variable today.

A group of JavaScript statements surrounded by braces ({}) is called a block. Statements grouped into a block can generally be treated as a single statement. This means you can use blocks in most places that JavaScript expects a single statement. Notable exceptions include the headers of for and while loops. Notice that the single statements within a block end in semicolons, but the block itself does not.

Generally, blocks are used in functions and conditionals. Notice that unlike C++ and some other languages, JavaScript does not consider a block to be a new scope; only functions create a new scope.

In the following example, the else clause contains a block of two statements surrounded by braces. The block is treated as a single statement. Also, the function itself consists of a block of statements surrounded by braces. The statements below the function are outside of the block and are therefore not part of the function definition.

function inchestometers(inches)
   {
   if (inches < 0)
      return -1;
   else
      {
      var meters = inches / 39.37;
      return meters;
      }
   }

var inches = 12;
var meters = inchestometers(inches);
document.write("the value in meters is " + meters);

A single-line JavaScript comment begins with a pair of forward slashes (//). Here is an example of a single line comment.

aGoodIdea = "Comment your code thoroughly."; // This is a single-line comment.

A multiline JavaScript comment begins with a forward slash and asterisk (/*), and ends with the reverse (*/).

/*
This is a multiline comment that explains the preceding code statement.

The statement assigns a value to the aGoodIdea variable. The value, 
which is contained between the quote marks, is called a literal. A 
literal explicitly and directly contains information; it does not 
refer to the information indirectly. The quote marks are not part 
of the literal.
*/
NoteNote

If you attempt to embed one multiline comment within another, JavaScript interprets the resulting multiline comment in an unexpected way. The */ that marks the end of the embedded multiline comment is interpreted as the end of the whole multiline comment. This means that the text that follows the embedded multiline comment will not be commented out; instead, it will be interpreted as JavaScript code, and will generate syntax errors.

It is recommended that you write all your comments as blocks of single-line comments. This allows you to comment out large segments of code with a multiline comment later.

// This is another multiline comment, written as a series of single-line comments.
// After the statement is executed, you can refer to the content of the aGoodIdea
// variable by using its name, as in the next statement, in which a string literal is
// appended to the aGoodIdea variable by concatenation to create a new variable.

var extendedIdea = aGoodIdea + " You never know when you'll have to figure out what it does.";

The equal sign (=) is used in JavaScript statements to assign values to variables: it is the assignment operator. The left hand operand of the = operator is always an Lvalue. Examples of Lvalues are:

  • variables,

  • array elements,

  • object properties.

The right operand of the = operator is always an Rvalue. Rvalues can be an arbitrary value of any type, including the value of an expression. Here is an example of a JavaScript assignment statement.

anInteger = 3;

The JavaScript compiler interprets this statement as meaning: "Assign the value 3 to the variable anInteger," or "anInteger takes the value 3."

Be certain you understand the difference between the = operator (assignment) and the == operator (equality). When you want to compare two values to find out if they are equal, use two equals signs (==). This is discussed in detail in Controlling Program Flow.

A JavaScript expression value can be of any valid JavaScript type - a number, a string, an object, and so on. The simplest expressions are literals. Here are some examples of JavaScript literal expressions.

3.9                       // numeric literal
"Hello!"                  // string literal
false                     // boolean literal
null                      // literal null value
{x:1, y:2}                // Object literal
[1,2,3]                   // Array literal
function(x){return x*x;}  // function literal

More complicated expressions can contain variables, function calls, and other expressions. You can combine expressions to create complex expressions using operators. Examples of operators are:

+  // additon
-  // subtraction
*  // multiplication
/  // division

Here are some examples of JavaScript complex expressions.

var anExpression = 3 * (4 / 5) + 6;
var aSecondExpression = Math.PI * radius * radius;
var aThirdExpression = aSecondExpression + "%" + anExpression;
var aFourthExpression = "(" + aSecondExpression + ") % (" + anExpression + ")";
Show:
© 2014 Microsoft