Mozilla and you
Brought to you by the Mozilla Developer Network
<canvas>
<h1>Example heading</h1>
h1 { color: red; }
console.log('JavaScript test!');
Run these in the browser console
myVariable
(undefined)var myVariable = 'Chris'
myVariable = 'Bob'
myVariable
('Bob')25
"Chris"
. "25"
is still a stringtrue
or false
var
to declare variablemyVariable
is not myvariable
;
){ ... }
+
— addition/concatenation-
— subtraction/
— division*
— multiplication%
— modulo=
— assignment===
— strict equality/identity||
— logical OR&&
— logical AND<
, >
, <=
, =>
— less than, greater than, less than or equal to, greater than or equal to!, !==
— logical NOT, not equal to8 + 4 / 3 * 4
= 13.33333333333(8 + 4) / (3 * 4)
= 1if(condition === true) {
// run expression 1
} else {
// run expression 2
}
var myTown = prompt('Enter your home town or city');
if(myTown === 'Manchester') {
alert('Is it still raining in Manchester?');
} else {
alert('I don\'t know your home town very well');
}
if(myTown === 'Manchester') {
alert('Is it still raining in Manchester?');
} else if(myTown === 'Barcelona') {
alert("You have beautiful architecture, and sun");
} else {
alert('I don\'t know your home town very well');
}
if(myTown === "Manchester" || myTown === "Bradford") {
alert("It is rainy in those Northern towns.");
}
if(myTown === "Manchester" && mySeason === "Summer") {
alert("Manchester must be enjoying some warmer weather.");
}
if(condition1 === true) {
if(condition2 === true) {
// run expression 1 {
}
} else {
// run expression 2
}
for(i = starting value; exit-condition ; iterator) {
// run code
}
i
— variablei
begins ati
has to be for the loop to keep runningi
changes each time the loop is runfor(i = 0; i <= 20; i++) {
console.log(i);
}
This is how many useful things are done on a web page
<h1 class="myHeading">My heading</h1>
var myH1 = document.querySelector('.myHeading');
myH1.innerHTML = "My updated heading";
<canvas>
is<canvas width="500" height="500">
</canvas>
var myCanvas = document.querySelector('canvas');
var myContext = myCanvas.getContext('2d');
myContext.fillStyle = 'rgba(0,0,255,0.5)';
myContext.fillRect(0,0,200,200);
myContext.strokeStyle = 'rgba(0,255,0,0.5)';
myContext.lineWidth = 1;
myContext.strokeRect(200,200,50,50);
myContext.fillStyle = 'rgba(0,0,255,0.15)';
for(i = 0; i <= 50; i++) {
myContext.fillRect(i,i,25+i,25+i);
}
myContext.fillRect((i*6),(i*4),25+(i*10),25+(i*10));
i+=5
myContext.fillStyle = 'rgba(0,0,' + (150+(i*6)) + ',0.15)';
Math.floor(Math.random()*200)
myContext.rotate(0.025);
myContext.fillRect((i*5),(250+(Math.sin(i/6))*50),10,10);
myContext.beginPath();
myContext.arc((i*5),(250+(Math.sin(i/6))*50),10,0,Math.PI*2,true);
myContext.fill();
function random10() {
var randomNumber = Math.floor(Math.random() * 10) + 1;
return randomNumber;
}
random10(); // call function
function random(input) {
var randomResult = Math.floor(Math.random() * input) + 1;
return randomResult;
}
random(250); // call function
function sineWave(number,y) {
for(i=0;i<=number;i++) {
myContext.fillStyle = 'rgba(0,0,'
+ (Math.floor(Math.random()*200)) + ',0.5)';
myContext.fillRect((i*5),(y+(Math.sin(i/6))*50),10,10);
}
}
<button class="welcome">Click me!</button>
var button = document.querySelector('.welcome');
button.onclick = function() {
alert("Ouch, that hurt!");
}
var button = document.querySelector('.welcome');
function clicky() {
alert("Ouch, that hurt!");
}
button.onclick = clicky;