Return

  • When a function is called the computer runs through all the tasks given inside a code block and executes them

  • By default the resulting value is undefined

  • So to capture that value we use return keyword which basically gives the value whenever a function is called

Example:

function rectangleArea(height, width) {
    let area = height * width;
}

console.log(rectangleArea(5, 7));

// Output: undefined

Instead:

function rectangleArea(height, width) {
    let area = height * width;
    return area;
}

console.log(rectangleArea(5, 7));

// Output: 35

or

function rectangleArea(height, width) {
    return height * width;
}

console.log(rectangleArea(5, 7));

// Output: 35

Last updated