Access the full course here: javabrains.io/... Thanks to functions being first class values, they can be passed as arguments to other functions. Let's see how that works!
Thank you! I was watching another tutorial and he did this, but didn't explain it, and I was left scratching my head. After a bit of Googling, I found this video and it helped me immensely! Thank you very much!! Hoping to see some Vue stuff from you in the near future! :)
Hi, I just want to ask to ask about return statement, in the very first function if I provide a return statement then how can I access the return statement with the second function or pass a parameter for return statement.
There is no semicolon after the executor function. It is defined to be as a value to a variable in the video. Is it correct or JS interpreter just ignores it but it should be there?
+Rhitik Bhatt (Lambu) You are right. It's good practice to add a semicolon at the end of that expression there, and I forgot to add it. But it's not mandatory though. The JavaScript interpreter figures out that it's the end of the line anyway, which is why it works.
hello there , can you show this with currying ? I am struck in a middleware in redux project code where we are currying some action I am not getting an argument is being called as function/method. export const loggerMiddleware = (store) => (next) => (action) => { if (!action.type) { return next(action); } console.log('type: ', action.type); console.log('payload: ', action.payload); console.log('currentState: ', store.getState()); next(action);//I am not getting this line console.log('next state: ', store.getState()); };
var f =function (name){ console.log("Hello "+name); }; var executor = function(fn,name){ fn(name); } executor(f,"vinay"); From bottom line 3, why we have to give the argument name in that?
hi Kaushik, can you please explain me the output of this code var f= function (){ console.log ("hello")}; var check = function(f) { console.log ("just checking " + f())}; check(f); output is : hello just checking undefined Why there is undefined printed after "just checking" thanks in anticitpation
I understand callbacks in C++ where you pass a 'pointer to a function' to another thread and wait for the callback result. This does not appear to be the same as a C++ callback. Maybe I'm not understanding the usecase in javascript, but this seems pointless. What problem does this technique solve?
Why do functions get invoked as CALLBACK functions when stored in a variable? And yet when a normal function is stored in a variable it is not invoked. Where is the critical difference?
I was just trying some sample program to follow the tutorial and I came across the following behaviour. var func = function(){ return 20; }; function print(fn){ console.log(fn()); } print(func); When I use the function name as print , nothing gets printed to console, but print is not even a reserved word. Can you please tell me why it behaves like this.
So what is the point of giving a _name_ in the function expression, if you cannot call it later on in the code? Here is the example code with comments: jsfiddle.net/oleshko/5jdt769b
+Aleksandr Oleshko Honestly, I have *always* used anonymous function expressions, because it doesn't make sense to have two names for the same thing anyway. Good to know that it doesn't even work! :)
You can use the name inside of the function you create with the function expression, but not outside. That way, you can call the function recursively regardless of the variable name it has on the outside. Another benefit is that some browsers / JS runtime environments will show the name in debugging, which may be helpful. var fn = function factorial(n){ if (n < 0) return NaN; if (n == 0) return 1; return n * factorial(n-1); }; var g = fn; var res_fn = fn(3); // doesn't depend on the name fn or g, still works var res_g = g(3); factorial(3); // doesn't work since the name isn't available outside
hii! nice video! very informative!! can u please solve this doubt of mine.... in a function I have seen people put like function trial(obj){}. here, obj has some definite meaning or is like just some word.. pls reply asap
I still don't get point of all this. WHERE or WHEN would this be applicable in a real life programming job? Instead of having a second function execute the first function, why not just simply execute the first function itself? This just seems like a pretentious convoluted way of executing an initial function, I really don't see how this makes "asynchronous programming" possible.
ReactJS does pass functions around like this. However, I have never done this with vanilla javascript. Somebody asked me this during an interview for "JavaScript Developer" 3 weeks ago. Chance are that is one of the only times somebody will ask you to write this puzzle.
Hey Koushik, wonderful explanation. Thanks for keeping it simple as usual.
Aha ! A big moment of understanding! Thanks for making some long-term confusion instantly get resolved.
Thank you for Clearing the Concept
Thank you! I was watching another tutorial and he did this, but didn't explain it, and I was left scratching my head. After a bit of Googling, I found this video and it helped me immensely! Thank you very much!! Hoping to see some Vue stuff from you in the near future! :)
Superbly Explained !!
Wow, Javascript is weird !
Very helpful, thank you!
Thank you very much!
Hi, I just want to ask to ask about return statement, in the very first function if I provide a return statement then how can I access the return statement with the second function or pass a parameter for return statement.
thank you!!!
There is no semicolon after the executor function.
It is defined to be as a value to a variable in the video.
Is it correct or JS interpreter just ignores it but it should be there?
+Rhitik Bhatt (Lambu) You are right. It's good practice to add a semicolon at the end of that expression there, and I forgot to add it. But it's not mandatory though. The JavaScript interpreter figures out that it's the end of the line anyway, which is why it works.
hello there , can you show this with currying ?
I am struck in a middleware in redux project code where we are currying some action I am not getting an argument is being called as function/method.
export const loggerMiddleware = (store) => (next) => (action) => {
if (!action.type) {
return next(action);
}
console.log('type: ', action.type);
console.log('payload: ', action.payload);
console.log('currentState: ', store.getState());
next(action);//I am not getting this line
console.log('next state: ', store.getState());
};
var f =function (name){
console.log("Hello "+name);
};
var executor = function(fn,name){
fn(name);
}
executor(f,"vinay");
From bottom line 3, why we have to give the argument name in that?
hello, and thanks, just question, what if we have many argument like name ,sruname and so on ,and instead of console log return ?
hi Kaushik, can you please explain me the output of this code
var f= function (){ console.log ("hello")};
var check = function(f)
{ console.log ("just checking " + f())};
check(f);
output is : hello
just checking undefined
Why there is undefined printed after "just checking"
thanks in anticitpation
I understand callbacks in C++ where you pass a 'pointer to a function' to another thread and wait for the callback result. This does not appear to be the same as a C++ callback. Maybe I'm not understanding the usecase in javascript, but this seems pointless. What problem does this technique solve?
Why do functions get invoked as CALLBACK functions when stored in a variable? And yet when a normal function is stored in a variable it is not invoked. Where is the critical difference?
what is the advantage of this usage? Where can this be used fairly?
You can encapsulate behaviors and send them as a parameter or return them. In another language you would do this with nested if-else
How does line 7 or fn know to call line 1 (var f)?
Would be great if you could have added the word "Call back" function concept in this.
i thought this is a callback'
Hi Koushik, i am not able to run javascript code from scratchpad. Should i download any plugins ? Please help me out. Thanks in advance.
Scratchpad is a part of Mozilla Firefox
I was just trying some sample program to follow the tutorial and I came across the following behaviour.
var func = function(){
return 20;
};
function print(fn){
console.log(fn());
}
print(func);
When I use the function name as print , nothing gets printed to console, but print is not even a reserved word.
Can you please tell me why it behaves like this.
try using "use strict"; and see if print works.
var f=function (){}; and function f(){}; seems to be the same thing.
you cant use just "f" as a function on second expression.. you have to put "f()" like this..
So what is the point of giving a _name_ in the function expression, if you cannot call it later on in the code?
Here is the example code with comments:
jsfiddle.net/oleshko/5jdt769b
+Aleksandr Oleshko Honestly, I have *always* used anonymous function expressions, because it doesn't make sense to have two names for the same thing anyway. Good to know that it doesn't even work! :)
You can use the name inside of the function you create with the function expression, but not outside. That way, you can call the function recursively regardless of the variable name it has on the outside. Another benefit is that some browsers / JS runtime environments will show the name in debugging, which may be helpful.
var fn = function factorial(n){
if (n < 0) return NaN;
if (n == 0) return 1;
return n * factorial(n-1);
};
var g = fn;
var res_fn = fn(3); // doesn't depend on the name fn or g, still works
var res_g = g(3);
factorial(3); // doesn't work since the name isn't available outside
hii! nice video! very informative!! can u please solve this doubt of mine.... in a function I have seen people put like function trial(obj){}. here, obj has some definite meaning or is like just some word.. pls reply asap
just some word
Over simplified. In real world scenarios it's much more complicated.
got it
thank you so `${much}`
i see indian, i click
I still don't get point of all this. WHERE or WHEN would this be applicable in a real life programming job? Instead of having a second function execute the first function, why not just simply execute the first function itself?
This just seems like a pretentious convoluted way of executing an initial function, I really don't see how this makes "asynchronous programming" possible.
ReactJS does pass functions around like this. However, I have never done this with vanilla javascript.
Somebody asked me this during an interview for "JavaScript Developer" 3 weeks ago. Chance are that is one of the only times somebody will ask you to write this puzzle.
do you remember when the terminator just loaded a program to fly a plane, then just like that he could fly a plane? yea... something like that
Why you use dip... Smokeless tobacco is just as dangerous
Please speak clearly. Thank you