- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is “Parameters without defaults after default parameters in JavaScript”
The default parameter came to handle function parameters with ease. Default parameters allow you to initialize formal parameters with default values. This is possible only if no value or undefined is passed. With ES6, you can easily set default parameters. Let’s see an example
Example
<html> <body> <script> // default is set to 1 function inc(val1, inc = 1) { return val1 + inc; } document.write(inc(10,10)); document.write("<br>"); document.write(inc(10)); </script> </body> </html>
The following code runs correctly showing the working of parameters from left-to-right. It overwrites default parameters even if parameters are added after without default.
Example
<html> <body> <script> function display(val1 = 10, val2) { return [val1, val2]; } document.write(display()); document.write(display(20)); </script> </body> </html>
Advertisements