In JavaScript values are undefined until they are initialized.
Let's break down the cases by example to help make this clear.
Referencing an Undeclared Variable
In JavaScript you must declare a variable before you may reference it. For instance,
var x;
console.log(x);
If you were to just type the following a reference error would occur.
console.log(x);
Referencing an Uninitialized Variable
Once you declare a variable, using the var keyword, it can be used in any expression, but has an initial value of undefined. For instance
var x;
x === undefined; // true
Give a Variable Type, by Giving it a Value
Once you give a variable a value, or give it any new value, you essentially let the interpreter know both the value and the type of the variable.
For instance,
var x;
typeof x; // undefined
x = 1;
typeof x; // number
x = 'a';
typeof x // string
Mixed Type Expressions
In JavaScript, if an expression is of mixed type the the interpreter will coerce one of the values to be the type of the other.
For instance,
var x = 1;
var y = 'a';
z = x + y; // '1a'
typeof z // string
The type coercion is performed on all expressions, not just +, and is not always straight forward. For instance,
[] == false // true
undefined. When converted to String, it's'undefined'.list='<li>'+arr.join('</li><li>')+'</li>'+operator coerces everything to strings if its arguments are incompatible, andundefinedis stringified to"undefined"rather than""