return [expression];
/* The return statement ends a function immediately at the
point where it is called. */
function counter() {
for (var count = 1; count < 5 ; count++) {
console.log(count + 'A');
if (count === 3) return; // ends entire counter function
console.log(count + 'B');
}
console.log(count + 'C'); // never appears
}
counter();
// 1A
// 1B
// 2A
// 2B
// 3A
function add(a, b) {
return a + b; //Return stops the execution of this function
}
var sum = add(5, 24); //The value that was returned got but inside the variable sum
function square(x) {
return x * x;
}
var demo = square(3);
// demo will equal 9
//The return statement ends function execution,
//and specifies a value to be returned to the function caller.
function getRectArea(width, height) {
if (width > 0 && height > 0) {
return width * height;
}
return 0;
}
console.log(getRectArea(3, 4));
// expected output: 12
console.log(getRectArea(-3, 4));
// expected output: 0