return
keyword stops the function and returns the given value from the function.You can use return
to either stop a function without returning a value:
function myFunction() {
var a = 1;
a++;
if (a > 1) {
return;
}
console.log("Cannot reach this line");
}
myFunction();
Or to stop a function and return a value:
function myFunction() {
var a = 1;
a++;
if (a > 1) {
return a;
}
console.log("Cannot reach this line");
}
var result = myFunction();
console.log(result); // 2