Microbit JavaScript
Advanced
60 mins
Teacher/Student led
+295 XP
What you need:
Chromebook/Laptop/PC

JavaScript Variables

In this lesson we learn about JavaScript Variables and start coding with them.

Teacher Class Feed

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
    123learn · Online learning platform

    Unlock the full learning experience

    You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.

    Hundreds of curriculum-aligned lessons
    Interactive activities in every lesson
    Printable resources & progress tracking
    Copyright Notice
    This lesson is copyright of Coding Ireland 2017 - 2026. 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