Skip to content Skip to navigation

Connexions

You are here: Home » Content » Variable declaration

Navigation

Recently Viewed

This feature requires Javascript to be enabled.
 

Variable declaration

Module by: Hannes Hirzel. E-mail the author

Summary: This module explains how variables are declared in JavaScript (ECMAScript).

Introduction

In JavaScript variables are declared with the var keyword, e.g. var theName;. You may initialize the variable at the same time, thus var theName = "John";.

If you omit the var keyword and just do introduce the variables by using them they are declared as global variables. This may happen anywhere in the code, including within functions. So use the var all the time within functions to declare local variables.

In EMCAScript5 you may use the "use strict"; directive either on the global level or within a function. It it is used within a function it only applies to that function. It means that all the variables must be declared with the var keyword.

Examples


var myObj1;  // myObj1 is initialized to undefined
var myObj2 = new Object(); // using the constructor function to initialize with an empty object
var myObj3 = {};     // literal notation for initialization with an empty object


var myString = "hello"; 
var myArray = new Array(); 

Reference

ECMAScript language specification; chapter 12.2 -- variable statement

Content actions

Download module as:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks