- ES6 provides a feature for passing default values of input parameters.
- The default value of a parameter can be configured using Assignment Parameter(=).
- In this Demo, “We will learn to configure default values of input parameters”.
- The following code contains 2 method multiplyTwoNumbers() for multiplication of 2 numbers and Student() for creating student object.The multiplyTwoNumbers() takes two parameters number1 and number2.The number2 parameter has default value 1.When the 2nd parameter is not supplied to multiplyTwoNumbers() the number2 takes the default value 1. The Student() function has 3 different properties name,score and subject. The default values of score and subject are 0 and Computer.
var multiplyTwoNumbers = function(number1, number2 = 1){ return number1*number2; }; var result2 = multiplyTwoNumbers(5); console.log("Result: ",result2); var Student = function(name, score = 0, subject = "Mathematics"){ this.name = name; this.score = score; this.subject = subject; }; var student1 = new Student("Sandeep"); console.log("Student name: ",student1.name); console.log("Student score: ",student1.score); console.log("Student subject: ",student1.subject);
- The output of the previous code can be found in the following JSBIN link.