Microbit JavaScript
Intermediate
60 mins
Teacher/Student led
295 points
What you need:
Chromebook/Laptop/PC

JavaScript Variables

In this step-by-step lesson, you'll learn about JavaScript variables. You'll understand how to create and use variables, add numbers and strings, and identify unique names. You'll also get hands-on experience by programming buttons on a website to display your name and age, and even simulate a birthday by adding one to your age.
Learning Goals Learning Outcomes Teacher Notes Lesson Files

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - JavaScript Variables

    Variables are containers for storing data values and objects. For example in a game you might have a 'score' variable. At the start of the game the 'score' variable would be 0 and then each time your get a point in the game it would be added onto the 'score' variable.

    Using programming blocks, the code would look like this:

    Description Code
    Create and set up the score variable.
    let score = 0
    Add 1 to the score.
    input.onButtonPressed(Button.A, function () {
        score += 1
    })
    Show the score.
    basic.forever(function () {
        basic.showNumber(score)
    })

    Using JavaScript, the code would look like this:

    Description Code
    Create and set up the score variable.
    let score = 0
    Add 1 to the score.
    input.onButtonPressed(Button.A, function () {
        score = score + 1
    })
    Show the score.
    basic.forever(function () {
        basic.showNumber(score)
    })

    2 - Adding number variables

    In this example apples, oranges and fruit, are variables.

    let apples = 4
    let oranges = 6
    let fruit = apples + oranges

    In programming, just like in algebra, we use variables (like apples) to hold values.

    In programming, just like in algebra, we use variables in expressions (fruit = apples + oranges).

    What value will fruit be equal to?

    see the answer

    fruit will be equal to 10 (as 4 + 6 = 10).

    3 - Adding string variables

    In JavaScript we can also "add" or join strings together, this is called concatenating them.

    let firstName = "LeBron"
    let lastName = "Smith"
    let fullName = firstName + " " + lastName // join or concatenate them with a space in the middle

    In this example the fullName variable will be equal to LeBron Smith.

    4 - JavaScript Identifiers

    All JavaScript variables must be identified with unique names. These unique names are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, firstName, country).

    The general rules for constructing names for variables (unique identifiers) are:

    • Variable names can contain letters, digits, underscores, and dollar signs.
    • Variable names must begin with a letter.
    • Variable names can also begin with $ and _.
    • Variable names are case sensitive (x and X are different variables).

    The following are examples of variables named correctly:

    let x = 10
    let X = 20
    let _age = 15
    let dateOfBirth = "8/5/2005"
    let country_name = "Mexico"
    let $height = "5 foot 7 inches"

    The following are examples of variables named incorrectly.

    let city = "London"
    let city = "Dublin"             // can't use the same name
    let age% = 15                   // can't use symbols
    let date Of Birth = "8/5/2005"  // can't have spaces

    5 - Exercise

    Now that we've learnt about JavaScript variables, let's create and use some in JavaScript code.

    Go to the https://makecode.microbit.org website, create a new project and switch to JavaScript instead of Blocks.

    Create the following 3 variables and store your own name and age in them.

    1. firstName
    2. lastName
    3. age

    see the code

    let firstName = "Evie"  // put in your first name
    let lastName = "Molloy" // put in your last name
    let age = 12            // put in your age

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more