0

Can anyone explain me this code. Also explain this syntax ${name} (I've already google it, but got nothing much).

function* ask() { 
   const name = yield "What is your name?"; 
   const sport = yield "What is your favorite sport?"; 
   return `${name}'s favorite sport is ${sport}`; 
}  
const it = ask(); 
console.log(it.next()); 
console.log(it.next('Ethan'));  
console.log(it.next('Cricket')); 
kvinbabbar
  • 185
  • 1
  • 4
  • 15
  • 1
    here is the explanation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function* – Randy Casburn May 19 '18 at 04:55
  • And the template literal ( `${name}` ) explanation is here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals – Randy Casburn May 19 '18 at 04:58

1 Answers1

1
  1. The function you are declaring is a generator. You can find more about generators here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function%2A
  2. ${name} This is a way to add a variable's value to the string. This is called literals. You can find out more about literals here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Let me know if you are still confused.

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
Ahmed Waqas
  • 245
  • 1
  • 3
  • 14