In this JavaScript example code, a basic JavaScript program is demonstrated to print Hello world as an output. The primary purpose of this JavaScript program is to explain to beginners the various ways to get output in JavaScript.



Printing Hello World

Since JavaScript is a client-side scripting language, it provides various ways to print sentences on the screen. These are:

  • document.write()
  • console.log()
  • alert()

All of the above methods do the same thing: displaying the output, but they all have a different way of showing it.

  • document.write() is used to print strings on the web browser.
  • console.log() is used mainly during debugging.
  • alert() is used to display text in the dialog box that pops up on the screen.

JavaScript document.write()

The document.write() is a JavaScript method used to write text as an output on a web browser screen.

Example:

document.write('Hello World');

Program Output:

 Hello World

JavaScript console.log()

A web console is available in all modern web browsers, and its primary purpose is debugging. console.log() is a method in JavaScript that writes messages to the console.

Example:

console.log('Hello World');

Program Output:

JavaScript alert()

The alert() is a method that displays a text message in a dialog box, and that message pops up with the OK button on the screen.

Example:

alert('Hello World');

Program Output:



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram