JavaScript Functions — Parameters and Arguments
Learn what parameters, arguments, and return statements are with videos and animations.
Let’s understand what is a JavaScript Parameter
& Argument
.
Also what the return
statement is.
In the previous article, we learned about the general idea of a function, function definition & function invocation. Visit to refresh your knowledge, if you want.
Let’s get a little more advanced with functions.
Table of Content:
- JavaScript variables in functions
- JavaScript Functions — Parameters & Arguments
- JavaScript
return
statement
JavaScript variables in functions
JavaScript functions can also contain variables.
Let’s create a function called addNumbers()
to demonstrate.
function addNumbers() {const a = 5;
const b = 10;
const sum = a + b; // 5 + 10return sum;
}
console.log(addNumbers());
Code Explanation:
In the function block {}
, we have 3 variables a
, b
, and sum
, with values.
In the end, we have the return
keyword.
Followed by function call, addNumbers()
Logging the function call, to see the result inside the console.
But we can change the above code, using Parameters
and Arguments
to replace variable declaration & assignment.
JavaScript Functions — Parameters & Arguments
JavaScript function parameters
are the names (placeholders) for values.
JavaScript function arguments
are the actual values of parameters.
Let me explain it to you like we are having a street conversation or talk in an informal language as friends.
- Parameters are variable names. The word parameter is just a fancy word for saying “variable name” in JS
- Arguments are the actual values of those variables. The word argument is just another fancy word for saying “variable value”
- So we use parameters(variable names) to refer to arguments(actual variable values)
Makes sense? Let’s see it in code, or better convert the above function’s variables & their values to parameters & arguments.
function addNumbers(a, b) {const sum = a + b; // 5 + 10return sum;
}
console.log(addNumbers(5, 10));
Code Explanation:
Instead of writing variable names inside the function block {}
.
We wrote them inside the function parentheses()
.
And we gave arguments (actual variable values) on function call addNumbers(5, 10)
.
Most people can’t see the relationship between parameters & arguments (including myself). So I decided to make a video, animations, and pictures to help you visualize.
Notice, we don’t need to use a JavaScript keyword const
, let
or var
to declare the variables a
and b
, inside the ()
parentheses.
JavaScript return
statement
The JavaScript return
statement as it sounds, it returns something.
When the JavaScript function reaches the return statement, it will stop the function execution and returns the value to the caller.
Thanks for reading. Follow me on my Youtube Channel
And here on Medium as well.
I love coffee, you can buy me one here “Buy me a Coffee”
More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.