In the following code:
var greeting = \"hi\"; function changeGreeting() { if (greeting == \"hi\") { var greeting = \"hello\"; } alert(g
In your first code snippet, you're checking global variable, which doesn't exist -> doesn't pass if condition.
Check out this on javascript scopes and how to work with variables Javascript garden - function scopes