Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn ECMAScript

You're reading from   Learn ECMAScript Discover the latest ECMAScript features in order to write cleaner code and learn the fundamentals of JavaScript

Arrow left icon
Product type Paperback
Published in Feb 2018
Publisher
ISBN-13 9781788620062
Length 298 pages
Edition 2nd Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Narayan Prusty Narayan Prusty
Author Profile Icon Narayan Prusty
Narayan Prusty
MEHUL MOHAN MEHUL MOHAN
Author Profile Icon MEHUL MOHAN
MEHUL MOHAN
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Getting Started with ECMAScript FREE CHAPTER 2. Knowing Your Library 3. Using Iterators 4. Asynchronous Programming 5. Modular Programming 6. Implementing the Reflect API 7. Proxies 8. Classes 9. JavaScript on the Web 10. Storage APIs in JavaScript 11. Web and Service Workers 12. Shared Memory and Atomics 13. Other Books You May Enjoy

Default parameter values

In JavaScript, there is no defined way to assign default values to function parameters that are not passed. So programmers usually check for parameters with the undefined value (as it is the default value for missing parameters) and assign the default values to them. The following example demonstrates how to do this:

 function myFunction(x, y, z) {
x = x === undefined ? 1 : x;
y = y === undefined ? 2 : y;
z = z === undefined ? 3 : z;
console.log(x, y, z); //Output "6 7 3"
}
myFunction(6, 7);

This can be done in an easier way by providing a default value to function arguments. Here is the code that demonstrates how to do this:

function myFunction(x = 1, y = 2, z = 3) {
console.log(x, y, z);
}
myFunction(6,7); // Outputs 6 7 3

In the preceding code block, since we've passed first two arguments in the function calling statement, the default values (that is x = 1 and y = 2) will be overwritten with our passed values (that is x = 6 and y = 7). The third argument is not passed, hence its default value (that is z =3) is used.

Also, passing undefined is considered as missing an argument. The following example demonstrates this:

function myFunction(x = 1, y = 2, z = 3) {
console.log(x, y, z); // Outputs "1 7 9"
}
myFunction(undefined,7,9);

A similar thing happens here. If you want to omit the first argument, just pass undefined in that.

Defaults can also be expressions. The following example demonstrates this:

 function myFunction(x = 1, y = 2, z = x + y) {
console.log(x, y, z); // Output "6 7 13"
}
myFunction(6,7);

Here, we're making use of the argument variables themselves inside a default argument value! That is, whatever you pass as the first two arguments, if the third argument is not passed it'll take the value of the sum of the first two arguments. Since we passed 6 and 7 to the first and second argument, z becomes 6 + 7 = 13.

You have been reading a chapter from
Learn ECMAScript - Second Edition
Published in: Feb 2018
Publisher:
ISBN-13: 9781788620062
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime