This is useful when you are trying to debug your code or when you want to update the UI of your application.
Using the alert() function
alert() function. This functions entire purpose is to display a message to the user.
You can then fill in this function with the value you want to display.
This is the full example:
You can use
It will replace the entire contents of the page with the value you want to display.
If you run that, the entire page will just consist of
That means you can use it to display variables by passing it in:
const a = "Hello World!"; document.write(a);
Probably the best and most useful way to display a value is by taking advantage of the
This property will take whatever element you call it on and replace its entire contents with the value you want to display.
const a = "Hello World!"; document.getElementById("demo").innerHTML = a;
Now if you have HTML like this:
The contents of that
div will be replaced with
This is useful because you can control exactly where to insert the value and what it should look like.
You can either use
innerHTML, each of which have their own advantages and disadvantages.
Thanks for reading and we hope this has been helpful!
If you want to learn about web development, founding a start-up, bootstrapping a SaaS, and more, follow me on Twitter! You can also join the conversation over at our official Discord!
Leave us a message!