JavaScript Course | Printing Hello World in JavaScript
Previous article: JavaScript Course | What is JavaScript ?
Whenever we write javascript we make use of the ‘script’ tag. We know what a normal HTML document is made up of. The ‘script’ tag is used to write javascript code and this ‘script’ tag is either placed inside the ‘head’ tag or inside the ‘body’ tag.
Example:
<!DOCTYPE HTML> <html> <head> <title></title> <!-- Script tag can also be placed here --></head> <body> <p>Before the script...</p> <!-- Script tag inside the body --> <script> // write the javascript code inside it </script> <p>...After the script.</p> </body> </html> |
We can put the script tag inside the ‘head’ or ‘body’ tag. Though it should be noted that each choice of putting the ‘script’ tag has its own consequences. For now, you can put the script tag anywhere you want.
Printing Hello World
In order to print the famous ‘Hello World’ sentence to the screen, we can make use of different methods that javascript provides. Most common are:
- console.log()
- document.write()
- alert()
Each of the above method have different ways of outputing the content. Though ‘document.write()’ is used when we want to print the content onto the document which is the HTML Document. Also ‘console.log()’ is mainly used when we are debugging javascript code and same thing with ‘alert()’.
Example:
<script> // using console.log console.log('Hello World'); </script> |
Hit Ctrl+Shift+K to see the output in the browser console.
Output:

Example:
<script> // using document.write document.write('Hello World'); </script> |
Output:
Hello World
Example:
<script> // using alert alert('Hello World'); </script> |
Output:

Next article: JavaScript Course | Understanding Code Structure in JavaScript
Recommended Posts:
- Javascript: A gateway to the world of web development
- HTML Course | First Web Page | Printing Hello World
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript Course | Objects in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Map in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



