Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Arguments | Functions
Introduction to JavaScript
course content

Conteúdo do Curso

Introduction to JavaScript

Introduction to JavaScript

1. Basic Concepts
2. Variables and Data Types
3. Basic Operations
4. Conditional Statements
5. Loops
6. Functions

Arguments

Arguments are function variables that you can use only inside the function:

12345678
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
copy

Also, if you name the arguments the same as the variables outside the function, the function will use the arguments instead of the variables:

123456789
let a = 15; function num(a) { console.log("(func) a =", a); }; num(20); console.log("(global) a =", a);
copy

Note

When the function finishes execution, its space disappears, and all arguments lose their values.

Arguments are received sequentially:

12345
function numSet(a, b, c) { console.log([a ** 2, b + 2, c - 10]); }; numSet(15, 12, 99);
copy

Unfilled arguments will receive the value undefined and will not be displayed in any way. Redundant arguments will not be used:

123456
function numSet(a, b, c) { console.log([a, b, c]); } numSet(12, 13); numSet(15, 12, 13, 15);
copy

The function receives values as arguments. Variables outside the function remain unchanged. An argument is an independent value inside a function:

12345678910
let a = 15; function add(numb) { numb += 5; console.log("(func) numb =", numb); }; add(a); console.log("(global) a =", a);
copy

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.

Tudo estava claro?

Seção 6. Capítulo 4
We're sorry to hear that something went wrong. What happened?
some-alt