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.
How to run your first "Hello World!" JavaScript Code?
- Open a text editor: You should have Notepad if working on Windows or TextEdit if working on a Mac.
- Simply copy the above code content to the editor and save as "HelloWorld.html"
- Just double click the file and it should open up in your default web browser!
- 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.
- 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!
- Get Device Screen Width and Height using javaScript
- How to Print from JavaScript HTML using window.print
- Send Extra Data with Ajax Get or Post Request
- How to get UTC (GMT) time using JavaScript
- How to detect Browser and Operating System Name and Version using JavaScript
- Fix: Error: error:0308010C:digital envelope routines::unsupported NodeJs/Vue/React
- How to send email from JavaScript HTML using mailto
- Remove items from JavaScript array
- JavaScript : Get current page address
- Add Animated Scrolling to Html Page Title Script
- [javaScript] Convert text case to lowercase
- JavaScript date in yyyy-MM-dd format
- Power of Print Statements in JavaScript: A Comprehensive Guide
- Submit html form on dropdown menu value selection or change using javascript
- Write javaScript code in Swedish using FikaScript
- Javascript convert text case from uppercase to lowercase
- Excel Fix: SECURITY RISK Microsoft has blocked macros from running because the source of this file is untrusted.
- JavaScript: Count Words in a String
- Meaning of javascript:void(0) explained with example
- JavaScript: Generate Random Numbers between 1 and 3
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
- How to get current URL Location using Javascript HTML
- Fix: SyntaxError: The requested module does not provide an export named default
- Fix: Error: Cannot find module /node-examples/init
- JavaScript: Check if variable is a number
- How to install SpaCy (NLP Library) on Mac - Python
- fix fatal: --local can only be used inside a git repository error - Git
- Check Bluetooth is turned on or off on Android device programmatically [Java Code] - Android
- Center align text in TextView Android Programming - Android
- [Android] RuntimeException: Unable to start activity Need BLUETOOTH permission - Android
- Fix - Error:Invalid Gradle JDK configuration found (Android Studio) - Gradle
- SDK Manager: failed to install : Android Studio on Windows - Android-Studio
- The Date Command and its usage [Linux - Unix - macOS] - Linux