0

When I use alert() or console.log() it works properly.

function greeting(name) {
  alert('Hello ' + name);
  console.log(`Hello ${name}`)
}

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

processUserInput(greeting);

But when I use return it returns undefined

function greeting(name) {
    let returnthis = `hello ${name}`
    return returnthis
  }


  
  function processUserInput(callback) {
    var name = prompt('Please enter your name.');
    callback(name);
  }
  
  console.log(processUserInput(greeting));

Why is this happening?

How to return value?

I read this answer but can't understand.

black
  • 703
  • 2
  • 7
  • 16

2 Answers2

2

You need to return callback also

function greeting(name) {
  return `hello ${name}`
}



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

console.log(processUserInput(greeting));
prasanth
  • 22,145
  • 4
  • 29
  • 53
2

Because processUserInput returns nothing. Do:

  return callback(name);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151