Getting Started with JavaScript
🎯 Introduction
JavaScript is an essential language for front-end development. It brings static HTML pages to life, enables user interactions, and adds functionality. This article introduces JavaScript and its role in a web page, shows you how to print messages in the browser console, and walks you through your first “Hello, world” program. It is a starting point for anyone new to front-end development or curious about JavaScript.
🌟 What Is JavaScript?
JavaScript, often abbreviated to JS, is a scripting language widely used in web development. It was originally created to add dynamic effects and interactivity to web pages. Unlike HTML and CSS, JavaScript is a programming language: it can control page elements, respond to button clicks, validate data, and create animations.
HTML defines a page’s structure, CSS controls its appearance and layout, and JavaScript adds behavior. It can manipulate elements, handle user events, and implement complex logic and data interactions.
🧩 How JavaScript Relates to HTML and CSS
The three core technologies of front-end development have different roles:
- HTML (HyperText Markup Language) defines content and structure, including paragraphs, headings, and images.
- CSS (Cascading Style Sheets) controls appearance through colors, fonts, and layout.
- JavaScript adds behavior and interaction, such as opening a dialog when a button is clicked or loading data dynamically.
Think of a web page as a car:
- HTML is the frame, defining its shape and structure.
- CSS is the paint and decoration, defining how it looks.
- JavaScript is the engine, enabling it to move and perform actions.
All three work together to create the rich experiences found on modern websites.
💻 Ways to Run JavaScript in a Web Page
There are several ways to run JavaScript:
- Inline scripts: Write JavaScript directly inside a
<script>element in an HTML file.
<script>
alert('Hello, JS!');
</script>
- External script files: Write JavaScript in a separate
.jsfile and load it with<script src="app.js"></script>. This makes code easier to organize and maintain. - The browser console: Modern browsers include developer tools where you can enter and execute JavaScript interactively for debugging and testing.
As the browser loads a page, it parses the HTML and executes JavaScript in ordinary <script> elements when it encounters them.
🖥️ Printing to the Browser Console with console.log
The browser console is one of the most frequently used tools for debugging JavaScript. It displays messages that help you understand what your program is doing.
To open the console in Chrome:
- Windows / Linux: Press
Ctrl + Shift + J - Mac: Press
Cmd + Option + J
In the “Console” tab, enter JavaScript and press Enter to run it.
For example:
console.log('Hello, console!');
This prints the string “Hello, console!” to the console. console.log is a common debugging method for inspecting variable values and program messages when troubleshooting.
✍️ Writing “Hello, World”
“Hello, world” is a classic first program. It provides a simple way to check that your environment works and become familiar with basic JavaScript syntax.
Copy the following code into an HTML file, save it as hello.html, and open it in a browser:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hello, world</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
#message {
font-size: 24px;
color: #333;
}
</style>
</head>
<body>
<div id="message"></div>
<script>
// Write text into the message element on the page
var element = document.getElementById('message');
element.innerHTML = 'Hello, world!';
console.log('The page successfully displayed “Hello, world!”');
</script>
</body>
</html>
The page displays “Hello, world!” Open the console to see a message confirming that the script ran successfully.
❓ Knowledge Check
Q1: What is JavaScript?
JavaScript is a scripting language used to add interactivity and dynamic effects to web pages.
Q2: How does JavaScript differ from HTML and CSS?
HTML defines structure, CSS controls styling, and JavaScript handles behavior and logic.
Q3: How do you print information to the browser console?
Use console.log(content) to display the content in the browser’s console panel.
🧪 Exercises
Exercise 1: Change the Page Background Color
Use JavaScript to change the page’s background color to light blue.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Change the background color</title>
</head>
<body>
<script>
// Change the background color here
document.body.style.backgroundColor = '#a0c8f0';
</script>
</body>
</html>
Exercise 2: Show a Message When a Button Is Clicked
Create a button that displays an alert saying “You clicked the button!” when clicked.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Button alert</title>
</head>
<body>
<button id="btn">Click me</button>
<script>
var button = document.getElementById('btn');
button.onclick = function () {
alert('You clicked the button!');
};
</script>
</body>
</html>
🎉 You now know the JavaScript basics, how it works with HTML and CSS, how to print messages to the browser console, and how to write your first “Hello, world” program. Keep practicing as you continue learning front-end development!
