Separate lines:
Statement 1
Statement 2
Separated by semi-colons:
Statement 1;Statement 2
Both:
Statement 1;
Statement 2;
A statement that isn't executed.
Statement 1;
// comment
Statement 2;
A label that stands for a value, even if the value changes.
Numbers, strings, Boolean values:
var age = 34;
var name = "Jeremy";
var name = 'Jeremy';
var height = '5\'10"';
var male = true;
JavaScript is a weakly typed language
var foo = 37.5;
var foo = "bar";
A variable that is a collection of variables.
var fruit = new Array();
fruit[0] = "apple";
fruit[1] = "orange";
fruit[2] = "banana";
Combine declaring and setting:
var fruit = new Array("apple","orange","banana");
The length of an array.
var fruit = new Array("apple","orange","banana");
alert(fruit.length);
Alert: 3
var fruit = new Array("apple","orange","banana");
alert(fruit[0]);
Alert: apple
for (initial condition; test condition; change operation)
var fruit = new Array("apple","orange","banana");
for (var i=0; i<fruit.length; i++) {
alert(fruit[i]);
}
Alert: apple
Alert: orange
Alert: banana
A self-contained series of statements
function functionName (arguments) {
Statements;
}
function multiply(x,y) {
var result = x * y;
return result;
}
var mynum = multiply(2,6);
alert(mynum);
Alert: 12
function multiply(x,y) {
var result = x * y;
return result;
}
function multiply(x,y) {
result = x * y;
return result;
}
result = 10;
var mynum = multiply(2,6);
alert(mynum);
alert(result);
Alert: 12
Alert: 12
Self-contained bundles of functions and variations.
Dot syntax:
object.property
object.method()
Math
object
Math.round(3.14);
The round
method of the Math
object.
Array
object
Array.length;
The length
property of the Array
object.
window
object
window.open("http://www.example.com/");
The open
method of the window
object.
document
object