Skip to content

Javascript Editor

Blocks Javascript

You are not limited to blocks. Each block has as Javascript API and you can quickly turn your block-based design into a javascript program.

You can mix the two, as the blocks editor also include a Javascript function block, where you can type code directly.

The Javascript API closely follows the blocks structure

The Javascript Editor allows to see in form of code the blocks designed.

See the full reference

Javascript for the Micro:bit

Hello world

basic.forever(() => {

})
basic.showString("Hello, World!")

Button A

input.onButtonPressed(Button.A, () => {
    basic.showIcon(IconNames.Happy)
})

Two buttons

input.onButtonPressed(Button.A, () => {
    basic.showIcon(IconNames.Happy)
})
input.onButtonPressed(Button.B, () => {
    basic.showIcon(IconNames.Sad)
})

Touch input

let touches = 0
input.onPinPressed(TouchPin.P0, () => {
    touches += 1
    basic.showNumber(touches)
})

Reading a digital input

basic.forever(() => {
    while (pins.digitalReadPin(DigitalPin.P0) == 1) {
        basic.showIcon(IconNames.Surprised)
    }
    basic.showIcon(IconNames.Asleep)
})

Writing a digital output

basic.forever(() => {
    pins.digitalWritePin(DigitalPin.P1, 1)
    basic.pause(1000)
    pins.digitalWritePin(DigitalPin.P1, 0)
    basic.pause(1000)
})

PWM Output

pins.analogSetPeriod(AnalogPin.P1, 10000)
let index = 0
basic.forever(() => {
    for (let index = 0; index <= 1023; index++) {
        pins.analogWritePin(AnalogPin.P1, index)
        basic.pause(1)
    }
    for (let index = 0; index <= 1023; index++) {
        pins.analogWritePin(AnalogPin.P1, 1023 - index)
        basic.pause(1)
    }
})

Analog input

 basic.forever(() => {
    basic.showNumber(pins.analogReadPin(AnalogPin.P2))
    basic.showString("")
})

Temperature sensor

basic.forever(() => {
    basic.showNumber(input.temperature())
})

Compass

basic.forever(() => {
    basic.showString("Heading " +↩
input.compassHeading())
})

Accelerometer

input.onGesture(Gesture.Shake, () => {
    basic.showIcon(IconNames.Surprised)
})

Accelerometer data

basic.forever(() => {
    basic.showString("X:" +↩
input.acceleration(Dimension.X))
    basic.showString("Y:" +↩
input.acceleration(Dimension.Y))
    basic.showString("Z:" +↩
input.acceleration(Dimension.Z))
})