How can I print a variable from within the Javascript script?

Asked

Viewed 791 times

2

The doubt is exactly how I can "print" a value from within my Javascript code so I can track if the variable has the correct value.

I’m learning Javascript in college and was writing a paper that takes the code down. However after some errors I solved tests one of the modules in order to verify the calculated value. But then came another problem that is not knowing how I can "print" this value in the code so I can view in my browser.

Impostoderenda

        <h1>Cálculo do Imposto de Renda</h1>

        <br>
        Contribuição previdenciaria: <input type="text" name="contribuicao" id="contribuicaoId">
        <br>
        Despesas medicas:<input type="text" name="despesas" id="despesasId">
        <br>
        Número de dependentes: <input type="text" name="dependentes" id="dependentesId">
        <br>        
        Enviar: <input type="submit" name="enviar" onclick="deducao()">


        <script language="JavaScript">
        function deducao(){
                var dedu;
                var contribuicao=parseInt(getElementById("contribuicaoId").value,10);
                var despesas=parseInt(getElementById("despesasId").value,10);
                var dependentes=parseInt(getElementById("dependentesId").value,10);

            dedu=contribuicao+despesas+dependentes*3050;
            document.write("Resposta:" +dedu);
    }

        </script>
    </body>
</html>
  • 2

    If it is only for debug purposes you can use console.log(Nameavailablehere) in the parts of the code where you want to track the value, then to view just press F12 with your website running and it will appear in the "console" section of the window that opens. Another alternative is to press again F12 and put a line Debugger; in your code to stop your program, there with F10 you can go running row by row viewing as the value of the variable you want to track changes in the window that F12 opened, in this case just put the mouse on top of the variable where it appears in the code.

2 answers

2


Use the function console.log() to display your value in the browser console. Press F12 in the browser, then click on the tab console and the value will be displayed in the browser console.

Example of use:

var valor = 10;
console.log('O valor é: ' + valor);

Example screenshot of the browser console

inserir a descrição da imagem aqui

Add to your code points that you need to analyze and keep up with your browser console.

I hope I’ve helped.

  • Hello, so I did as you said. However is giving an error of uncaght Ference. On the line: var contributor=parseint(getElementById("contributorId").value,10); Obs:I believe this line is correct.

  • 1

    You’re trying to recover an element by id but the function is called document.getElementById(). Try it this way: var contribuicao = parseInt(document.getElementById("contribuicaoId").value,10);

  • I found the bug. I was in Document.getElementById..... Where I for some reason omitted "Document." = S

1

Utilize console.log(), this will print on the browser console that can be accessed using F12 (look for the 'Console' tab')

Browser other questions tagged

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