HTML = organization of webpage content
CSS = definition of content presentation style
JavaScript = how the content interacts and behaves with the user
We use the <script>
tag to connect our HTML page with our JavaScript file as follows:
<script type="text/javascript" src="path/file.js"></script>
The Chrome JavaScript Console, accessed via View > Developer > JavaScript Console.
There are four primary data types in JavaScript:
string
number
boolean
array
Used for storing textual information.
Double quotes:
"a name means a lot just by itself"
Single quotes:
'freedom is a luxury not a necessity'
Invert quote type:
"Colognes in a blue bottle or with the word 'blue' in their names have proliferated on
men's fragrance counters in the last few years"
Escaping:
"Many economists say that such \"first dollar\" coverage leaves beneficiaries
insensitive to costs"
Used to represent numerical data:
Integers:
75
Float:
3.14159
Used for representing a binary value (true or false):
true
or
false
Contain other data types
Mixed types OK
Starts with [
, ends with ]
, elments separated with ,
.
Whitespace is ignored
Empty array:
[]
Single element:
["one"]
Multiple elements:
["first", 2, "three"]
Array Indexing
[]
+ number = index into array
JavaScript is a zero indexed language:
["red", "white", "blue"][0] => "red"
["red", "white", "blue"][1] => "white"
["red", "white", "blue"][2] => "blue"
Array length:
["red", "white", "blue"].length => 3
First element at zero, last element at one less than array length
Declaration:
var blackjack;
Assignment:
blackjack = 21;
Declaration and assignment:
var blackjack = 21;
Reassignment:
var name = "Brendan";
name = "Brendan Griffiths";
console.log("Hello world");
Console log accepts any variable or data type. Multiple values can be strung together, separated with a comma:
console.log(myVar, "Hello", 7);
Less than:
<
Greater than:
>
Less than or equal to:
<=
Greater than or equal to:
>=
Addition:
1 + 1 = 2
Subtraction:
3 - 2 = 1
Multiplication:
5 * 3 = 15
Division:
10 / 2 = 5
Increment:
myVar = myVar + 1;
myVar++;
Decrement:
myVar = myVar - 1;
myVar--;
Produces a random number between 0 and 1:
Math.random();