0

In this example I have a function named somar which returns the sum of numA and numB and after that, the console shows me the result of the sum:

let somar = (numA, numB) => { //função anônima JS

  console.log("Somando " + numA + " com " + numB);
  return numA + numB;
}

var resultado = somar(10, 5);
console.log(resultado);

When I run this code, my output obviously is 15. But how do I console.log numA and numB?

When I tried to console log numA and numB my output is undefined.

I just got the expected result when I used the console.log inside the function as shown in the example. But how to do it outside?

j08691
  • 204,283
  • 31
  • 260
  • 272
  • _"When I tried to console log numA and numB"_ - where? – evolutionxbox Jun 21 '22 at 15:02
  • They don't _exist_ outside of the function, those are just the parameter names _inside_ the function. – jonrsharpe Jun 21 '22 at 15:04
  • This logs the parameters just fine. `numA` and `numB` are local to `somar` and cannot be accessed outside of it. – Dave Newton Jun 21 '22 at 15:06
  • @evolutionxbox Thx for the link, gonna read that. And about ur question, i didn't post it the part *where* i tried but it was something like ```console.log("Somando" somar.numA);``` But, didn't worked either I think i have to study more about function scopes – Gabriel Nunes Jun 21 '22 at 15:07
  • @DaveNewton The example is of what actually works: _I just got the expected result when I used the console.log inside the function as shown in the example. But how to do it outside?_ – Boaz Jun 21 '22 at 15:08
  • You are passing those parameters to that function, so you already have them, just put them on a variable before passing them to that function – Lk77 Jun 21 '22 at 15:11

0 Answers0