JavaScript Syntax Refresher
by anandhinava
JavaScript
// This is an example of a single-line comment.
/*
* this is an example
* of a
* multi-line
* comment.
*/
// Whitespace is insignificant.
var hello = "Hello";
var world = "World!";
//Do have semicolon and curly brackets always
// Readable code is good!
var foo = function() {
for (var i = 0; i < 10; i++) {
alert(i);
}
};
foo();
// This is much harder to read!
var foo = function () {
for (var i = 0; i < 10; i++) {
alert(i);
}
};
foo();
/* reserved words:
break, case, catch, class, const, continue, debugger, default, delete, do, else, enum, export, extends, false, finally, for, function, if, implements, import, in, instanceof, interface, let, new, null, package, private, protected, public, return, static, super, switch, this, throw, true, try, typeof, var, void, while, with, yield
*/
// Valid identifier names.
var myAwesomeVariable = "a";
var myAwesomeVariable2 = "b";
var my_awesome_variable = "c";
var $my_AwesomeVariable = "d";
var _my_awesome_variable_$ = "e";
// This works:
var test = 1;
var test2 = function () {...
};
var test3 = test2(test);
// And so does this:
var test4 = 1,
test5 = function () {...
},
test6 = test2(test);
// declared but non-defined vars are "undefined"
var x;
console.log(x === undefined); // true
// Concatenation
var foo = "hello";
var bar = "world";
console.log(foo + " " + bar); // "hello world"
// Multiplication and division
console.log(2 * 3);
console.log(2 / 3);
// Incrementing and decrementing
// The pre-increment operator increments the operand before any further processing.
var i = 1;
console.log(++i); // 2 - because i was incremented before evaluation
console.log(i); // 2
// The post-increment operator increments the operand after processing it.
var i = 1;
console.log(i++); // 1 - because i was evaluated to 1 and _then_ incremented
console.log(i); // 2 - incremented after using it
// Addition vs. Concatenation
var foo = 1;
var bar = "2";
console.log(foo + bar);...