JS 3

Yes, exactly! console.log(answer + 2) prints 14. Your answer was correct.

Let's trace it slowly:

function multiply(a, b) {
  return a * b;
}

const answer = multiply(3, 4);

console.log(answer + 2);

Step 1

The function is called:

multiply(3, 4)

Inside the function:

return 3 * 4;

So it returns:

12

Step 2

This line becomes:

const answer = 12;

Now the variable answer stores 12.


Step 3

This line:

console.log(answer + 2);

becomes:

console.log(12 + 2);

which becomes:

console.log(14);

So the output is:

14

You were absolutely right.


One important concept

Don't confuse return and console.log():

function add(a, b) {
  return a + b;
}

const result = add(2, 3); // result is 5
console.log(result);       // prints 5

Here:

  • return gives the value (5) back to the caller.

  • console.log() displays whatever value you give it.

A good way to think about it:

  • return = "Give this value back."

  • console.log = "Show this value on the screen."


💡 Since you caught this correctly, you're understanding the flow of execution. That's an important skill for reading and writing JavaScript. Keep asking questions like this whenever something feels unclear.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.