Doubt in callback function

function greeting(name) {
alert('Hello ’ + name);
}

function processUserInput(callback) {
var name = prompt(‘Please enter your name.’);
callback(name);
}

processUserInput(greeting);

In the processUserInput(callback), does callback(name) refers to greeting(name)?

Hey there!

Yes, you are correct. Just be careful some of those single quotes in the code you posted are actually backticks(`) so, the code will not run. Here it is without those items fixed.

function greeting(name) {
  alert("Hello " + name);
}

function processUserInput(callback) {
  var name = prompt("Please enter your name.");
  callback(name);
}

processUserInput(greeting);

On which page did you get the code example?

1 Like

If they are backticks, then also the code should run I suppose, because it will be the template literal.
I just searched for ‘callback functions mdn’, it gave me the following page.

Yes indeed. If they are both backticks it will work. However, unless you are doing string interpolation or creating tagged templates, I would recommend sticking with plain strings.

1 Like

I see on that page it does use plain strings. Perhaps during copying it changed for some reason. Here is a Codepen with the running code as well: https://codepen.io/schalkneethling/pen/mdpGxPo?editors=1111

2 Likes

yes, thank u so much @schalkneethling

1 Like

Yeah, that’s Discourse converting vertical quotes to typographical quotes in some situations when not using code fences.

1 Like