Using Function Arguments Effectively
Arguments are function variables that you can use only inside the function:
12345678function funcName(a, b) { console.log("Arg A =", a); console.log("Arg B =", b); }; funcName(15, 24); console.log(a); // This will raise an Error
Also, if you name the arguments the same as the variables outside the function, the function will use the arguments instead of the variables:
123456789let a = 15; function num(a) { console.log("(func) a =", a); }; num(20); console.log("(global) a =", a);
Note
When the function finishes execution, its space disappears, and all arguments lose their values.
Arguments are received sequentially:
12345function numSet(a, b, c) { console.log([a ** 2, b + 2, c - 10]); }; numSet(15, 12, 99);
Unfilled arguments will receive the value undefined
and will not be displayed in any way. Redundant arguments will not be used:
123456function numSet(a, b, c) { console.log([a, b, c]); } numSet(12, 13); numSet(15, 12, 13, 15);
The function receives values as arguments. Variables outside the function remain unchanged. An argument is an independent value inside a function:
12345678910let a = 15; function add(numb) { numb += 5; console.log("(func) numb =", numb); }; add(a); console.log("(global) a =", a);
Note
This does not work the same way for arrays because an array contains a reference to some data. This reference is passed to the function, so changes inside the function affect the data outside. This will be studied in the "OOP in JavaScript" course.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Awesome!
Completion rate improved to 2.33
Using Function Arguments Effectively
Deslize para mostrar o menu
Arguments are function variables that you can use only inside the function:
12345678function funcName(a, b) { console.log("Arg A =", a); console.log("Arg B =", b); }; funcName(15, 24); console.log(a); // This will raise an Error
Also, if you name the arguments the same as the variables outside the function, the function will use the arguments instead of the variables:
123456789let a = 15; function num(a) { console.log("(func) a =", a); }; num(20); console.log("(global) a =", a);
Note
When the function finishes execution, its space disappears, and all arguments lose their values.
Arguments are received sequentially:
12345function numSet(a, b, c) { console.log([a ** 2, b + 2, c - 10]); }; numSet(15, 12, 99);
Unfilled arguments will receive the value undefined
and will not be displayed in any way. Redundant arguments will not be used:
123456function numSet(a, b, c) { console.log([a, b, c]); } numSet(12, 13); numSet(15, 12, 13, 15);
The function receives values as arguments. Variables outside the function remain unchanged. An argument is an independent value inside a function:
12345678910let a = 15; function add(numb) { numb += 5; console.log("(func) numb =", numb); }; add(a); console.log("(global) a =", a);
Note
This does not work the same way for arrays because an array contains a reference to some data. This reference is passed to the function, so changes inside the function affect the data outside. This will be studied in the "OOP in JavaScript" course.
Obrigado pelo seu feedback!