As fond as I am to come up with “Hello World” via a programming language, using JS to print that is exactly what we are going to learn here.
Using <script> Tag in JS
And following the cardinal rule about opening and closing things – what’s opened, shall always be closed; the closing tag for JS is:
Step 2: Open it and as learned from previous Chapters of HTML (Don’t tell me you forgot!), prepare the basic setup. It should look something like this:
Step 3: Now since JS requires <script> tag to be inserted, we can’t really do without them. So let’s do that. Remember you can use it either between <head> or <body> tags. I am doing this between the <body> tag:
Display Commands in JS
Anything that goes in between the <script> tags, system treats it as a JS element. So, if you wish to simply write “Hello World” in there, you can’t just write Hello World between the scripts and expect your browser to print it. Go ahead and try that. Nothing would happen. You will get a blank page as a result. In JS, the print command or data display can be done in 4 different ways:
We shall be focusing on the second method that is the document.write() command from above, herein.
Step 4: The document.write() command tells the browser to display whatever comes between its brackets. If there is a string you are trying to show make sure you put it between double inverted commas. If there are numbers entailed you can lose the quotes. Go ahead and complete the rest:
Step 5: Save the file in .html extension giving it a name you prefer. You were already briefed on this, don’t be so dumb C’mon! If you can’t remember, then go back to the HTML chapter 1 and check.
Once you are done, run it on your browser. This is what you should be getting:
Voila! There it is! Favourite two words!
Repeat the procedure, mess around experiment with document.write() to get different results. For e.g. try putting numbers in there. Say, add 5 and 8 together:
You should be getting the following result when you save the file and try opening it in the browser: