Function as an argument to a function

Function as an argument to a function
0

#1

Hello, everyone. I was going through JavaScript course and found myself a bit stuck on one thing. It’s about functions and their arguments. Could someone explain it, please?
When you write a function, it has one or more arguments and you pass some values to this function using these arguments? Also, you can use another function as the agrument to your function. But how you give your argument/function it’s own arguments? Especially if this function is anonymous and you can’t call it(or can you?).

function Hello (a, b) {
// some code here
}; 

It’s obvious, you just call “Hello(5, 10);” for example, and 5 goes as “a” and 10 goes as “b”.
But what with

function Hello (function (c, d) {
//some code here
}) {
//some more code here
};

this example? How can you call the “Hello” function and give it’s argument “c” and “d” arguments?
I would really appreciate it, if someone explained it to me, at least a bit.


#2

Do you have a full example of the Hello function? This syntax you have written does not appear to be valid.

Not sure if this is what you are after, but I can pass a function to another function and then call that function inside it.

function sayMyName(nameFunction) {
  console.log(nameFunction('Randell', 'Dawson'))
}

sayMyName(function(firstName, lastName){
  return 'My name is ' + firstName + ' ' + lastName;
});

#3

Yeah, i may have written something wrong.
What i mean is

const myFunc = function() {
  const myVar = "value";
  return myVar;
}

looks like this piece of code(from ES6 challenge).
But if you change it to

const myFunc = function(value) {
  const myVar = value;
  return myVar;
}

how do you pass “value” to this function? It has no name, so i’m a bit lost.
Or code from another ES6 challenge

const profileUpdate = (profileData) => {
  const { name, age, nationality, location } = profileData;
  // do something with these variables
}

How do you pass “profileData” to this function?

And according to your words(if i got it right), it should be:

function Hello (a, b) {
// code
};
Hello(function(){
// code
}, b);

if there is need to pass a function as an agrument?


#4

You would simply call it like:

console.log(myFunc(100)); // displays 100

#5

Could you please post the link to this question in the curriculum, so that we may have a better understanding of the question?


#6

From randelldawson’s anwer it looks like i’ve confused myself, after looking at all these " const a = function(value) {};" examples and got “function as an argument to another function” wrong. It’s not one exact challenge in the curriculum.


#7

Thank you for the answers, i think i got it, somewhat.