JavaScript is one of the most popular programming languages for web development. If you’re just starting with JavaScript, the first program you’ll likely create is the classic “Hello, World!” example. This simple program helps you understand how to output text using JavaScript.
Here’s how you can write and execute your first JavaScript code:
Writing “Hello, World!” in JavaScript
- Using the Console: Open your browser’s developer tools (usually accessible with
F12
orCtrl + Shift + I
) and go to the console tab. Type the following code:javascriptCopyEditconsole.log("Hello, World!");
Press Enter, and you’ll see “Hello, World!” displayed in the console. - Embedding in HTML: You can also write JavaScript directly in an HTML file. Create a file named
index.html
and add the following code:htmlCopyEdit<!DOCTYPE html> <html> <head> <title>Hello World in JavaScript</title> </head> <body> <h1>Hello World Example</h1> <script> document.write("Hello, World!"); </script> </body> </html>
Open this file in a browser, and “Hello, World!” will appear on the webpage.
Understanding the Code
console.log
: This method is used to print messages to the browser’s console. It’s handy for debugging and testing.document.write
: This function writes directly to the webpage. However, it’s less commonly used in modern web development.
Start experimenting with these basics, and you’ll be ready to dive deeper into JavaScript programming. Happy coding! 😊