Understanding this JS syntax [duplicate] - javascript

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Higher-order functions in Javascript
(5 answers)
Understanding Higher Order functions in Javascript
(2 answers)
Closed 3 years ago.
I am practising ES6 and I have this code:
const over = (...fns) => (...args) =>
fns.map(fn => fn.apply(null, args));
const minMax = over(Math.min, Math.max);
console.log(minMax(1, 2, 3, 4, 5));
console.log(minMax(1, 2, 5, 4, 3));
console.log(minMax(1, 2, 5, -4, 3));
The goal is to get the minimum and the maximun values between the numbers passed as arguments.
I could understand almost everything, the dynamic is very clear, with one exception, I know that args refers to the parameters coming from minMax(), but I couldn't get how the code recognize it.
My guess is: since we have two functions, over() and minMax(), when called, they are automatically read in this order, that is why the code knows that the first anonymous function refers to over() and the second one to minMax(). But this is just a guess, I don't know if I am right.
What is exactly happening here?

Related

Assigning value to an array or object inside a function [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 1 year ago.
I am learning javascript and I read that in javascript array and objects are by default passed as a reference. So when I do this:-
var a = [2, 3, 4]
function pushEl(a, num) {
a.push(num)
}
pushEl(a, 5)
console.log(a)
The output is as expected which is
[2,3,4,5]
But what I am not able to understand is that when I assign value to an array or an object inside the function the original array/object is not changed.
var a = [2, 1, 3]
function change(a) {
a = [1, 2]
}
change(a)
console.log(a)
I expect the output to be [1,2] but the output is [2,1,3].
If the array is passed by reference then the changes should have been reflected in the original array too.
Can anybody tell me what concept I'm missing here?

What is going on with this assignment? [duplicate]

This question already has answers here:
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
What is destructuring assignment and its uses?
(3 answers)
Closed 2 years ago.
Can someone explain me what is going on here?
const [counter, setCounter] = useState(0);
Is it some kind of assignment?
I think you might need to take a look into the React Hooks docs
It's a destructuring assignment on the results of calling the useState function:
the first element of the array is the value
the second element is a function to change value.

how is undefined treated while looping [duplicate]

This question already has answers here:
Why does JavaScript map function return undefined?
(13 answers)
Removing undefined values from Array
(14 answers)
Closed 2 years ago.
[1, 3, 4, 5, 5, undefined, 4, 3, 4].map((item) => {
if (item) {
return item
}
})
The output of the above code is
Why is undefined also returned, shouldn't it be stopped by the if loop?
Your map function doesn't return anything explicitly if the item is falsy, hence it returns undefined (per default).
As another comment has pointed out, you are probably looking for Array.prototype.filter:
console.log([1,3,4,5,5,undefined,4,3,4].filter((item)=>{
if(item){
return item;
}
}));
Note that the above is slightly verbose, .filter(x => x) would suffice, filter calls ToBoolean on the return anyways. It will still filter out any falsy values though, e.g. 0.

Can someone explain why the extra ()? [duplicate]

This question already has answers here:
What do empty parentheses () after a function declaration do in javascript? [duplicate]
(4 answers)
Closed 3 years ago.
I am learning JavaScript from freecodecamp and there is a function that I don't understand the meaning of the extra (), I will attach the code, sorry if this already been asked before
I know that I need to the () in order to get the outcome but I cannot explain the reason behind it
const sum = (function() {
return function sum(...args) {
return args.reduce((a, b) => a + b, 0);
};
})(); //what I am asking is (func....)(); what is the reason behind those 2 ()?
console.log(sum(1, 2, 3, 4));
The outcome is 10, which I understand the function, I just don't understand the meaning behind it
It's an IIFE (Immediately Invoked Function Expression) - it's a JavaScript function that runs as soon as it is defined.
https://developer.mozilla.org/en-US/docs/Glossary/IIFE

What does this documentation syntax mean at MDN for a JavaScript function? [duplicate]

This question already has answers here:
How to interpret function parameters in software and language documentation?
(4 answers)
Closed 7 years ago.
I'm learning JavaScript through "Eloquent JavaScript" manual and now I'm doing exercises from chapter 5 "High-Order Functions". One of the functions this chapter introduces you is "reduce". I understand how it works, my problem comes up when I try to understand its definition at MDN. I don't understand syntax definition it gives:
arr.reduce(callback[, initialValue])
This syntax sections is followed by the section called Parameters. These are:
callback
previousValue
currentValue
index
array
initialValue (optional)
What I don't understand is what do those square brackets and the comma mean? Because when I see square brackets immediately I think in arrays. And why is just initialValue in the definition and not the others parameters? Why there is no space between square brackets and callback?
Because below there are two examples:
Example 1
[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array) {
return previousValue + currentValue;
});
Example 2
var total = [0, 1, 2, 3].reduce(function(a, b) {
return a + b;
});
// total == 6
and I don't know how do they fit into the definition.
Thanks
It's usually a convention for API documentations to use [] to denote optional parameters. However, the [] is not part of the syntax on usage. It's just documentation convention.
As already explained in other answers, the parameters inside of the "[]" are optional. Regarding the question as to why the "other parameters" (i.e. previousValue etc.) are not there, those are parameters to callback and not to reduce. So callback will receive those arguments on each run of reduce.

Categories

Resources