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

111
Vistas
From string to integer Javascript
function FinalAmount() {
  var FinalPrice = document.getElementById("FinalPrice");
  let AllProductTotalPrice = 0;

  for (let index = 0; index < OrderedProductList.length; ++index) {
    alert;
    var CurrentProductTotal = 0;
    CurrentProductTotal = OrderedProductList[index].TotalPrice;
    console.log(CurrentProductTotal);
    AllProductTotalPrice += CurrentProductTotal;
    console.log(AllProductTotalPrice);
  }
  FinalAllProductPrice = AllProductTotalPrice;
  FinalPrice.innerText = "RM" + FinalAllProductPrice;
}

This is my javascript code. I would have a question about why the console.log(CurrentProductTotal) is already an integer but the above AllProductTotalPrice is a string value. Is there any way to let its sum as an integer and bring it to the below FinalAllProductPrice. Please help me TT

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

0

I think the easiest way would be to

use Number() to parse a string to a number. (we don't define integers specifically in javascript)

const num = "1234"
Number(num)

⚠️ If you give it a string which does not solely consist out of numbers it will return NaN

So in your example that would be:

CurrentProductTotal = OrderedProductList[index].TotalPrice

Now to your questions:

  • console.log() automatically displays it formatted for you. I would use the typeof operator to check whether CurrentProductTotal is really a number, or a string which the console formats differently which is a bit confusing.

Try this in your browser console:


const num = "1234"
> undefined
num
> '1234'
console.log(num)
> 1234 // formatted like a number
console.log(num, typeof num)
> 1234 string // still formatted like a number, but a string in reality

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