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.
Namaste UI collaborates closely with clients to develop tailored guest posting strategies that align with their unique goals and target audiences. Their commitment to delivering high-quality, niche-specific content ensures that each guest post not only meets but exceeds the expectations of both clients and the hosting platforms. Connect with us on social media for the latest updates on guest posting trends, outreach strategies, and digital marketing tips. For any types of guest posting services, contact us on info[at]namasteui.com.
We've gotten so used to seeing men streetwear joggers, ripped jeans, and sleeveless shirts. Hair…
When it comes to festivals, the options for wedding jewellery are endless. You can go…
Whether it concerns your home or an office building, the state of a property’s windows…
You know that running an environmentally sustainable business is the right thing to do. But…
If you are in a financial crisis , or need to start a new business…
To realize the strategic advantage from the fleet management system (FMS) and differentiate the business…