DONATE  
MENU
JSON
AJAX
HTML
JAVASCRIPT

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Functions » Functions' Arguments
Share Tweet WhatsApp

Functions' Arguments

Functions' arguments are the values passed to parameters when calling or invoking a function. This is a page on it own because most of the time, beginners confuse parameters and arguments for same thing. But then, the difference is clear. Parameters are variables declared on a function parentheses (()) which can be used in the function as their values will be assigned when calling the function. Arguments on the other hand are the real values passed or assigned to those parameters when calling the function. In another view, parameters can be seen as declared variables while arguments are the values assigned to those parameters or variables.
function addNum(x, y){
//x is the first parameter
//y is the second parameter
}

addNum(2, 10); //2 is the argument or value to the first parameter
//10 is the argument or value to the second parameter
In simpler terms, an argument is a value to a function parameter. A FUNCTION CAN HAVE MORE PARAMETERS THAN ARGUMENTS AND VICE VERSA A function can have up to six(6) parameters when defined and less arguments when called. This is not allow in some strict programming languages like PHP and JAVA but JavaScript permits such.
//DEFINING FUNCTION
function myFunc(a, b, c, d, e, f){
}
//CALLING FUNCTION
myFunc(1, 2, 3); //THREE ARGUMENTS
myFunc(3); //ONE ARGUMENT
What happens: Remember variables without values are undefined. So, when we don't pass some arguments for some parameters, the value of those parameters becomes undefined.
<script>
function myFunc(x, y){
alert(x);
alert(y);
}

myFunc(34); //Argument for x but none for y, so y is undefined
</script>

Copy PRACTICE IT

Therefore, ensure you pass arguments to all parameters that your function depends on else it won't execute or execute properly.
<script>
function addNums(x, y){
var sum = x + y; //the function needs x and y to function properly
alert(sum);
}

addNums(3, 4);
addNums(7);
</script>

Copy PRACTICE IT

Also, functions in JavaScript can have more arguments than the parameters.
function myFunc(x,y){
}
myFunc(1, 2, 3);
myFunc(3, 9, 0, 59)
The implication is that you can't access all arguments because there was no variable in the function parentheses to hold or name them but there is a way around this. The JavaScript functions' arguments object. You will learn about the argument object in the next page.
NOTE:
  • Functions' arguments can be values of any data type be it numbers, strings, booleans, arrays, objects and function. A particular parameter can have separate data type at various points of invocation.
    alert(1); //argument is a number
    alert("Hello"); //argument is a string
    alert(true); //argument is a boolean
    alert([1, 2, 3]); //argument is an array
    alert({"name":"stackbeginner"}); //argument is an object
    alert(sayHello()); //argument is a function
    So, you can see one parameter having different data types as arguments when invoked.
SUMMARY
  1. After declaring parameters when defining a function, the values we assign to those parameters are known as arguments .
  2. In JavaScript, it is allowed for functions to have more parameters and when calling them, we pass less arguments. The implication is that the value for parameters without arguments will be undefined.
  3. It is important you pass arguments to all parameters your function depends on for its execution or proper execution.
  4. It is also allowed for a function to have more arguments than declared parameters. This also means that a function without parameters can be assigned arguments. The implication is that you won't be able to access such arguments.
  5. If a function have more arguments than parameters, you can access all the arguments using the function argument object.
Take a short quiz on Functions' Arguments.
Ask a question or tell us if Functions' Arguments is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode