JavaScript – Declaration Statements

The var and function are declaration
statements
—they declare or define variables and functions.
These statements define identifiers (variable and function names) that
can be used elsewhere in your program and assign values to those
identifiers. Declaration statements don’t do much themselves, but by
creating variables and functions they, in an important sense, define
the meaning of the other statements in your program.

The subsections that follow explain the var statement and the function statement, but do not cover
variables and functions comprehensively. See Variable Declaration and Variable Scope
for more on variables. And see Chapter 8 for
complete details on functions.

var

The var statement declares
a variable or variables. Here’s the syntax:

var name_1 [ = value_1] [ ,..., name_n [= value_n]]

The var keyword is followed
by a comma-separated list of variables to declare; each variable in
the list may optionally have an initializer expression that
specifies its initial value. For example:

var i;                                        // One simple variable
var j = 0;                                    // One var, one value
var p, q;                                     // Two variables
var greeting = "hello" + name;                // A complex initializer
var x = 2.34, y = Math.cos(0.75), r, theta;   // Many variables
var x = 2, y = x*x;                           // Second var uses the first
var x = 2,                                    // Multiple variables...
    f = function(x) { return x*x },           // each on its own line
    y = f(x);

If a var statement appears
within the body of a function, it defines local variables, scoped to
that function. When var is used
in top-level code, it declares global variables, visible throughout
the JavaScript program. As noted in Variables As Properties,
global variables are properties of the global object. Unlike other
global properties, however, properties created with var cannot be deleted.

If no initializer is specified for a variable with the
var statement, the variable’s
initial value is undefined. As
described in Function Scope and Hoisting, variables are defined
throughout the script or function in which they are declared—their
declaration is “hoisted” up to the start of the script or function.
Initialization, however, occurs at the location of the var statement, and the value of the
variable is undefined before that
point in the code.

Note that the var statement
can also appear as part of the for and for/in loops. (These variables are
hoisted, just like variables declared outside of a loop.) Here are
examples repeated from Variable Declaration:

for(var i = 0; i < 10; i++) console.log(i);
for(var i = 0, j=10; i < 10; i++,j--) console.log(i*j);
for(var i in o) console.log(i);

Note that it is harmless to declare the same variable multiple
times.

function

The function keyword is
used to define functions. We saw it in function definition
expressions in Function Definition Expressions. It can also be used
in statement form. Consider the following two functions:

var f = function(x) { return x+1; }  // Expression assigned to a variable
function f(x) { return x+1; }        // Statement includes variable name

A function declaration statement has the following
syntax:

function funcname([arg1 [, arg2 [..., argn]]]) {
      statements
}

funcname is an identifier that
names the function being declared. The function name is followed by
a comma-separated list of parameter names in parentheses. These
identifiers can be used within the body of the function to refer to
the argument values passed when the function is invoked.

The body of the function is composed of any number of
JavaScript statements, contained within curly braces. These
statements are not executed when the function is defined. Instead,
they are associated with the new function object for execution when
the function is invoked. Note that the curly braces are a required
part of the function statement.
Unlike statement blocks used with while loops and other statements, a
function body requires curly braces, even if the body consists of
only a single statement.

Here are some more examples of function declarations:

function hypotenuse(x, y) {
    return Math.sqrt(x*x + y*y);  // return is documented in the next section
}

function factorial(n) {           // A recursive function
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

Function declaration statements may appear in top-level
JavaScript code, or they may be nested within other functions. When
nested, however, function declarations may only appear at the top
level of the function they are nested within. That is, function
definitions may not appear within if statements, while loops, or any other statements.
Because of this restriction on where function declarations may
appear, the ECMAScript specification does not categorize function
declarations as true statements. Some JavaScript implementations do
allow function declarations to appear anywhere a statement can
appear, but different implementations handle the details differently
and placing function declarations within other statements is
nonportable.

Function declaration statements differ from function
definition expressions in that they include a function name. Both
forms create a new function object, but the function declaration
statement also declares the function name as a variable and assigns
the function object to it. Like variables declared with var, functions defined with function
definition statements are implicitly “hoisted” to the top of the
containing script or function, so that they are visible throughout
the script or function. With var,
only the variable declaration is hoisted—the variable initialization
code remains where you placed it. With function declaration
statements, however, both the function name and the function body
are hoisted: all functions in a script or all nested functions in a
function are declared before any other code is run. This means that
you can invoke a JavaScript function before you declare
it.

Like the var statement,
function declaration statements create variables that cannot be
deleted. These variables are not read-only, however, and their value
can be overwritten.

Comments are closed.