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

JavaScript Operators

In this step-by-step lesson, you'll learn about JavaScript operators. You'll explore what an operator is, and delve into different types including arithmetic, string, assignment, comparison, and logical operators. Each step includes practical examples and exercises to try out on a coding website, helping you understand how these operators function in real-world scenarios.
Learning Goals Learning Outcomes Teacher Notes

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 - What is an Operator?

    If we look at the following sum: 7 + 4 = 11

    The + is the operator and the 7 and the 4 are the operands.

    So the numbers are called operands and the operation (to be performed between the two operands) is defined by an operator.

    In JavaScript we can use the following types of operators.

    Type Description
    Arithmetic Operators
    For adding, subtracting, multiplication and division.
    String Operators
    For joining strings together.
    Assignment Operators
    For assigning values to variables.
    Comparison Operators
    For comparing two different values (equal to, greater than, less than etc.)
    Logical Operators
    For determining the logic between variables or values (and, or, not).

    2 - Arithmetic Operators

    Arithmetic operators perform arithmetic on numbers. The main arithmetic operators are:

    Operator DescriptionExample
    +
    For adding two numbers together.
    let answer = 4 + 5
    -
    For subtracting one number from another.
    let answer = 6 - 3
    *
    For multiplying two numbers together.
    let answer = 2 * 7
    /
    For dividing one number by another.
    let answer = 10 / 2

    There are also some other arithmetic operators that we can use:

    Operator DescriptionExample
    ++
    For incrementing a number (increasing it by 1).
    answer++
    --
    For decrementing a number (decreasing it by 1).
    answer--
    %
    Modulus operator (%) returns the division remainder.
    let answer = 8 % 3

    3 - Try it out

    Let's try using the arithmetic operators. Go to the https://makecode.microbit.org website, create a new project and switch from Blocks to JavaScript.

    Add the following code examples one by one to your project and see if you get the same result in the simulator.

    Add this code Result
    let answer = 4 + 5
    basic.showNumber(answer)
    let answer = 6 - 3
    basic.showNumber(answer)
    let answer = 2 * 7
    basic.showNumber(answer)
    let answer = 10 / 2
    basic.showNumber(answer)
    let answer = 5
    
    basic.forever(function () {
        basic.showNumber(answer)
    })
    
    input.onButtonPressed(Button.A, function () {
        answer++
    })
    let answer = 5
    
    basic.forever(function () {
        basic.showNumber(answer)
    })
    
    input.onButtonPressed(Button.A, function () {
        answer++
    })
    
    // add this code
    input.onButtonPressed(Button.B, function () {
        answer--
    })

    4 - String Operators

    The + operator can also be used to join (concatenate) strings together.

    let firstName = "Stefan"
    let lastName = "Jones"
    let fullName = firstName + " " + lastName
    

    This will make fullName equal to Stefan Jones.

    If you add strings and numbers together then JavaScript will treat the number as a string.

    let fname = "Jane"
    let age = 25
    let x = fname + age
    

    This will make x equal to Jane25.

    5 - Try it out

    Let's try using the string operators. Delete all the previous code that you added.

    Add the following code examples one by one to your project and see if you get the same result in the simulator.

    Add this code Result
    let firstName = "Stefan"    // put in your own name
    let lastName = "Jones"      // put in your own name
    let fullName = firstName + " " + lastName
    basic.showString(fullName)
    let fname = "Jane"  // put in your own name
    let age = 25        // put in your own age
    let x = fname + age
    basic.showString(x)

    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 DigitalSkills.org. 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