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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// using console.log
console.log('Hello World');
</script>

chevron_right


Hit Ctrl+Shift+K to see the output in the browser console.
Output:

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// using document.write
document.write('Hello World');
</script>

chevron_right


Output:

Hello World

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// using alert
alert('Hello World');
</script>

chevron_right


Output:

Next article: JavaScript Course | Understanding Code Structure in JavaScript



My Personal Notes arrow_drop_up

Executive Software Developer at Cavisson Systems

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.