by Dinesh Thakur Category: JavaScript Tutorial

A JavaScript Statement is a basic programming unit, usually representing a single step in a JavaScript program. Think of a JavaScript Statement as a sentence: Just as you string sentences together to create a paragraph (or a chapter, or a book), you combine JavaScript Statements to create a JavaScript program. For example:

alert ('Hello World!');

This single JavaScript Statement opens an alert window with the message “Hello World!” in it. In many cases, a JavaScript Statement is a single line of code. Each JavaScript Statement ends with a semicolon—it’s like a period at the end of a sentence. The semicolon makes it clear that the step is over and that the JavaScript interpreter should move onto the next action.

The general process of writing a JavaScript program is to type a statement, enter a semicolon, press Return to create a new, blank line, type another JavaScript Statement followed by a semicolon, and so on and so on until the program is complete.

If multiple JavaScript Statements appear on the same line, though, you must use the semicolon to terminate each one:

var bValue = true; 
var sValue = "this is also true"

However, not explicitly terminating each JavaScript statement is a bad habit to get into, and one that can result in unexpected consequences. For instance, if you use a tool to compress the whitespace in your JavaScript code, that tool can have problems with statements that are not explicitly terminated.

The use of whitespace in JavaScript has little impact on the processing of the code, though it can impact both the readability of the code and the size of the JavaScript file. For instance, the following two lines of code are interpreted in exactly the same way:

var firstName = 'Shelley'       ;

var firstName = 'Shelley';

Other than to separate words within quotes or to terminate statements, extra whitespace—such as tabs, spaces, and newlines is disregarded. In the following code, the variable assignment completes successfully, even though line breaks separate the statement:

var

firstName=
'Shelley';

The line break isn't considered a statement terminator in this instance because JavaScript assignments require a variable name on the left, an assignment operator (=), and a value on the right. The processing application continues to process the JavaScript until either the semicolon is reached or the JavaScript Statements is completed.

The following code is also successful, because when a new variable is not assigned a value immediately, it is assigned a value of "undefined." The new variable assignment then triggers the processing application to finish processing the previous statement:

var firstName

var lastName;

In the following code, though, an error results because the JavaScript processing application recognizes that the var keyword begins a new JavaScript Statement, yet the previous statement is not complete:

var firstName =

var lastName = 'Powers';

Whitespace helps an application's readability, but it also adds to the file size. Normally, this isn't a problem as most of our JavaScript files are relatively small. However, with some larger Ajax applications and more complex JavaScript libraries, the JavaScript can actually be quite large.

The Assignment Statement

The most common JavaScript statement is the assignment statement. It's an expression consisting of a variable on the left side, an assignment operator (=), and whatever is being assigned on the right. The expression on the right can be a literal value:

nValue = 35.00;

Or, it can be variables and literals combined with any number of operators:

nValue = nValue + 35.00;

The right side of the JavaScript statement can also be a function call:

nValue = someFunction();

You can include more than one assignment on a line, as long as you separate the statements with a semicolon:

var firstName = 'Shelley'; var lastName = 'Powers';

You can also assign the same value to several variables at once:

var firstName = lastName = middleName = "";

However, with the following, where variables are lined up with commas between them, the first variable is set and the second ends up undefined:

var nValue1,nValue2 = 3; // nValue2 is undefined

You can concatenate assignments with commas, however:

var nValue1=3, nValue2=4, nValue3=5;

For readability and to ensure that no bugs creep in, I recommend that you separate your assignments with semicolons.



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.