Return help please


#1

Can someone walk me through the code in the JavaScript section of Code Academy. I am really confused about return.

let orderCount = 0;

const takeOrder = (topping, crustType) => {
orderCount++;
console.log('Order: ’ + crustType + ’ pizza topped with ’ + topping);
};

takeOrder(‘mushroom’, ‘thin crust’);
takeOrder(‘spinach’, ‘whole wheat’);
takeOrder(‘pepperoni’, ‘brooklyn style’);

const getSubTotal = (itemCount) => {
return itemCount * 7.5;
};
const getTax = (orderCount) => {
return getSubTotal(orderCount) * 0.06;
};

console.log(getSubTotal(orderCount));

const getTotal = () => {
return getSubTotal(orderCount) + getTax(orderCount);
};

console.log(getTotal());


#2

a simple example:

const example = () => {
    return "hello world";
}
x = example();

return literally means what it says, the function returns (hands something back) to the function call, which then can capture in a variable (x in my example), if you then log x to the console:

console.log(x);

you see the returned result by function.


#3

Also @thexboss the return keyword immediately breaks/exits a function so in the following example:

const example = () => {
  console.log("lets park this string here first");
  return "I will remember where you are";
  console.log("Dude where's my log?");
}
/*
lets park this string here first
I will remember where you are
*/

the last statement in the function above, threw an error “console.log unreachable after return statement”

so a console.log is always good to display the contents of our code and share whats going on without interrupting the normal flow of things,
the return keyword will interrupt the flow, but it is generally used in callbacks to store and manipulate data :+1:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.