What is the meaning, scope, and how to explain the curly braces in the js function?

seeing that js is written in this way, I don"t know the meaning of the curly braces:

  function run(){
    {
      alert(1);
    }
 }

I don"t really understand that he just executed a method in curly braces, which has anything to do with the scope of the variable.

Oct.08,2021

the curly braces you have here belong to block scope


in ES6 .

Let me talk about another case
such as vue source code rollup

if ('development' !== 'production') {
    alert(1)
}

after compilation:

{
    alert(1)
}

ES6 adds block-level scope
variables within the block-level scope cannot be accessed outside the block-level scope. The var declaration does not recognize block-level scope, that is,

{
    var a = 1;
}

is not a block-level scope, but let,const can recognize a block-level scope


code block, which has two different meanings

  • For the block-level scope after es6 , many of the above answers say
  • is used as code segmentation, pure code maintenance requirements, such as two things to do in a function, which are completely unrelated, so that some of the code is not in the main process. For example, you need to record a log and wrap it with {} to make the code easier and easier to read. When combing the process, you do not need to care about this branch task.

    {
        do something...
    }
    {
        do something...
    }
    
    do something...

the problem of the landlord is said a lot above, but not so much.
tell me about syntax what means , how to explain and so on.
generate AST directly online and you'll know everything.


in ES6 you can understand curly braces as block-level scope,

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3fd62-2c4b8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3fd62-2c4b8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?