Redeclare JavaScript Variable

前端 未结 3 513
一生所求
一生所求 2020-12-17 00:05

In the following code:

var greeting = \"hi\";

function changeGreeting() {
    if (greeting == \"hi\") {
        var greeting = \"hello\";
    }

    alert(g         


        
3条回答
  •  鱼传尺愫
    2020-12-17 01:07

    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

提交回复
热议问题