Statement
If Statementif (2000 > 100) {
console.log('\if文/');
}
Expression Statement10 + 20 * 30;
For Statementfor (var i = 0; i < 100; i++ ) {
console.log(i);
}
While Statementvar i = 100;
while (i > 0) {
i -= 1;
}
while (true) {
i++;
if (i > 100) break;
}
LABEL: while (false) {
}
Try Statementtry {
throw "an error occured!!!";
} catch (e) {
console.log(e);
} finally {
console.log('finally...');
}
Variable Declaration
Numbervar n = 100;
String literalvar s = "this is a string literal";
var difficult_string = "hoge\n /* not a comment // here */ hoo #! baz/^[a-z]*/ ";
RegExp literalvar r = /^a[a-z][A-Z]$"/ig;
Object literalvar obj = {
s: "string hogehoge",
num: 100,
arr: [ 1000, 2000, 3000 ],
if: function () {
},
};
var ecma5obj = {
set i (n) {
console.log('setter of i called with ' + n);
if (n < 0)
throw 'invalid value for i : ' + n.toString();
this._i = n;
},
get i () {
console.log('getter of i called');
return this._i;
}
};
Assignment Expressionvar x = 50 + 50 * Math.sin(50),
y = 10,
b = true;
b |= x > 10 & x < 80;
x += 10;
y %= x++;
s = x > 3 ? "Yes" : "No";
Multi declarationvar x = 10,
i = 20 + x,
j = 30 + ++i,
r = /^abc[a-z]*/i,
foo = function () {
return 100;
},
nu = null,
self = this,
b = true,
a = [
100,
"hogehoge",
/abc/i
],
print = console.log,
$ = jQuery,
obj = new Object;
Function Declaration
Function Declarationfunction f () {
return 100;
}
Recursive Declarationfunction fact (x) {
if (x < 1) return 1;
else return x * fact(x - 1);
}
Many argumentsfunction foo (s, t, u, v) {
console.log(s);
return u;
}
Closurevar f = (function () {
var i = 0;
return function () {
i++;
console.log('i is ' + i);
return i;
};
})();