
How to display JavaScript variable value in HTML
Are you looking for ways to display JavaScript variable values in HTML? If so, then you've come to the right place. In this article, I'll explore three different methods of displaying a JavaScript variable value in an HTML page.
- Using document.write() method.
- Using innerHTML property.
- Using window.alert() method.
Display JavaScript variable using document.write() method.#
The first method is using the write() method of the document object. This method allows us to write any string or expression directly onto a webpage.
It should be noted that if you use this approach too much, it can slow down your website as all of those strings are being written directly into each visitor's browser window when they load up the page.
Display JavaScript variable using innerHTML property.#
The second way of displaying a JavaScript variable value is by using innerHTML property on an element like so:
This will replace whatever content was previously inside our div element with "Hello world!".
Display JavaScript Variable using window.alert() method.#
Finally there's also another option available where we can use window alert() function.
Doing so would bring up an alert box containing text "hello world!".
We hope now after reading these examples, you have better understanding about how one can display javascript variables values inside html pages. If there are still questions left unanswered than feel free leave comment section below !