Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> JavaScript >> Snippet

05 Data types / variables


Variables in JavaScript
- are case - sensitive
- must start with letter or underscore or dollar sign
- do not have to be declared before using them
- no limit on length
- cannot use reserved words
- four types :  string, number, boolean , object
- can be assigned a different type at any time
- four special values : True, False, Undefined, Null

Variables can be created in two ways - declaration or assignment

Create by assignment :
newVariable = 5   //create a variable simply by assigning it
newVariable = "dog"   //variables can be any type
newVariable = 'dog'    //single or double quotes can be used on strings


Create by declaration using var :
var  X
var X, Y   //note the comma between the two variables

- var limits scope to function where created
- var creates a variable that is location to the function in which it is used
- var in main body of script create variable that is available to all scripts on the page
- var in main body cannot be accessed in a function


Create by BOTH declaration and assignment :
var X = 6
var X = 6,Y = //note the comma between the two declarations


if ( typeof myvariable = "number" ) ...

Undefined - variable created but no value assigned

Special characters - escape sequences - are used when the context of the
string doesn 't allow the usual character.  JavaScript special characters start
with a backslash.  Common ones are :

\b backspace
\f form feed
\n newline
\r carriage return
\t tab
\ ' single quote
\ " double quote