Someone can explain to me the following situation:

Asked

Viewed 91 times

2

pessoa = {
    nome: 'leandro',
    idade: '2'
}
for(x=0;x<pessoa.idade;x++){
   console.log('Ola mundo');     // ele repete 2 vezes "Ola mundo" na tela
}

The question I have is that I thought that Javascript differentiated string from numbers, but it seems that I was wrong because when I put string in the object it normally accepts.

3 answers

2


As @Andrew replied, the Javascript makes implicit conversions to resolve certain situations. Where it is possible to perform operations such as String, he operates as such, otherwise he converts.

See below some practical examples:

var a = "5";
var b = 2;
var c = 5;

// confirmando os tipos:
console.log('Tipo de a: ' + typeof a);
console.log('Tipo de b: ' + typeof b);
console.log('Tipo de c: ' + typeof c);

// é possível operar como string, concatena:
console.log(a+b)
// não é possível operar como string, divide:
console.log(a/b)
// é possível operar como string, compara os dois como string:
console.log(a == c);
// não é possível operar como string, converte para número:
console.log(a < b);

  • vlw mano I’m starting a couple of weeks still am adapting to the JS

1

The Javascript differs string of numbers yes. But he’s doing an automatic job that you’re missing.

The moment you do:

x < pessoa.idade

Javascript knows the operator of < makes more sense comparing numbers, so behind the scenes he’s doing it for you:

x < parseInt(pessoa.idade)

Welcome to JS, one of the world’s most flexible languages.

I hope you understand.

Hugs.

  • vlw mano for having helped I’m still a little lost to that two weeks ago q’m training.hugs

1

In terms of comparison, Javascript always sees a numeric string as a number. In your case, the number 2 is seen as a value to be compared, no matter if it is a string or not.

Except in cases of mathematical operations, Javascript will consider the type in case of sum, because of the sign + which is also used in concatenation. Example:

pessoa = {
    nome: 'leandro',
    idade: '2'
}
console.log(pessoa.idade+1); // imprime 21

To sum the value would need to convert the string to number using, for example, parseInt():

pessoa = {
    nome: 'leandro',
    idade: '2'
}

console.log(parseInt(pessoa.idade)+1); // imprime 3

In the case of subtractions, divisions and multiplications, the value shall be considered an absolute number:

pessoa = {
   nome: 'leandro',
   idade: '2.    '
}

console.log(pessoa.idade-1); // subtração: imprime 1
console.log(pessoa.idade/2); // divisão: imprime 1
console.log(pessoa.idade*3); // multiplicação: imprime 6

See what the value in idade i put a dot and several spaces. Javascript will convert the string value to absolute value. Since point is the decimal separator and spaces are ignored, the absolute value of the string will be only 2.

  • 1

    vlw mano one day I get there more for now it’s just study. Strong hug tamojunto

Browser other questions tagged

You are not signed in. Login or sign up in order to post.