Learn how to write your first JavaScript program with a step-by-step guide to creating a basic HTML file structure and embedding JavaScript.
Welcome to the exciting world of programming! In this section, we will guide you through writing your first JavaScript program: the classic “Hello, World!” This is a rite of passage for every programmer, marking the beginning of your journey into coding. By the end of this guide, you will have a solid understanding of how to create a basic HTML file, embed JavaScript, and display output using different methods.
Before we dive into writing code, let’s briefly discuss the components we’ll be working with:
To begin, we need to create a simple HTML file that will serve as the foundation for our JavaScript code. Follow these steps:
Open Your Text Editor: Use any text editor you prefer, such as Visual Studio Code, Sublime Text, or Notepad++.
Create a New File: Save this file with a .html
extension, for example, hello_world.html
.
Write the Basic HTML Structure:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello, World!</title>
</head>
<body>
<h1>Welcome to JavaScript Programming!</h1>
</body>
</html>
<!DOCTYPE html>
: Declares the document type and version of HTML.<html lang="en">
: The root element of the HTML document, with a language attribute set to English.<head>
: Contains metadata and links to stylesheets or scripts.<meta charset="UTF-8">
: Sets the character encoding for the document.<meta name="viewport" content="width=device-width, initial-scale=1.0">
: Ensures the webpage is responsive on different devices.<title>
: Sets the title of the webpage, displayed in the browser’s title bar.<body>
: Contains the content of the webpage.<script>
TagsNow that we have our HTML structure, let’s embed JavaScript into our file. We will use the <script>
tag, which tells the browser to execute the code within it.
Place the <script>
Tag:
You can place the <script>
tag either in the <head>
or at the end of the <body>
section. For this example, we’ll place it at the end of the <body>
to ensure the HTML content loads before the script runs.
<body>
<h1>Welcome to JavaScript Programming!</h1>
<script>
// This is where our JavaScript code will go
</script>
</body>
<script>
: The tag used to embed JavaScript code within an HTML document.//
): Used to annotate code, making it easier to understand. Comments are ignored by the browser.JavaScript provides several methods to display output. We’ll explore three common methods: console.log()
, alert()
, and document.write()
.
console.log()
The console.log()
method is used to print messages to the browser’s console. It’s a great tool for debugging and testing code.
<script>
// Displaying output in the console
console.log("Hello, World!");
</script>
Ctrl + Shift + I
(Windows/Linux) or Cmd + Option + I
(Mac).alert()
The alert()
method displays a pop-up dialog box with a message. It’s useful for grabbing the user’s attention.
<script>
// Displaying output using an alert box
alert("Hello, World!");
</script>
document.write()
The document.write()
method writes directly to the HTML document. It’s often used for testing but not recommended for production code as it can overwrite existing content.
<script>
// Displaying output directly on the webpage
document.write("Hello, World!");
</script>
document.write()
after the page has loaded can erase the entire page content.Let’s combine everything we’ve learned into a complete example. Below is the full HTML file with embedded JavaScript:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello, World!</title>
</head>
<body>
<h1>Welcome to JavaScript Programming!</h1>
<script>
// Displaying output in the console
console.log("Hello, World!");
// Displaying output using an alert box
alert("Hello, World!");
// Displaying output directly on the webpage
document.write("Hello, World!");
</script>
</body>
</html>
To help you understand the flow of how JavaScript interacts with HTML, let’s visualize the process using a simple flowchart.
flowchart TD A[Start] --> B[Create HTML File] B --> C[Add HTML Structure] C --> D[Embed JavaScript with <script>] D --> E{Choose Output Method} E --> |Console| F[Use console.log()] E --> |Alert| G[Use alert()] E --> |Document| H[Use document.write()] F --> I[View Output in Console] G --> J[View Alert Box] H --> K[View Output on Webpage] I --> L[End] J --> L K --> L
Experiment with the code examples provided. Here are some suggestions:
console.log()
, alert()
, document.write()
) to display different messages.For further reading and a deeper dive into JavaScript and HTML, consider exploring the following resources:
<script>
tag is used to embed JavaScript into HTML.console.log()
, alert()
, and document.write()
help display information.By following this guide, you’ve taken your first steps into the world of JavaScript programming. Keep experimenting and exploring to deepen your understanding and skills. Happy coding!