By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
Responding to and Tracking Key Presses
How to Work With Responding to and Tracking Key Presses
JavaScript
Passing a JavaScript Value Between HTML Pages
Passing a JavaScript Value Between HTML Pages
JavaScript
Compare Objects in an Array
JavaScript Problem: Compare Objects in an Array
JavaScript
Switching Name Order Using Capturing Groups in Regular Expressions
Switching Name Order Using Capturing Groups in Regular Expressions
JavaScript
Shuffling an Array
JavaScript Problem: How to Perform Shuffling an Array
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript Program > How to Print Hello World Using JavaScript with Three Different Ways
JavaScript Program

How to Print Hello World Using JavaScript with Three Different Ways

Rocoderes
Last updated: 2022/08/27 at 3:26 PM
Rocoderes
Share
3 Min Read
How to Print Hello World Using JavaScript with Three Diffrent Ways

In this article, we will make a program to print hello world using JavaScript. As we know that, in every language, printing “hello world” is the first program. As a beginner of JavaScript, This is going to be our first ever program, or you can say this will be our first step into JavaScript.

Contents
Using console.log()Using Alert() Using document.write()

We will complete this program with using three ways:

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

These all are also known as JavaScript methods, for simplicity you can call them functions as well. These methods are not the same at anyhow, but we will use these, so you can identify the differences between them.

Using console.log()

Firstly, let me tell you that, you can’t use JavaScript methods directly you need HTML file as a base with which you can do this program perfectly. So just make a little program of HTML in which we will add the JavaScript code within the <script> tag.

console.log() is used in debugging the code. In most of the JavaScript programs, we can add console.log() to debug the code to check out flow of the program.

Syntax:

console.log("your message")
<html>
    <body>
        <script>
            console.log("Hello World!!!")
        </script>
    </body>
</html>

Here, we have added a basic HTML code, in this we just added script tag to write JavaScript code. There are other ways as well to add JavaScript to HTML, of course. In console.log, we can add our message within the quotation(“ “).

You can see the output in the browser, by going into inspect element>console.

How to Print Hello World Using JavaScript with Three Different Ways

Using Alert()

alert() method is used to show the alert message in the alert box in the current window.

Syntax:

alert("your message")

In here, we just replaced the console.log with alert, using alert() method, we can see the differences in outputs. In alert(), we can see that alert box show up as soon as we run the program here.

<html>
    <body>
        <script>
            alert("Hello World!!!")
        </script>
    </body>
</html>
How to Print Hello World Using JavaScript with Three Different Ways

Using document.write()

Okay, we have seen two methods to print hello world, but if you have noticed the output of above methods, we didn’t print the message on the main window.

document.write() is used when you want to print the content to the HTML document, simply on the main tab.

Syntax:

document.write("Hello World!!!")

In here, document is the variable of the DOM, or HTML document, and it targets the HTML document. It is actually a pre-defined variable in JavaScript.

write() is the method in which we have to write our message. As its name suggests that it just print the things inside of it.

<html>
    <body>
        <script>
            document.write("Hello World!!!")
        </script>
    </body>
</html>

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

TAGGED: programs
Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Rocoderes
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article Create a Certificate Generator Website in JavaScript
Next Article How to Add Two Numbers, Swap Two Numbers And Find Square Root in JavaScript How to Add Two Numbers, Swap Two Numbers And Find Square Root in JavaScript
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

Computing Fibonacci Sequence in JavaScript

Computing Fibonacci Sequence in JavaScript

January 12, 2023
Swap Two Numbers Without A Third Variable

How to Swap Two Numbers Without A Third Variable in JS

December 12, 2022
Armstrong Number

How to Find an Armstrong Number Using JavaScript

September 23, 2022
Print The Table

How to Print The Table of Any User Defined Number Using Function

September 22, 2022
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?