- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM Node Lists
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
- JavaScript Useful Resources
- JavaScript - Questions And Answers
- JavaScript - Quick Guide
- JavaScript - Functions
- JavaScript - Resources
JavaScript - Default Parameters
The default parameters in JavaScript are a feature that allows you to specify a default value for a function parameter. The concept of the default parameters was introduced in the ES6. We can initialize the parameters with the default values. So, if the function is called with missing argument or argument with an undefined value, it uses the default value of the parameter in the function.
The default value of a function parameter is "undefined" in JavaScript. When a function is called with missing arguments the parameters are set to 'undefined'. The undefined parameter values are acceptable but can generate unusual outcomes.
Before the ES6 version of JavaScript, we needed to check whether the parameter value was "undefined" inside the function body. If yes, they need to initialize the parameter with the proper value.
Let's understand it via the example below.
function sum(p, q) { return p + q; } sum(10, 20); // 30 sum(10); // NaN sum(); // NaN
In this example, we observe the following −
sum(10, 20) returns the sum of the two arguments, i.e., 30. Here both arguments are passed.
sum(10) returns NaN. Here only one argument is passed. The second parameter q is set to undefined. Mathematical operation on undefined returns NaN.
sum() also returns NaN. Here both arguments are missing. So they are set to undefined.
When we call the function with missing argument values, it returns NaN which is unusual.
To overcome this problem, we can use default parameter values to be used if function is called with missing argument values.
Default Parameters Syntax
The syntax to use the function default parameters in JavaScript is as follows –
function functName(param1 = defaultValue1, param2 = DefaultValue2, ..) { // Use parameters here }
In the above syntax, the default value of the param1 is set to defaultValue1, and the default value of the param2 is set to defaultValue2.
Let's look at the example below −
Example (Default parameters)
In the below code, parameter p and q contains the 30 and 40 default values, respectively.
In the output, unlike the first example, you can see that it returns the sum of the default parameter values when any argument is missing.
<html> <body> <p id = "output"> </p> <script> let output = document.getElementById("output"); function sum(p = 30, q = 40) { return p + q; } output.innerHTML += "sum(10, 20) -> " + sum(10, 20) + "<br>"; // 10 + 20 = 30 output.innerHTML += "sum(10) -> " + sum(10) + "<br>"; // 10 + 40 = 50 output.innerHTML += "sum() -> " + sum() + "<br>"; // 30 + 40 = 70 </script> </body> </html>
Output
sum(10, 20) -> 30 sum(10) -> 50 sum() -> 70
Passing an expression as a default parameter value
We can pass an expression as a default parameter value to a JavaScript function. The expression can also contain the values of the previous parameters.
Example
We pass the expression as a default parameter value in the code below. The expression contains the value of the previous parameters.
In the output of the second function call, you can observe that the value of the r is 100, which is equal to (p = 5) * (q = 10) * 2. We haven't passed any argument in the third function call, so all parameters take the default value.
<html> <body> <p id = "output"> </p> <script> let output = document.getElementById("output"); function sum(p = 2, q = p * 2, r = p * q * 2) { return p + q + r; } output.innerHTML += "sum(5, 10, 15) -> " + sum(5, 10, 15) + "<br>"; // 5 + 10 + 15 = 30 output.innerHTML += "sum(5, 10) -> " + sum(5, 10) + "<br>"; // 5 + 10 + (5 * 10 * 2) = 115 output.innerHTML += "sum() -> " + sum() + "<br>"; // 2 + 4 + 16 = 22 </script> </body> </html>
Output
sum(5, 10, 15) -> 30 sum(5, 10) -> 115 sum() -> 22
You can't use the uninitialized parameter in the expression. Otherwise, the code will raise a reference error.
Passing Undefined Argument
When you pass the undefined argument to the function call, the JavaScript function definition uses the default parameter values to avoid unnecessary errors.
<html> <body> <p id="output"> </p> <script> let output = document.getElementById("output"); function sum(p = 24, q = 26) { return p + q; } output.innerHTML += "sum(5, undefined) -> " +sum(5, undefined)+"<br>"; // 5 + 26 = 31 output.innerHTML += "sum(undefined) -> " + sum(undefined) + "<br>"; // 24 + 26 = 50 </script> </body> </html>
Output
sum(5, undefined) -> 31 sum(undefined) -> 50
Function expression as a default parameter
The JavaScript function expression can also be paased as a fucntion default parameter.
In the example below, the getNum() function returns 5. We used the function expression as a default value of parameter q.
The output shows that when the second argument is missing, the parameter uses the value returned from the getNum() function.
<html> <body> <p id = "output"> </p> <script> let output = document.getElementById("output"); function getNum() { return 5; } function mul(p = 5, q = getNum()) { return p * q; } output.innerHTML += "mul(10) -> " + mul(10) + "<br/>"; output.innerHTML += "mul() -> " + mul() + "<br/>"; </script> </body> </html>
Output
mul(10) -> 50 mul() -> 25
Function Optional Parameters
The function default parameters are also called the optional parameters, as the function will give output without any error even if we don't pass the arguments for the optional parameter.
You should pass all required parameters at the start and optional parameters at the function end.
function sum(p, q=10){ return p+q; }
In the above JavaScript code snippet, we put the optional parameter q at the end of the parameter list.
Example
The JavaScript code below shows that the first parameter is required, and the second parameter is optional.
<html> <body> <p id = "output"> </p> <script> let output = document.getElementById("output"); function func(p, q=10) { return p + q; } output.innerHTML += "func(10, 20) -> " + func(10, 20); </script> </body> </html>
Output
func(10, 20) -> 30
If we put the optional parameter at beginning, we may encounter error while calling the function with undefined value.
function sum(p=10, q){ return p+q; } sum(,10) // Error sum(10) // NaN
So, if you pass only a single argument, it replaces the default value of the first parameter, and the second parameter remains undefined.