Returning functions

function creatgreater(name)
{
firstname = name.split(" ")[0];
function greater()
{
console.log("Hello " + firstname);
}

return greater;

}

let johncreater = creatgreater(“John Doe”);
let harrycreater = creatgreater(“Harry potter”);

johncreater();
harrycreater();

//EXPECTED
Hello John
Hello Harry

//OUTPUT
Hello Harry
Hello Harry

Variables defined without var/let keyword become global variables.
So when you set the value of firstname for the 1st time, you are actually declaring a global variable and assign ing the value.
That explains the program behavior.