How do I use this JavaScript variable in HTML?

You don’t “use” JavaScript variables in HTML. HTML is not a programming language, it’s a markup language, it just “describes” what the page should look like.

If you want to display a variable on the screen, this is done with JavaScript.

First, you need somewhere for it to write to:

    <p id="output"></p>

Then you need to update your JavaScript code to write to that <p> tag. Make sure you do so after the page is ready.

window.onload = function(){
    var name = prompt("What's your name?");
    var lengthOfName = name.length

    document.getElementById('output').innerHTML = lengthOfName;

Show code snippet

Leave a Comment