JavaScript is the programming language of the web. HTML gives a page its structure and CSS gives it style; JavaScript makes it respond: it reacts to clicks, checks forms, updates content without reloading and talks to servers. It also runs outside the browser through Node.js, so one language can power both the front end and the back end of a website.
In this lesson you will learn
- What JavaScript is and where it runs
- How to write your first program with
console.log() - How to add JavaScript to a web page with
<script> - Comments, statements and semicolons
- How to open the browser console
Your First JavaScript Program
// My first JavaScript program
console.log("Hello, JavaScript!");
console.log("2 + 3 =", 2 + 3);
const name = "Rahim";
console.log("Welcome, " + name + "!");
Output
Hello, JavaScript!
2 + 3 = 5
Welcome, Rahim!
console.log()prints values to the console. It is the tool you will use most for checking what your code is doing.- You can pass several values separated by commas, and they are printed with spaces between them.
const name = "Rahim";stores a value in a variable. The+operator joins strings together.- Lines that start with
//are comments. JavaScript ignores them; they are notes for people.
Where to Run JavaScript
| Place | How |
|---|---|
| Browser console | Press F12 (or Ctrl+Shift+J in Chrome), open the Console tab and type code directly. Quickest way to experiment. |
| Web page | Put code inside a <script> tag or in a separate .js file. |
| Node.js | Install Node.js, save code as app.js, then run node app.js in a terminal. The console examples on this site were run this way. |
JavaScript in a Web Page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First script</title>
</head>
<body>
<h3 id="msg">Click the button.</h3>
<button id="btn">Say hello</button>
<script>
const msg = document.getElementById("msg");
document.getElementById("btn").addEventListener("click", function () {
msg.textContent = "Hello from JavaScript! The time is " + new Date().toLocaleTimeString();
});
</script>
</body>
</html>
Result
- The
<script>element holds the JavaScript. It is placed at the end of<body>so the heading and button already exist when the code runs. document.getElementById("msg")finds an element on the page. You will learn this properly in the DOM lesson.addEventListener("click", ...)runs a function each time the button is clicked. Try it in the preview.
For real projects, keep JavaScript in its own file and link it:
<script src="app.js" defer></script>
The defer attribute lets the script load in the background and run only after the HTML has been read, so the tag can safely go in the <head>.
Statements, Semicolons and Case
- Each instruction is a statement. Ending statements with
;is optional in most cases, but using it consistently avoids rare surprises. - JavaScript is case-sensitive:
name,NameandNAMEare three different variables, andConsole.logis an error. - Comments come in two forms:
// one lineand/* several lines */.
JavaScript Is Not Java
Despite the similar names, JavaScript and Java are different languages with different designs and uses. The name was a marketing decision in 1995. The official standard behind JavaScript is called ECMAScript, and versions such as ES6 (2015) added much of the modern syntax used in these lessons.
Common Mistakes
- Putting the script in the head without
defer, so it runs before the elements it needs exist. - Wrong capitalisation, such as
console.LogorgetElementByID. - Mismatched quotes or brackets. Read the error message in the console; it gives the line number.
- Ignoring the console. When nothing happens, an error is almost always shown there.
Practice Exercises
Exercise 1. Print your name and your age on one line.
Show answer
console.log("Name:", "Sabbir", "Age:", 19);
Exercise 2. Print the number of minutes in a week, letting JavaScript do the multiplication.
Show answer
console.log(7 * 24 * 60); // 10080
Frequently Asked Questions
Do I need to install anything to learn JavaScript?
No. Every web browser already runs JavaScript, so you can start in the browser console. Install Node.js later if you want to run scripts from files.
Should I learn HTML and CSS first?
It helps for the browser parts, such as the DOM and events. The core language (variables, loops, functions) can be learned on its own. See the Web Development lessons for HTML and CSS.
Summary
- JavaScript adds behaviour to web pages and also runs on servers through Node.js.
console.log()prints values; the browser console shows output and errors.- Add scripts with
<script>, ideally from a separate file withdefer. - JavaScript is case-sensitive and is not related to Java.
