This content originally appeared on DEV Community and was authored by Deep Space
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 Contents:
- 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 + 10
return 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 return
keyword.
Followed by function call addNumbers();
.
But we can change the above code, using parameters
& arguments
to replace variable declaration and 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.
- 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 argument (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 + 10
return sum;
}
console.log(addNumbers(5, 10));
Code Explanation:
Instead of writing variable names inside the function block {}
.
We wrote them inside the 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 functions 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 (Deep Space).
And here on the dev community, too.
I love coffee, you can buy me one here "Buy me a coffee"
This content originally appeared on DEV Community and was authored by Deep Space
Deep Space | Sciencx (2022-02-05T20:56:09+00:00) JavaScript Functions – Parameters & Arguments Explained. Retrieved from https://www.scien.cx/2022/02/05/javascript-functions-parameters-arguments-explained/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.