Conteúdo do Curso
Introduction to JavaScript
Introduction to JavaScript
Arguments
Arguments are function variables that you can use only inside the function:
function 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:
let 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:
function 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:
function 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:
let 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!