Skip to content
KALANBLON
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.

Step 8 of 8
Text description of the animation
  1. The function total receives an array of prices and returns their sum.
  2. Line 8: to display the result, JavaScript first calls the function.
  3. Inside the function: prices receives the array, sum starts at 0.
  4. First pass: p is 3, sum becomes 3.
  5. Second pass: p is 5, sum becomes 8.
  6. Third pass: p is 2, sum becomes 10.
  7. The array has been gone through: the function returns 10.
  8. 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
A loop for (let i = 1; i <= 15; i++) counts from 1 to 15. i % 3 === 0 is true for multiples of 3.
Hint 2
Test the "multiple of 3 and 5" case first, otherwise it will never be reached.
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 % 3 before i % 15: 15 would display Fizz.
  • Writing i <= 15 without let in the loop (for (i = 1; …)): in strict mode, it's a ReferenceError.

Summary

  1. 1const declares a variable that won't be reassigned, let a variable that can change.
  2. 2console.log displays a result in the console.
  3. 3A function is declared with function and returns a value with return.
  4. 4An array is written between square brackets; for…of goes through its items.
  5. 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.