2
I really wanted to understand what it’s for and how to use the method toString
, why I always search on websites and can’t understand the way they are talking and demonstrating, so if anyone can explain to me what this method serves, as well as what ways to use it will help me a lot.
I have the code below, showing the command I learned by classes around, but I don’t understand how the toString
.
/* Objetivo: Desenvolver um programa em JavaScript que faça a leitura de 2 números Inteiros
aplique a conversão de valores com parseInt e apresente o resultado da soma entre eles.
Entrada de Dados por Formulário. */
/* Definição de Variáveis */
var js_n1;
var js_n2;
var js_soma;
/* Definição da função soma() para capturar dados, processar somatória e apresentar o resultado */
function soma() {
/* Entrada de Dados */
js_n1 = parseInt(document.MeuFormulario.f_n1.value);
js_n2 = parseInt(document.MeuFormulario.f_n2.value);
/* Processamento de Dados */
js_soma = js_n1 + js_n2;
/* Saída de Dados */
window.alert('Resultado da Somatória = ' + js_soma.toString());
}
<h3>Somatória de dois números inteiros</h3>
<!-- Definição da área e dos elementos de um Formulário -->
<form name="MeuFormulario" action="" method="POST">
Entre com o valor 1: <input type="text" name="f_n1"><p><p>
Entre com o valor 2: <input type="text" name="f_n2"><p><p>
<input type="button" name="btn_somar" value="Somar" onclick="soma()">
<input type="reset" name="btn_apagar" value="Apagar">
</form>
To which "comment" you refer?
– Luiz Felipe
@x7MKDeath7x if you want it to erase the reply, that’s not how the site works. Do the [tour]
– Rafael Tavares
Not quite so (// Number
2
is converted to string by the operator+
.). What happens is that if you add a string to a number, you convert the number into its string equivalent.– user60252
@Leocaracciolo, yes, is it wrong? Above the code of this comment, there is a more detailed explanation. Basically, the binary operator is responsible for the conversion
+
.– Luiz Felipe
I just wanted to help, what makes it clear in the sentence is that if you simply put the + sign before the number converts it into a string.
– user60252
Yes, I understand, thank you. : ) I removed that comment from the code, leaving only the explanation (more detailed) above it.
– Luiz Felipe