Javascripting Tutorial | Javascript Hello World

Weighing in at 0 pounds (it’s free) in the lightweight category is Javascript, a scripting language. No, it’s not Java. Javascripting is entirely a different enclave. Don’t get confused there. Java is a complex programming language. JS or Javascript on the other hand, is a much simpler scripting language that your browser can tap into.

Languages like Java generally require you to download JRE, JDK etc. Au contraire, with Javascript you don’t have to download anything. It is there. Yes, it has always been there with you!

It is there running on your browser, your phone, everywhere. Now you know why it becomes a foremost choice when organizations are selecting a programming or scripting language for their work on the web.

All About Javascripting

Javascript helps you do all the cool stuff. That’s why it relates to me the most. Where’s that sunglasses emoji? Can’t find it. 😎 There we go!

stoned javascript dumb guy

So if you are wondering why do you need Javascript for, when you have a good thing going on with HTML, well hear me out. We use Javascript in HTML. Javascript literally completes HTML. The latter is incomplete without some Javascripting elements to fill it up. Look at it this way:

Imagine you are creating a robot. If you bring together all its body parts, then you have simply gone HTML on it. Now that you provide it with eyes to see, ears to hear, nose to smell, then you are talking Javascripting, meaning providing functionality to it. Well, eventually when you are done with the functionality bit, I don’t know, you throw in some clothes on it, well that’s where you get CSS. Clear? Moving on.

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.

So up until now we had learnt how to create a hello world page with notepad, now we will learn how to print hello world using Javascript. Let’s get cracking!

Using <script> Tag in JS

We must first establish how a javascript is written in HTML. Just like we used tags for HTML, here too there’s a tag that we need to remember:

<script>

And following the cardinal rule about opening and closing things – what’s opened, shall always be closed; the closing tag for JS is:

</script>

You put anything between this and it gets treated like a Javascripting language by your browser. So, with that knowledge, we can proceed further with the creation of an HTML file.

Steps to display Javascript Hello World

Step 1: Create a separate folder for JS on your system. Coz order is everything when you are learning new things. Here we will experiment with Javascript-related stuff. So, in that folder right click and create a new text document or notepad file.

snapshot of new text document

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:

basic setup of html

You can lose the Demogorgon and choose any title you want. That character of Stranger Things has become a thing of the past now.

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:

html with javascript script tags inserted

Display Commands in JS

Anything that goes in between the <script> tags, the 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:

  1. window.alert()
  2. document.write()
  3. innerHTML
  4. console.log()

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:

printing Javascript Hello World using document.write()

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:

Javascript Hello World image javascripting

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:

javascripting to display addition of numbers

You should be getting the following result when you save the file and try opening it in the browser:

result of adding 5 and 8 via javascript

Now that we know how to display Javascript Hello World, we will focus on learning other aspects of Javascript in the upcoming chapters. Till then, go party hard! You have learned something soldier. You have earned it.

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

1 Response

  1. January 18, 2017

    […] is advisable you follow the standard learning routine by finishing Chapter 1 of Javascript first before arriving here. Already done? Whoa, you are a tiger! Let’s claw in, shall […]

Leave a Reply