JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • File Location
  • Sample Code
  • The Arguments Object
  • More Practice
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 4-Functions

Arguments

A parameter is a variable in a method definition. When a method is called, the arguments are the data you pass into the method's parameters. To say it again, a parameter is the variable in the declaration of the function. An argument is the actual value of this variable that gets passed to function.

File Location

Let's work in this file:

    javascript-library
        └── 1-Fundamentals
            └── 4-Functions
                06-arguments.js <----You will be working in this file.

Sample Code

Let's create a function with three parameters:

function addThree(a, b, c){
    return a + b + c;
}

Let's pass in arguments to satisfy those parameters:

console.log(addThree(1,2,3));
console.log(addThree(4,5,6));

A few more rules on arguments and functions: 1. Extra argument get ignored:

console.log(addThree(1,2,3,4));
  1. If an argument hasn't been provided, it's NaN.

    console.log(addThree(1,2));

The Arguments Object

The arguments object is a local variable available within all (non-arrow) functions. You can refer to a function's arguments within the function by using the arguments object. arguments is a keyword, and it is array-like, which means that the object is indexed, starting at 0. Take some time to type the following demoFunction and analyze it's interworkings:

function demoFunction(x, y) {
    console.log(arguments);
    console.log(arguments[0]);
    console.log(arguments[1]);
}

demoFunction(1, 2);

More Practice

The most important takeaways here are this:

  1. Know the difference between a parameter and an argument.

  2. Know about the arguments object found inside of functions.

PreviousParametersNextClosures

Last updated 7 years ago