Why is the returned value undefined when it looks fine right before it is return when I console log it. I assume it is something to do with closure but don't get it. The code and the console logs for two different test calls are below:
CODE:
function addTogether() {
const [a, b] = arguments;
console.log(`a: ${a} b: ${b}`);
if (typeof a !== 'number') {
return undefined;
}
if (b !== undefined){
if (typeof b !== 'number'){
return undefined;
} else{
let sum = a+b;
console.log("sum being returned =", sum)
return sum;
}
}
return ((b)=>{
addTogether(a,b);
})
}
console.log("returnVal =", addTogether(2,5));
console.log("returnVal =", addTogether(5)(7));
CONSOLE OUTPUT:
a: 2 b: 5
sum being returned = 7
returnVal = 7
a: 5 b: undefined
a: 5 b: 7
sum being returned = 12
returnVal = undefined