10.4.1.1. Returning a Value¶ // Functions
/*To return a value from functions that we create, we can use a return
statement. A return statement has the form:*/
return someVal;
//where someVal is any value.//
/*This function has a single parameter, n, which is expected to be a
positive integer. It returns the sum 1+2+...+n.*/
function sumToN(n) {
let sum = 0;
for (let i = 0; i <= n; i++) {
sum += i;
}
return sum;
}
console.log(sumToN(3));
Console Output
6