I know the the pure functions has some rules below.
could someone help me list out the all pure functions in Javascript until ES2020?
I know slice is a pure function
Array.prototype.slice
Note: This list includes the first standard the method appears in. For clarity for older standards:
at
[ES2022]
concat
[ES1999]
entries
2 [ES2015]
every
1 [ES2009]
filter
1 [ES2009]
find
1 [ES2015]
findLast
1 [ES2023]
findIndex
1 [ES2015]
findLastIndex
1 [ES2023]
flat
[ES2020]
flatMap()
1 [ES2020]
forEach
3 [ES2009]
includes
[ES2015]
indexOf
[ES2009]
join
[ES1997]
keys
2 [ES2015]
lastIndexOf
[ES2009]
map
1 [ES2009]
reduce
1 [ES2009]
reduceRight
1 [ES2009]
slice
[ES1999]
some
1 [ES2009]
toReversed
4 - pure version of reverse
[ES2023]
values
2 [ES2015]
with
4 - pure version of assigning a value to an index [ES2023]
@@iterator
2 [ES2015]
1 These accept a callback parameter. While the method will not itself alter any of the arguments, the callback might. So, technically it's possible to pass an impure callback which makes the whole operation impure.
2 Returns an iterator. The operation is pure but using the iterator is not. For example, you might get different results if you get all the values of the iterator immediately or you get some, then the array is altered in-place, then you continue getting values from the iterator.
3 Technically pure by its operation but the expectation is that the callback it accepts will have side effects. If the callback doesn't have side effects, it's likely not going to be a useful operation.
4 This is a pure version of an existing method. Instead of mutating the current instance, it returns a new array with the operation applied to it.
References:
This Standard has been adopted as 3rd Edition of ECMA-262 by the ECMA General Assembly in December, 1999