Introduction
In JavaScript, function parameters default to undefined. In each programming language has one or the other specified default values for the in-built functions and properties. The same applies to JavaScript also where it enables the function to own their default values.
JavaScript has become the main base for several reasons and it is being utilized desperately in the most popular frameworks such as Angular, react, and Vue. In this blog, we will put rays on JavaScript default parameters.
In the following code, you’ll create a function that returns the square of a given number, defined as x:
function square(x) {
return x * x
}
square(5);
const no = 5;
square(no);
const defaultNumber = (number = 42) => console.log(number)
defaultNumber()
const defaultString = (string = ‘ABC’) => console.log(string)
defaultString()
const defaultBoolean = (boolean = true) => console.log(boolean)
defaultBoolean()
const defaultObject = (object = { id: 4 }) => console.log(object)
defaultObject()
const defaultArray = (array = [1, 2, 3]) => console.log(array)
defaultArray()
const defaultNull = (nullValue = null) => console.log(nullValue)
defaultNull()
This will give the output as follows:
42
ABC
true
{id:4}
[1,2,3]
Null
function settings(options = {}) {
const { theme, debug } = options
}
Syntax:
Function
[name]
([para1[ =defaultval1 ][, … ,paraN[ =defaultValN ]]])
{
statements
}
Example of Default function parameter
function multiply(a, b = 1) {
return a * b;
}
console.log(multiply(6, 2));
console.log(multiply(6));
[output: 12, 6]
In the above example, the first called multiply function in that value of a is 6 and value of b is 2, and at that time, value should be printed within the console. And therefore, the output is 12. In the second, the value of a is 6, and therefore, the value of b is 1 which is acquired in the function that’s b=1. So the second output is 6 by multiplying the value of a and b.
Let’s see the example of that:
function outer(
parameter = function inner() {
return 10
}
) {
return parameter()
}
outer()
It’s also possible to produce expressions as default values.
function sum(a= 2, b = a, c = a + b) {
console.log( a + b + c );
}
sum();
In the above example,
The default value of x is 2.
The default value of y is ready to the x parameter.
The default value of z is that the sum of x and y.
If you reference the parameter that has not been initialized yet, You’ll get an error.
Let’s see the example for that,
function sum( a = b, b = 1 ) {
console.log( a + b);
}
sum();
[ReferenceError: Cannot access ‘b’ before intialization]
Passing Function Value as Default Value
const sum = () => 15;
const calculate = function( a, b = a * sum() ) {
return a + b;
}
const result = calculate(10);
console.log(result);
[output: 160]
In the above example,
10 is passed to the calculate() function.
The value of x becomes 10.
The value of y becomes 150 because the sum function returns 15.
So the result is 160.
Passing undefined value
In JavaScript, when you pass undefined to a default parameter, the function takes the default value.
Let’s see an example,
function demo(x = 1) {
console.log(x);
}
demo(undefined);
[output: 1]
When we pass the undefined it takes the default value is 1.
So the output will be 1.
Multiple default parameters
function sum(a = 2, b = 3) {
return a + b
}
sum()
Let’s see the example:
function createUser(name, rank, userObj = { name, rank }) {
return userObj
}
const user = createUser(‘Abcd Efgh’, ‘demo’)
{name:” Abcd Efgh ”, rank:” demo ”}
Conclusion
In this blog, you learned what default parameters are and the way to use them. Now you’ll use default parameters and functions and easy to learn them. Another is you can also empty values or parameters to reduce the complexity of code.
In a world where digital presence is paramount, the question isn't whether you should do…
Over the years, people have experimented with various methods to maintain healthy and beautiful hair.…
Your brand more than developing an attractive and creative logo and infectious motto. It's the…
Introduction Are you someone who has suffered from a personal injury and want to file…
Operating from home has emerged as one of the most popular ways of doing jobs…
If the consequences of our society’s ever-growing debt are what worries you, then it is…