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

352
Vistas
Is there difference between Number() and Number().valueOf()

I've come across an example which tries to check if number is part of a particular range using following code:

class RangeValidationBase {
  constructor(private start: number, private end: number) {}
  
  protected RangeCheck(value: number): boolean {
    return value >= this.start && value <= this.end;
  }
  
  protected GetNumber(value: string): number {
    return new Number(value).valueOf();
  }
}

what is the use of using new Number().valueOf() instead of Number()? and are there any cases where using new Number().valueOf() is preferred over the other as they both seem same to my noobie brain?

almost 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

what is the use of using new Number().valueOf() instead of Number()?

None. Their behaviour is the same for all inputs, other than the technicality that Number or Number.prototype.valueOf could be replaced in a bad environment, which probably makes the new Number option harder to optimize for engines and therefore slightly less efficient in practice.

are there any cases where using new Number().valueOf() is preferred over the other

No, except in some misguided in-house style. There are good reasons to prefer a simple Number(value):

  • the aforementioned behavior technicality

  • It’s good practice to avoid primitive wrapper types like the one produced by new Number(), because they don’t behave close enough to primitives to be more useful than a plain object that contains a number property (for example), and are generally confusing. Even though the wrapper is immediately discarded in this case, it’s a red flag, picked up by both humans and linters.

almost 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