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

383
Vistas
Is it a bad practice to use methods like Number() String() in javascript or we shall use other ways as shown below and try to minimize their use?

  let num = 231.32;
  console.log( num.toPrecision() );
  console.log( String(num) );
  
  {

  let numStr = "250";
  console.log(Number(numStr)); console.log(Number.parseFloat(numStr));

}

  
Here we can achieve same things that String() and Number() can do by other ways. The above code gives same output. So shall we try to minimize the use case of String() and Number(). Which one would be nice ?

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

0

There is never really a real bad method just depends on the situation you are working on.

ToPrecision() will change the original number and round to specific decimal: Note that ToPrecision() could only deal with number and will produce error with deal with string.

The toPrecision() method returns a string representing the Number object to the specified precision

Number v.s ParseFloat is almost the same in most of the situation, but Number will return NaN and parseFloat will return the number part when deal with a string that contain some characters and number. (e.g 123ABC)

Example:

let num = '123ABC'
//return 123
console.log(parseFloat(num))
//return NaN
console.log(Number(num))

let num1='123'
 //Produce error
console.log(num1.toPrecision(2))

I think the main difference between String and use " " is that you can't always and it is very annoying to redeclare the things you want (e.g. redeclare ABC to "ABC"). (I think your numStr is to show use " " change string.

Another built-in function to use is toString() to convert to string, but toString will produce error when you use want to convert null and undefined.

Example:

let test = undefined;
//produce 'undefined'
console.log(String(test))
//produce error
console.log(test.toString())

I think it is OK to use any of them in the most situation, but you have to notice the small difference. They are not totally same.

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