JavaScript curly braces with no function or json

后端 未结 6 806
别那么骄傲
别那么骄傲 2020-12-06 16:16

Just opened a client\'s javascript file and the first lines are along the lines of this:

{
    var s_account=\"blog\";
}

Which I don\'t get

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 17:16

    It's called a block statement. It lets you group expressions. It's normally used with control structures like if and while, but can also be used on its own.

    Since JavaScript doesn't have block scope, the code runs in the same scope (as if the {} weren't there).

    Example:

    {
        var s_account="blog";
    }
    
    console.log(s_account);
    

    That works fine.

提交回复
热议问题