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

207
Vistas
Why this is a valid syntax in javascript?
const arr = [(firstName = 'hello')]

Why the above code is valid code in javascript. I didn't understand why this is not throwing any error.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here's what that code does and any it's valid.

That code does this:

firstName = 'hello';
const arr = ['hello'];

...except 'hello' is only evaluated once. (In the case of a literal like 'hello' it doesn't matter that it's only evaluated once, but it would if 'hello' were instead a function call returning a string.)

It's valid because:

  • Using the result of an assignment operation is valid; the result of an assignment operation is the value being assigned. So the expression firstName = 'hello' results in the value 'hello'.

  • The grouping operator (()) can be used where any expression can be used; the result of the grouping operation is the result of the expression within it.

  • [x] creates an array with x in it.

  • const arr = /*...*/ creates a constant (a read-only variable) and assigns it the result of evaluating the initializer expression (the expression on the right-hand side of the =).

So all of the parts are valid and they're assembled in a valid way. The identifier arr is declared; the firstName = 'hello' assignment expression is evaluated, setting firstName and resulting in 'hello'; the grouping operation ('hello') is evaluated resulting in 'hello'; the array literal ['hello'] is evaluated creating an array; and the arr constant is initialized with the array reference.

One wrinkle is that it looks like firstName is an undeclared identifier (your code doesn't declare it anywhere, there's no let firstName or similar). If it's really undeclared, then this is one of the unfortunate quirks of JavaScript in loose mode: assigning to an undeclared identifier implicitly creates a global variable (I call it "The Horror of Implicit Globals"). This is one of many reasons to use strict mode ("use strict" at the top of the file, or use JavaScript native modules ["ESM"]; the body of a class definition is also always strict). In strict mode, assigning to an undeclared identifier is the error it always should have been.

about 4 years ago · Juan Pablo Isaza 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