top of page
Logo der Online Agentur mdwp

Pure Function

A pure function in React JS is a type of function that always returns the same result if the same arguments are passed. It does not depend on any states or values that may change during the program's execution and does not mutate any of the arguments or externally referenced variables. Pure functions have two main properties:

1. Given the same input, will always return the same output.
2. Pure functions only depend on the input provided to them and do not use any outside or global state.

Pure functions are important in React because they help to keep your components predictable and maintainable.

Here is an example of a pure function:

```
function addTwoNumbers(x, y){
return x + y;
}

console.log(addTwoNumbers(2, 3)); // returns 5
```

No matter how many times we call this function with the same arguments (2,3), it will always return the same result (5). The function does not depend on any outside value or state, and it does not change or impact any value or state outside of the function. Thus, it is considered a pure function.

bottom of page