Please enable JavaScript to use CodeHS

CodeHS Glossary


Nested Function JavaScript

A nested function is a function that is defined inside another function. This should be avoided.

An example of a nested function:

function start() {
    move();
    function turnRight() {
        turnLeft();
        turnLeft();
        turnLeft();
    }
    turnRight();
    move();
    ...
}

Instead, your functions should be defined after the other functions rather than inside them. Like this:

function start() {
    move();
    turnRight();
    move();
    ...
}

function turnRight() {
    turnLeft();
    turnLeft();
    turnLeft();
}