Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
Variable changes type when added to an array

I'm parsing a string to a float, and when I print the type of the variable, I get number. However, if I add the variable to an array, and print it from there, I get string. But if I then index clicks[0], I once again get number

let clicks = []
let latitude = parseFloat(stringCoords.substring(11, 28))
clicks.push(latitude) 
console.log(typeof(latitude)) -- prints number
for (var object in clicks) {
  console.log(typeof(object)) -- prints string
}
console.log(typeof(clicks[0])) -- prints number

The only thing I'm adding to the clicks array is the latitude variable. I'm confused as to why it's changing types. Please let me know.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The for..in loops through the properties of an object which has the type string. And for..of loops through the values of an iterable object, which has the type of whatever you store in it (In your case, a number).

let clicks = []
let latitude = parseFloat("10.33")
clicks.push(latitude)
console.log(typeof(latitude)) 
for (var object of clicks) {
  console.log(typeof(object)) 
}
console.log(typeof(clicks[0])) 

about 4 years ago · Juan Pablo Isaza Relatório

0

as 'for(... in ...)' you are navigating through an array, 'object' actually produces the index value of that element, not the element of the current array. When you navigate with the 'clicks.forEach(...)' structure, you access the elements of the array. Therefore, you get a "string", so the operation you do with "typeof(...)" is not on the same elements. One of them gives the array index and the other gives the element of the array.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda