user interaction
...
interpreted
free
<head> // or anywhere
<script src=”utils.js”></script>
</head>
weakly typed language
whole numbers
fractional numbers
"string" or
'string' or
"I\"m a string" or
'I\'m a string" or
'I"m a string' or
"I'm a string"
true/false
new Boolean(false) === false (wtf?)
in-memory data
temporal
case sensitive
reserved words
[a-zA-Z0-9_$]
var firstVariable; // declare
firstVariable = 1; // assign value
alert(firstVariable);
firstVariable = 'hello';
alert(firstVariable);
var secondVariable = 2;
firstVariable = secondVariable;
console.log(firstVariable);
secondVariable = 'world';
console.log(firstVariable); data are copied on assignment
var sum = 1 + 2 + 4 + 5;
sum += 20;
sum *= 5;
sum /= 9;
sum -= 1;
var str = 'Hello ' + 'world';
var name = prompt("Your name is", "unknown");
alert('Hello, ' + name);
document.write('Hello, ' + name); // document?
alert ("I'm " + 26 + " years old");
more than one item of data at the same time
zero-based indexing!
var myArray = [];
myArray[0] = 'Hello';
myArray[1] = 'World';
myArray[2] = 1500;
var myArray2 = ['how', 'are', 'you'];
degrees to decimal converter
>, <, >=, <=, ===, ==, !=, !==
var bigEnough = 1000 > 1;
if (1000 > 1) {
alert('Big enough');
}
var x = 0;
var y = x > 0 ? 'positive' : 'negative';
console.log(y);
logical operators
t && t -> true
t && f -> false
f && t -> false
f && f -> false
t || t -> true
t || f -> true
f || t -> true
f || f -> false
!t -> f
!f -> t
!!t -> t
!!f -> f
if (condition1) {
statement1
} else if (condition2) {
statement2
}
else if (condition3) {
statement3
}
...
else {
statementN
}
switch (expression) {
case value1:
//Statements executed when the result of expression matches value1
[break;]
case value2:
//Statements executed when the result of expression matches value2
[break;]
...
case valueN:
//Statements executed when the result of expression matches valueN
[break;]
default:
//Statements executed when none of the values match the value of the expression
[break;]
}
for ([initialization]; [condition]; [final-expression]) {
statement
}
for (var i = 0; i <= 9; i++) {
console.log(i);
// more statements
}
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for (var len = arr.length - 1; len >= 0; len -= 1) {
console.log('index: ', len);
console.log('value: ',arr[len]);
}
unknown number of iterations
while (condition) {
statement
}
var n = 1000,
x = 0;
while (n > 0) {
x += n;
n -= 1;
}
executes at least once
do {
statement
} while (condition);
var i = 0;
do {
i += 1;
console.log(i);
} while (i < 0);
exit the loop prematurely
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (var i = 0, len = arr.length; i < len; i +=1) {
if (arr[i] % 2 > 0) {
break;
}
console.log(arr[i]);
console.log(arr[i]/2);
}
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (var i = 0, len = arr.length; i < len; i +=1) {
if (arr[i] % 2 > 0) {
continue;
}
console.log(arr[i]);
console.log(arr[i]/2);
}
parameters
return values
function scope
function name([param[, param[, ... param]]]) {
statements
}
name([param[, param[, ... param]]]) // function call
degrees to decimal converter