Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda