(Acknowledgment: This way of introducing JavaScript is inspired by the methods of Seb Lee-Delisle.)
page.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Greetings!</title>
<script src="greetings.js" type="module"></script>
</head>
<body>
</body>
</html>
NB These days we write type="module", not type="text/javascript"
greetings.js:
let now = new Date();
window.alert('Hello world. It is ' + now + ', right now.');
<script> start tag and </script> end tag?
window.alert() method. Why?
Array, Date and Mathcanvasparticles.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Particles</title>
<link rel="stylesheet" href="particles.css">
<script src="particles.js" type="module"></script>
</head>
<body>
<canvas width="500" height="300">
</canvas>
</body>
</html>
particles.css:
body {
background-color: black;
}
canvas {
display: block;
margin-left: auto;
margin-right: auto;
border: 1px solid white;
}
particles.jslet canvas;
let context;
let width;
let height;
document.addEventListener('DOMContentLoaded', init, false);
function init() {
canvas = document.querySelector('canvas');
context = canvas.getContext('2d');
width = canvas.width;
height = canvas.height;
}
JavaScript variables should be explicitly declared (using let or const)
| Python | JavaScript |
|---|---|
hourly_pay = 9.5 hours_worked = 35 total_pay = hourly_pay * hours_worked print(total_pay) # Hurray! A pay rise: hourly_pay = 10.5 total_pay = hourly_pay * hours_worked print(total_pay) |
let hourly_pay; let hours_worked; let total_pay; hourly_pay = 9.5; hours_worked = 35; total_pay = hourly_pay * hours_worked; console.log(total_pay); // Hurray! A pay rise: hourly_pay = 10.5; total_pay = hourly_pay * hours_worked; console.log(total_pay); |
But JavaScript does allow you to combine variable declaration with initialization
| Python | JavaScript |
|---|---|
hourly_pay = 9.5 hours_worked = 35 total_pay = hourly_pay * hours_worked print(total_pay) # Hurray! A pay rise: hourly_pay = 10.5 total_pay = hourly_pay * hours_worked print(total_pay) |
let hourly_pay = 9.5; let hours_worked = 35; let total_pay = hourly_pay * hours_worked; console.log(total_pay); // Hurray! A pay rise: hourly_pay = 10.5; total_pay = hourly_pay * hours_worked; console.log(total_pay); |
| Python | JavaScript |
|---|---|
# This is a comment x = 3 # This is also a comment |
// This is a comment let x = 3; // This is also a comment |
| Python | JavaScript |
|---|---|
""" This is another comment. It extends over more than one line. """ |
/* This is another comment. It extends over more than one line. */ |
| Python | JavaScript |
|---|---|
def print_1_to_n(n):
for i in range(1, n+1):
print i
print_1_to_n(10)
|
function print_1_to_n(n) {
for (let i = 1; i <= n; i += 1) {
console.log(i);
}
}
print_1_to_n(10);
|
NB: Python uses indentation to denote blocks of code; JavaScript uses curly braces
context.fillRect(x, y, width, height)
x |
The x-coordinate of the upper-left corner of the rectangle |
y |
The y-coordinate of the upper-left corner of the rectangle |
width |
The width of the rectangle, in pixels |
height |
The height of the rectangle, in pixels |
context.clearRect(x, y, width, height)
x |
The x-coordinate of the upper-left corner of the rectangle to clear |
y |
The y-coordinate of the upper-left corner of the rectangle to clear |
width |
The width of the rectangle to clear, in pixels |
height |
The height of the rectangle to clear, in pixels |
window.setInterval(function, milliseconds)
function |
The function that will be executed |
milliseconds |
The interval between calls to the function |
particles.jslet canvas;
let context;
let width;
let height;
let x = 250;
let y = 150;
let size = 10;
let xChange = getRandomNumber(-10, 10);
let yChange = getRandomNumber(-10, 10);
document.addEventListener('DOMContentLoaded', init, false);
function init() {
canvas = document.querySelector('canvas');
context = canvas.getContext('2d');
width = canvas.width;
height = canvas.height;
window.setInterval(draw, 33);
}
function draw() {
context.clearRect(0, 0, width, height);
context.fillStyle = 'yellow';
context.fillRect(x, y, size, size);
x = x + xChange;
y = y + yChange;
}
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}