V
V
Valentin Popov2018-02-06 07:38:42
JavaScript
Valentin Popov, 2018-02-06 07:38:42

How does passing parameters to a javascript function work?

Please explain why in this code:

var a = 3, b = 2, c = 4;
function test(a, b, c) {
  var d = b * (a + c);
  console.log(d);
}

test(b, c, a);

The output is 20, not 14?...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Padre, 2018-02-06
@elarkov

Because the parameters are passed to the place where they are, for example:

var a = 2;
var b = 4;
var c = 5;
function test(x,y,z) {

}
test(a,b,c)

So x becomes a
y becomes b
z starts c

S
Sergey Sokolov, 2018-02-06
@sergiks

This is an exercise in variable scoping in JavaScript . Variables
inside a function are not the same as outside. They are called the same, it is confusing, but they mean different things. testa, b, c

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question