Skip to content
Related Articles

Related Articles

Improve Article

JavaScript Course | Printing Hello World in JavaScript

  • Last Updated : 11 Aug, 2021

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: 

HTML




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

Javascript




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

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

Output: 

Example: 

Javascript




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

Output: 

Hello World

Example: 

Javascript




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

Output: 

Supported Browser:

  • Google Chrome
  • Microsoft Edge
  • Firefox
  • Opera
  • Safari

Next article: JavaScript Course | Understanding Code Structure in JavaScript
 




My Personal Notes arrow_drop_up