Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript curly braces with no function or json

Tags:

javascript

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. Normally, in my experience, curly braces wrap around a function...

function welcome(){ ...

...or a json JavaScript object

var attributes = { this : "that...

Can anyone tell me why there are curly braces with no text before them or after them? What does it do / what is the point of it?

like image 468
Djave Avatar asked Aug 08 '13 15:08

Djave


People also ask

What do empty curly brackets mean in JavaScript?

It means the variable is a dictionary that stores key value pairs. The subscript or the value within the [] brackets is the key and the value on the right side is the value.

Do you need curly braces in JavaScript?

Curly braces are required around object literals and functions. The are also required around conditional statements that are more than a single line of code. You can omit them around single line conditional statements, but it is good practice (and good for readability) to use them in all cases, in my opinion.

Does JSON require curly braces?

No. Curly braces do not have to be escaped in JSON.


1 Answers

It's a block and completely pointless unless you label it:

block: {
    var s_account="blog";
    console.log("executed");
    break block;
    console.log("not executed");
}
like image 195
Esailija Avatar answered Oct 02 '22 13:10

Esailija