I bet, you don’t know about JavaScript’s pure functions (Pure functions Ep - 2)

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

Hi Vasant, in the filter method I believe it would be correct to mention that provided the callback function doesn't introduce some kind of side effect, it is a pure function. What do you think?
Also, you mentioned to check whether another function is being called inside a function is a condition to check whether a function is pure or not, from that point of view, we do execute a callback function inside filter, then how come we don't call it impure? Looking forward to your response.

AparajitGarg
Автор

q1: console.log is a build in function.it doesn't make any interactions .it's just used to clarified the values before declared for interactions in ui

srikanthravi
Автор

Are you sure that console.log is a pure function?? I agree with your point that console.log is a deterministic function as it always returns undefined. But then, being deterministic is not the only constraint for a function to be called pure. Along with being deterministic, the function should also not mutate anything outside of its boundary/scope; And console.log prints something on the screen which is definitely outside of its scope. So I don't think calling it a pure function would be correct.

ritikraj.
Автор

console.log() is an inbuilt function in javascript.

s-qcns
Автор

Here's what i got as a result when i searched is console.log is a pure function on chat GPT, i re-checked this with some other docs and videos as well that were telling the same thing :

No, console.log is not a pure function in JavaScript. A pure function is a function that always produces the same output for the same input and has no side effects. console.log does not meet these criteria because it has a side effect: it outputs data to the console.

In functional programming, pure functions are preferred because they are predictable, testable, and don't introduce hidden side effects that can make code harder to reason about. Pure functions have the property of referential transparency, which means you can replace a function call with its result without changing the behavior of the program.

AnkitKumar-ihmt
Автор

If a function has another function called inside its body, then it is non-deterministic, as the definition of that function might change anytime. So, that is not a pure function.

as_if
Автор

if we use pure function inside a pure function then outer function is still pure because inner pure function have no side effect
Let see Example:
function outerFun(x) {
return function innerFun(x) {
return x + x;
}
}
outerFun(3); // result is 6

usmanregard
Автор

console.log(window.console) this will list out all the available functions associated with console... Even one will see "log" in the list 😁

satyaprakashsahoo
Автор

console.log() is not a pure function. A pure function is a function that, given the same input, will always return the same output and has no side effects. console.log() has a side effect because it performs an action—printing to the console—rather than simply returning a value based on its inputs.

Pure functions do not modify external state or have observable side effects, and they are deterministic. In contrast, console.log() is designed to have a side effect by displaying information in the console, and it doesn't return a value that depends solely on its inputs.

mdshamimhossain
welcome to shbcf.ru