How To Display Text Using Javascript

For those getting into web development like Javascript, displaying or hiding text and elements is one of the most frequently. This tutorial will focus on how to display text using Javascript. 

How To Display Text Using Javascript

Method 1: Use innerHTML property

If you want to avoid unwanted auto-load scripts, let’s use the div and ID of innerHTML property:

        <div id="displayText">
        </div>
        <script>
            document.getElementById("displayText").innerHTML = "Use innerHTML property to display text";
        </script>

Output

Method 2: Use The document.write() method

This method doesn’t require you to use div or ID. Instead, it writes the message on a HTML Webpage:

<body>
        <script>
            document.write("Use The document.write() method to display text")
        </script>
</body>

Method 3: Use alert() box

There are various ways to employ the alert() box; however, here is a simple way to write a text:

<body>
        <script>
            alert("Use alert() box to display text")
        </script>
</body>

This function tends to take parameters of any type, including number, string, or boolean. Thus, there is no need to convert the value to a string type.

Conclusion

This tutorial has offered six methods to display text in Javascript. Choose one that suits your demands and circumstances.

Leave a Reply

Your email address will not be published. Required fields are marked *