+ 2

JS - null (what does it stand for exactly in this case?)

Can anybody tell me what "null" stands for here? It outputs NaN when I remove it. Obviously it is a must to add it here. But why? What does it do exactly? function myFunction(w, x, y, z) { console.log(w + x + y + z); } var args = [1, 2, 3]; myFunction.apply(null, args.concat(4));

3rd Oct 2021, 5:27 PM
Aykut AKSAKAL
Aykut AKSAKAL - avatar
2 ответов
+ 1
The function apply() takes "this" pointer as the first, required argument. When using .call() or .apply(), null can be passed when you have no specific value that you want to set the this pointer to and you know that the function you are calling is not expecting this to have any specific value (e.g. it does not use this in its implementation). Null in js is used to demonstrate absence of any object value. It is like a place holder that means "no value"
4th Oct 2021, 4:28 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
Thanks for the reply :) So, the program doesn't understand where to concat '4' in the argument (to apply myFunction() ), should it be involved in the middle, at the start of argument elements or else. And "null" here works here as only a place holder telling the program ' "null" is no extra value but imagine it replacing the var args above and then concat 4 to the arg and apply myFunction () only. Am I right? *PS. I changed the order and put the bull after the concat like this: myFunction.apply(args.concat(4), null); it output NaN so, the order is significant I think.
4th Oct 2021, 5:07 PM
Aykut AKSAKAL
Aykut AKSAKAL - avatar