Show contents
Chapter 3 of 4 · 15 min
JavaScript: your first lines
By the end of this chapter, you'll be able to
- Declare variables with const and let
- Display a result with console.log
- Write and call a function
- Go through an array with for…of
Variables and output
In JavaScript, you declare a variable with const (its value won't be reassigned) or let (it can change). console.log() displays a result in the console, the equivalent of Python's print(). Each statement ends with a semicolon.
const firstName = "Yaw";
let points = 10;
points = points + 5;
console.log(firstName, "has", points, "points");
console.log(`Well done ${firstName}!`);
Yaw has 15 points
Well done Yaw!
Text between backticks ` is a template literal: ${...} inserts the value of an expression into it, like Python's f-strings.
Functions and arrays
A function is declared with function, an array (the equivalent of a list) is written between square brackets, and for…of goes through its items:
Back on line 8: console.log displays 10.
Text description of the animation
- The function total receives an array of prices and returns their sum.
- Line 8: to display the result, JavaScript first calls the function.
- Inside the function: prices receives the array, sum starts at 0.
- First pass: p is 3, sum becomes 3.
- Second pass: p is 5, sum becomes 8.
- Third pass: p is 2, sum becomes 10.
- The array has been gone through: the function returns 10.
- Back on line 8: console.log displays 10.
function total(prices) {
let sum = 0;
for (const p of prices) {
sum = sum + p;
}
return sum;
}
console.log(total([3, 5, 2]));
const fruits = ["mango", "papaya"];
fruits.push("guava"); // adds at the end
console.log(fruits.length, fruits[0]);
10
3 mango
Code blocks are delimited by braces { }: unlike in Python, indentation is only a readability convention (keep it anyway!).
Your turn
Mini-lab: the average in JavaScriptJavaScript
Output
Click “Run” to execute your code.Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Worked exercise
Worked exercise: FizzBuzz
Display the numbers from 1 to 15, but write Fizz instead of multiples of 3, Buzz instead of multiples of 5, and FizzBuzz for multiples of both.
Hint 1
for (let i = 1; i <= 15; i++) counts from 1 to 15. i % 3 === 0 is true for multiples of 3.Hint 2
Show the solution and common mistakes
for (let i = 1; i <= 15; i++) {
if (i % 15 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);
}
}
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
=== compares the value and the type: it's the comparison to use in JavaScript (== converts types and has surprises in store, like "5" == 5 being true).
Common mistakes:
- Testing
i % 3beforei % 15: 15 would displayFizz. - Writing
i <= 15withoutletin the loop (for (i = 1; …)): in strict mode, it's aReferenceError.
Summary
- 1const declares a variable that won't be reassigned, let a variable that can change.
- 2console.log displays a result in the console.
- 3A function is declared with function and returns a value with return.
- 4An array is written between square brackets; for…of goes through its items.
- 5Blocks are delimited by braces, and you compare with ===.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.