What is JavaScript? Your first program
Learn what JavaScript is, where it runs, how to add it to a page, and write your first program with the console.
In this lesson you will
- Explain what JavaScript does on the web
- Add JavaScript to a page in three ways
- Use the browser console
- Write and run your first program
About 14 minutes
The language that makes pages move
Imagine a restaurant. HTML is the building and the menu: the walls, the tables, the words on the page. CSS is the decoration: colours, lighting, the layout of the tables. JavaScript is the waiter. It listens to what the customer wants, carries things back and forth, and makes things happen. When you click a button and a menu opens, when a form tells you your password is too short, when a map follows your finger, that is JavaScript at work.
JavaScript (often shortened to JS) is the programming language of the web. Every modern browser can run it, which means you need no installation to start. It is also one of the most used languages in the world, so the time you spend learning it pays back for years.
JavaScript and Java are two different languages. The names were a marketing accident in the 1990s. Think of it like “car” and “carpet”: they share letters and nothing else.
What JavaScript can do
- React to people: clicks, typing, scrolling, touch.
- Change the page: add, remove or edit text, images and styles without reloading.
- Check and calculate: validate forms, add up a shopping cart, convert units.
- Talk to servers: load new posts, send messages, fetch the weather.
- Build whole apps: games, editors, dashboards. Outside the browser, with Node.js, it also runs servers and tools.
Where JavaScript lives
There are three ways to add JavaScript to a web page. In practice you will use the third one almost all the time.
<!-- 1. Inline: inside an attribute (avoid) -->
<button onclick="alert('Hi')">Click</button>
<!-- 2. Internal: a script block -->
<script>
console.log('Hello from the page');
</script>
<!-- 3. External: a separate file (best) -->
<script src="app.js" defer></script>
Using an external file keeps your code tidy, lets several pages share it, and lets the browser cache it. The defer keyword tells the browser to wait until the page’s HTML is ready before running the script, which avoids many beginner errors.
Your first program
A program is a list of instructions the computer follows from top to bottom. Here is the most famous first program in the world.
console.log("Hello, world!");
Let us read it like a sentence. console is a built-in tool that represents the browser’s console, a hidden notepad for developers. .log() is an action that writes a message onto it. The text inside the brackets, wrapped in quotes, is the message. The semicolon ; marks the end of the instruction, like a full stop.
Press Run and read the output. Then change the message so it prints your own name.
console.log("Hello, world!");
console.log("I am learning JavaScript");
Change the text between the quotes. Each console.log writes one new line.
console.log("Hello, Shine!");
console.log("I am learning JavaScript");
The console: your best friend
Every browser has a console. Press F12 (or right-click and choose Inspect), then open the Console tab. You can type JavaScript there and see the answer instantly, which makes it a perfect playground. It is also where the browser shows you error messages in red, which are not scary: they are clues that tell you the line and the problem.
- Open any web page in Chrome, Edge or Firefox.
- Press F12 and click the Console tab.
- Type
2 + 2and press Enter. The console answers4. - Type
alert("Hi!")and press Enter. A pop-up appears. - Type
document.titleand press Enter. It shows the title of the page you are on.
Doing something visible
Printing to the console is invisible to visitors. To change what they see, JavaScript uses the page itself, called the DOM (Document Object Model). You will study it fully later, but here is a taste so you can see why this matters.
The result updates as you type. Edit the script so the heading says “JavaScript changed me!”.
<h2 id="title">Hello</h2>
<script>
document.getElementById("title").textContent = "Changed by JS";
</script>
Change the text between the quotes on the last line of the script.
<h2 id="title">Hello</h2>
<script>
document.getElementById("title").textContent = "JavaScript changed me!";
</script>
How the browser reads your code
The browser reads a JavaScript file from the top to the bottom, one instruction at a time. If it finds a mistake, it stops at that line and reports an error. Fix the line, reload, and carry on. This top-to-bottom order matters: you cannot use something before you have created it.
Comments: notes to yourself
// This is a single-line comment. The computer ignores it.
console.log("Visible"); // A comment can follow code
/*
This is a multi-line comment.
Good for longer explanations.
*/
Write comments to explain why you did something. Future you, who has forgotten everything in three months, will be grateful. You can also temporarily “comment out” a line to stop it running while you test.
Tools you need
- A browser with developer tools (Chrome, Edge or Firefox).
- A code editor. Visual Studio Code is free and the most popular. A plain notepad works for now.
- A folder for your practice files, with
index.htmlandapp.js.
Using curly “smart” quotes copied from a word processor, like “this”. JavaScript only understands straight quotes: " and '. If code you pasted mysteriously fails, check the quotes.
A red error such as Uncaught ReferenceError: x is not defined (app.js:3) tells you the kind of error, the problem and the file and line. Start at the line number it gives you.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What is JavaScript mainly used for on the web?
- Styling the colours of a page
- Making pages interactive
- Storing images
- Writing the page’s text
HTML structures, CSS styles, JavaScript adds behaviour.
Which tag loads an external JavaScript file?
<script src="app.js"><js file="app.js"><link src="app.js"><style src="app.js">
The script tag with a src attribute loads a file.
What does console.log("Hi") do?
- Shows a pop-up
- Writes Hi to the console
- Deletes Hi
- Makes Hi bold
It prints a message to the developer console.
How do you write a single-line comment?
// comment<!-- comment -->** comment **# comment
Two slashes start a single-line comment.
Are Java and JavaScript the same language?
- Yes
- No, they are different languages
- Java is the old name
- JavaScript is a version of Java
They only share part of a name.
Summary
- JavaScript makes pages interactive.
- Use an external file with defer for real projects.
- console.log prints to the developer console.
- Comments explain your code and are ignored by the computer.
Questions
Do I need to install anything?
No. Every browser runs JavaScript. A code editor like Visual Studio Code makes bigger projects easier.
Is JavaScript hard to learn?
The basics are friendly. Programs are made of small pieces you learn one at a time, and this course introduces them in order.
Do I need to know HTML and CSS first?
A little HTML helps because JavaScript changes pages. If you have finished the HTML course, you are ready.
