JavaScript declaration and assignment

JavaScript. Multiple variables can be declared on the same line, separated by comma var name1, name2, name3 …;

// declare variable, use 「var」
var x;
var y;

// multiple variables can be declared on the same line, separated by comma
var a, b, c;

Multiple variables can be declared and assigned at the same time.

// declare variable and assign
var x = 4;

// declare variable and assign multiple variables
var a = 1, b = 2, c = 3;

When a variable is declared but not assigned, the default value is undefined.

When a variable is not declared nor assigned, and you try to access it, you get ReferenceError.

var x;
console.log(x); // prints 「undefined」
console.log(y); // ReferenceError: y is not defined

Popular posts from this blog

Browser User Agent Strings 2012

11 Years of Writing About Emacs

does md5 creates more randomness?