Back to Course

Introduction to JavaScript

0% Complete
0/0 Steps
  1. JavaScript: Basics
    9Topics
    |
    10 Quizzes
  2. JavaScript: Variables
    9Topics
    |
    10 Quizzes
  3. JavaScript: Conditional Statements
    10Topics
    |
    11 Quizzes
  4. JavaScript: Functions
    10Topics
    |
    11 Quizzes
  5. JavaScript: Scope
    6Topics
    |
    7 Quizzes
  6. JavaScript: Arrays
    11Topics
    |
    12 Quizzes
  7. JavaScript: Loops
    8Topics
    |
    9 Quizzes
  8. JavaScript: High Order Functions
    3Topics
    |
    3 Quizzes
Lesson 4, Topic 5
In Progress

JavaScript: Default Parameters

Yasin Cakal 5 Nov 2021
Lesson Progress
0% Complete

The ability to use default parameters is one of the new features in ES6. If no argument is passed into the function or if the argument is undefined when it is called, default parameters allow parameters to have a predetermined value.

Consider the following code snippet, which makes use of a default parameter:

function studentName (name = 'Freshie') {
  console.log(`Hi ${name}!`)
}
 
studentName('Nick') // Output: Hi Nick!
studentName()       // Output: Hi Freshie!
  1. In the preceding example, we used the = operator to set the default value of the parameter name to Freshie. This will come in handy if we ever need to use a non-personalized default student name!
  2. The value of the argument is passed in when the code calls studentName('Nick'), and Nick will override the default parameter of Freshie to log Hi Nick! to the console.
  3. When studentName() is called without an argument, the default value of Freshie is used, and the message Hi Freshie! is printed to the console.

We account for situations in which an argument is not passed into a function that expects an argument by using a default parameter.

Responses

Leave a Reply

%d bloggers like this: