Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
What is the purpose of these superfluous curly braces?

So I have recently started at a new place of employment and I have run across a format of javascript which makes me question its purpose. ( in particular the brackets {})

var _occurrences = getOccurrences($('#ddlTours').val());
{
    var _occurrence = getObjectByValue(_occurrences, 'tourID', booking.tourID);
    {
        _occurrenceID = _occurrence.occurrenceID;
    }
}

To me it almost looks like an attempted object construction. i.e.

var _occurrences : // Ignoring = getOccurrences($('#ddlTours').val());
{
    _occurrence : // Ignoring getObjectByValue(_occurrences, 'tourID', booking.tourID);
    {
        _occurrenceID : _occurrence.occurrenceID;
    }
}

But as I understand it will execute it like.

var _occurrences = getOccurrences($('#ddlTours').val());
var _occurrence = getObjectByValue(_occurrences, 'tourID', booking.tourID);
_occurrenceID = _occurrence.occurrenceID;

Or is it so _occurrence gets delete and does not sit around as its encapsulated and we assign a var that outside of the encapsulation. Does that actually work as a performance improvement? i.e.

Global var a = 1
{
    b = someFunction()  // After execution because of encapsulation it poofs???
    for(var c in b)
    {
        a += c.somefunction()
    }
}

Another option is that its just bad code?

Or perhaps its meant as a logical separation of code to help the dev?

I was just wondering if someone could shed some light on this for me :)

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You are right to question those curly braces. They don't do anything at all. The code inside the braces executes just the same as it would if the braces weren't there. It's clearly a mistake to have them there like that.

As you mentioned, it looks like somebody could have thought that the curly braces would introduce a block scope, perhaps causing a variable to go out of scope after the braces are closed. But JavaScript doesn't have block scope for var variables! (It does have block scope for let, but only in the newer JavaScript engines that support let.)

Or maybe they just thought it would be a good way to document where the variables are used. It's not.

Adding to the humor here, the code appears to be missing the var for _occurrenceID entirely - so it's probably creating a global variable unintentionally!

The way you rewrote the code without the curly braces is indeed how it will actually execute. It is a better representation of what the code actually does and is how the code should be written. (Fixing the missing var of course...)

over 4 years ago · Santiago Trujillo Report

0

Or perhaps its meant as a logical separation of code to help the dev?

I'm going to make 2 assumptions here:

  1. the developer was not incompetent
  2. you left out a lot of lines between the curlies

(if 1 is not true then all bets are off)

Do-nothing curly-bracket blocks do have a purpose - in various text editors they mark sections that can be collapsed and thus removed from sight. I often do this if I have 50+ lines that I know work but I have to constantly scroll past. Put curlies around the content (mind the nesting), click the "collapse/fold" icon in the gutter -> code disappears. My particular editor will remember folded blocks so I don't need to re-fold each time.

over 4 years ago · Santiago Trujillo Report

0

As far as I can tell the braces are only a hint to the developer who is reading the code.

My guess is the braces and nesting are only to illustrate that the list contains an item which contains an id. This might be intended to help the reader understand that if they edit any of that code, or move it, then also edit the inner code to match.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!