Hello World - potatoscript/javascript GitHub Wiki
The "Hello World" example is the traditional first program every programmer writes. It's a simple program that displays a message, typically to the screen. In JavaScript, this can be done using the alert() function or by manipulating the content of the webpage.
Here’s how you can create your own Hello World program in JavaScript!
Before we start with the JavaScript, let’s set up an HTML page just like we did earlier. Open your code editor and create a new file named index.html (or use the one from the previous section).
Here’s the basic HTML structure that we’ll be working with:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World Example</title>
</head>
<body>
<h1>Hello World in JavaScript!</h1>
<button onclick="showMessage()">Click Me!</button>
<script>
function showMessage() {
alert("Hello, World!");
}
</script>
</body>
</html>-
<html>&<head>: We start with the basic HTML structure to set up the page, including the meta tags and title. -
<h1>: This is the main heading of the page, displaying Hello World in JavaScript!. -
<button>: A button is created, and when clicked, it triggers theshowMessage()JavaScript function. -
<script>: The JavaScript code goes inside the<script>tag:-
function showMessage(): This is a JavaScript function that runs when the button is clicked. -
alert("Hello, World!");: This line of code creates an alert box that displays the message "Hello, World!" when the button is clicked.
-
-
Save your
index.htmlfile. - Open the file in your web browser by double-clicking it.
- Click the Click Me! button.
- An alert box should appear with the message "Hello, World!".
When you click the Click Me! button:
- The
onclick="showMessage()"tells the browser to run theshowMessage()function. - Inside the
showMessage()function, we have thealert()method, which shows a message box on the screen.
This is a very basic interaction in JavaScript, but it’s a great way to see how JavaScript can respond to user actions.
Now that you’ve created a simple Hello World example, let’s try modifying the code a little and experimenting with some changes.
-
Change the Message: Instead of
"Hello, World!", change the message inside thealert()to something like"Welcome to JavaScript!".alert("Welcome to JavaScript!");
-
Display the Message in the Webpage Instead: Instead of using an
alert(), let’s display the message directly on the webpage.<script> function showMessage() { document.body.innerHTML = "<h2>Welcome to JavaScript!</h2>"; } </script>
This will change the content of the webpage when the button is clicked, replacing everything with the message.
-
Create Multiple Buttons: Add multiple buttons to show different messages when clicked:
<button onclick="showMessage('Hello!')">Say Hello</button> <button onclick="showMessage('Goodbye!')">Say Goodbye</button>
And modify the
showMessage()function to display different messages:function showMessage(message) { document.body.innerHTML = "<h2>" + message + "</h2>"; }
-
alert(): A built-in JavaScript function that creates a pop-up message box with the text you provide. -
document.body.innerHTML: This JavaScript method allows you to change the HTML content of the page dynamically. In this case, it updates the page with a new message when the button is clicked. -
Function Argument (
message): By passing an argument to the function, we make it reusable. The message displayed will change depending on which button is clicked.
By completing the Hello World example, you’ve learned some fundamental concepts in JavaScript:
-
Functions: Functions allow us to bundle code that we can reuse. Here, we used the
showMessage()function to display a message when a button is clicked. -
Events: We used the
onclickevent to trigger the JavaScript function when the user clicks the button. - Interactivity: JavaScript can manipulate the content of a webpage and respond to user actions, like clicking a button.
- JavaScript Functions: A way to organize and reuse code.
- Events: JavaScript can respond to user actions, like button clicks.
- Dynamic Content: JavaScript allows us to change the webpage’s content in real-time.