top of page
Logo der Online Agentur mdwp

Default Parameter

Default parameter in React JS refers to the default values set for function parameters. That means, when no arguments or values are passed during the function call, these default values are used.

Below is a simple explanation with a code snippet:

Let's consider a function `greeting` in JavaScript:

```javascript
function greeting(name, message) {
return `${message}, ${name}!`;
}
```
In the `greeting` function, we are passing two parameters: `name` and `message`. Now, if we want to provide each parameter with a default value, we can set the default parameters as follows:

```javascript
function greeting(name="User", message="Hello") {
return `${message}, ${name}!`;
}
```
In the above function, `name` has a default value of "User" and `message` has a default value of "Hello". If you call the function without providing any argument like `greeting()`, it will use these default parameters and the output will be "Hello, User!".

Although, if you provide arguments during the function call like `greeting('John', 'Welcome')`, it will override the default values and the output will be "Welcome, John!". This concept used in writing React JS components where props have default values. It's helpful because it makes the code more robust and avoids any errors that could be caused by undefined values.

bottom of page