when I console.log this function, why does it print 12 and undefined? Shouldn't it just be undefined as the function is not returning anything?
function area(w,h) {
console.log(w*h)
}
console.log(area (3,4))
EDIT: Sorry super new and I thought that regardless of what is printed by the console.log, the second console.log would receive only what the function returned.
function area(w,h) {
console.log(w*h) // Runs first and logs 12
}
console.log(area (3,4)) // Log undefined as the function doesn't return anything
The area function is logging the area, which is where 12 comes from.
The second undefined is the output of the console.log itself.
Generally, when you run anything which does not have output in a REPL, JavaScript adds an extra undefined
Because you already print it once
function area(w,h) {
console.log(w*h)
}
area (3,4);
or
function area(w,h) {
return (w*h)
}
console.log(area (3,4));