Writing your first Hello, World! 🌍 JavaScript code Tutorial


Writing your first JavaScript "Hello World!" 🌍 program is simple as compared to other programming languages as you do not need any special software or IDE. To get started a simple text editor like Windows Notepad, or TextEdit if you work on a macOS is good enough! Also, to test your code your default browsers like Microsoft Edge or Safari will do! So let us see how our first JavaScript Hello World program looks like,

File: HelloWorld.html

<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript : Hello World! Example</title>
<script>alert("Hello World!");</script>
</head>
    
<body>
<p>This is my first JavaScript Program saying hello in a alert message!</p>
 </body>
</html>

Above what you see is a minimal HTML5 webpage template with a <script> tag in the head section, anything that you write inside it is the JavaScript code. alert() is a JavaScript method that when executed will display a prompt window as shown in the below image with the string message you pass in it.

JavaScript Hello World! Alert Message
JavaScript Hello World! Alert Message

How to run your first "Hello World!" JavaScript Code?

  1. Open a text editor: You should have Notepad if working on Windows or TextEdit if working on a Mac.
  2. Simply copy the above code content to the editor and save as "HelloWorld.html"
  3. Just double click the file and it should open up in your default web browser!
  4. If you see "Hello World!" alert pop-up! Your code worked! 😊, if not maybe something went wrong ❗️ Just recheck if you didn't do any of the above steps correctly.
  5. Click OK to close the alert box, see how you refresh your page the message is displayed again as each time you request the webpage JavaScript gets executed again.

Thats it! I hope this was helpful!



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap