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

173
Vistas
Should I use let or var when implementing a singleton?

I understand the difference between both (let locks the reference inside the block, whereas var declares a variable accessible scope-wide).

But considering the singleton pattern module-based:

var singleton = null;

module.exports = () => singleton ? singleton : singleton = newInstance();

Should I declare the singleton variable with let or var ? Is there any difference, considering CommonJS module imports/exports?

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

0

There is no difference between let or var, when we are talking about singleton implementation. I have already checked it in my IDE. Considering that using "let" is better practice, I suggest you to use exactly "let" keyword.

about 4 years ago · Juan Pablo Isaza Denunciar

0

In case to choose between 'var' or 'let' the best choice is 'let' because one of the differences it that 'let' removes the error-prone behavior with variable hoisting, you can read this for specifications: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let. But you should consider using only const as your default variables because the majority of bugs out there involve unexpected state changes, everything you can use to guarantee variable values and states should be used to avoid this kind of most commum problems.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You've already consider to use const instead?

I've always used const and never had a problem, because you'll never reassing the entire object.

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