JavaScript Kit > JavaScript Reference > Here

JavaScript Statements

Last updated: January 4th, 2013

JavaScript Statements enable you to implement logic, looping, and more within your script.

Related Tutorials

JavaScript Statements

Statements Description

JavaScript 1.5 feature, NOT supported in IE as of IE10

Defines a read-only constant whose value cannot be changed (unlike a variable using "var"). It also cannot be re-declared once initially defined:

const mysalary = 3000 //start with a letter or underscore, then alphabetic, numeric, or underscore characters

const follows the same rules when it comes to scope as variables, meaning a const defined inside a function is only visible to code within the function:

( function(){ // execute function
 const width = 5 // define const inside function
}) ()

alert( width ) // error: undefined

var The var statement is used to explicitly declare a variable in JavaScript. While technically  you do not have to use the var statement in front of a variable to create it, it is good programming practice. Once a variable is declared, you can continue referencing that variable without the var statement in front of it. For example:

var x=5
x=x+10 //x now contains 15

You can declare multiple variables at once by separating each declaration with a comma:

var x, y=10, z=25, dog="Spotty"

Variable names in JavaScript are case sensitive, so "mydog" and "MYDOG" are considered two different variables in JavaScript.

-Local versus Global Scope

When you use the var statement outside of any functions, the variable declared is added to the global scope, making it a global variable accessible anywhere inside the page. You can reference this variable inside a function and change it as well from within the function.

When you use the var statement inside of a function, however, you are creating a local variable that's only visible from within the function. Any changes to this variable is contained within the function only.

-Copying variables- by value or by reference?

In JavaScript, when you copy one variable to another, you are either:

  1. Copying the value of the original variable to the new variable, creating a 2nd copy of it in the process, or:
  2. Creating a reference to the original variable, so the new variable merely acts as a pointer to the original. Modifying the new variable inexplicitly also modifies the original variable.

The general rule in JavaScript is that primitive variable types (Numbers, Booleans and Strings) are manipulated by value, where as reference type variables (Arrays, objects, and functions) are manipulated by reference. This means if you copy an array to a new variable, you are in fact merely creating a new pointer to the original array. Changes to the new variable will affect the original array as well. For example:

var x=[1,2,3]
var y=x //"copy" variable x to variable y, which merely creates new pointer
y[0]=4 //change 1st element of array y to 4
alert(x[0]) //alerts 4. Array x's first element is changed to 4 as well!

In the above I copy array x to a new variable y, then change array y's first element to 4. Since arrays are reference types, doing so also inexplicitly changes array x's first element to 4 as well.

with "with" is a time saving statement that allows you to "cache" an object, so for statements that follow, you no longer need to reference the object again when referring to it.


with (object)


with (document.form1){

In the above example, the object "document.form1" is cached, so "field1" actually refers to "document.form1.field1".

Conditional Statements

Looping Statements

Reference List

Right column

CopyRight 1998-2012 JavaScript Kit. NO PART may be reproduced without author's permission.