Skip to content

Bindings again

This lesson explores how the concepts of bindings and scopes relate to variables in JavaScript.

SlidesPDF

The exercises below are reading exercises. Study the programs given and try to determine their outputs, without running them. You might find it helpful to draw how the variables are attached to values with a pen and paper.

Confirm your answers by running each program, only after you’ve attempted to guess its output.

  1. let text = "JavaScript";
    let language = text;
    text = text.toUpperCase();
    
    console.log(text);
    console.log(language);
    
  2. let x = 10;
    if (true) {
      console.log(x);
      let x = 30;
    }
    
  3. let y = 10;
    if (true) {
      y = 20;
    }
    if (true) {
      let y = 30;
    }
    console.log(y);
    
  4. const person = {
      firstName: "Mubaraq",
      lastName: "Wahab",
      address: {
        state: "Lagos",
        country: "Nigeria"
      }
    };
    
    person.firstName = "Isa";
    console.log(person);
    
    const address = person.address;
    address.state = "Abuja";
    console.log(person);
    
    person = "Elleman";
    console.log(person);
    
  5. const console = "🎮";
    console.log(console);
    

We know how to declare many variables:

let a = 1;
let b = 2;

We can also do these in the same declaration (note the use of the comma ,):

let a = 1, b = 2;

This works with const too:

const a = 1, b = 2;

You might come across these two terms, keyword and reserved word. How are they different? Let’s define them:

A keyword is a word in a programming language that has a special meaning. The word if in JavaScript is a keyword because it’s used to start an if statement.

A reserved word is a word that is not a valid name. The word if in JavaScript is also a reserved word because you can’t use it as a (variable) name.

Most keywords in JavaScript are also reserved words, and vice versa, so we’ll use the two terms interchangeably for our purposes.