JavaScript Basics: Your First Programs
What Is JavaScript?
In this lesson you will
- Explain what JavaScript is and what it is used for
- Print output with console.log
- Write comments and understand statements
Introduction
HTML gives a page its structure. CSS makes it look good. JavaScript makes it do things: open a menu, check a form, update a score, load new content without refreshing. It is the third pillar of every website, and today it also runs on servers and in apps.
The simple explanation
JavaScript is a programming language: a way to give a computer step-by-step instructions.
Unlike HTML, JavaScript can make decisions, repeat actions and calculate. Despite the similar name, it has nothing to do with the Java language. The names are a historical accident.
Quick quiz
What is JavaScript mainly used for on a web page?
Your first program
The simplest way to see a result is console.log. It prints a message to the console, a place programmers use to check what their code is doing.
Press Run. On this site, the output appears below the code. In a real browser you open the developer tools to see the console.
- The text in quotes is a string, and it is printed exactly.
2 + 3is calculated first, so5is printed.
Statements and comments
Each instruction is a statement. Statements usually end with a semicolon. The computer runs them from top to bottom, one at a time.
A comment is a note for humans. The computer ignores it.
Where does JavaScript run?
- In the browser, usually in a
scriptelement or an external.jsfile. - On a server or your computer, using tools such as Node.js.
HTML
<script src="app.js"></script>Every browser has a JavaScript engine built in, so you need to install nothing to start.
Errors are normal
Everyone makes mistakes when coding. When something is wrong, JavaScript tells you. Run this and read the message:
The error says Hello is not defined: without quotes, JavaScript thinks Hello is the name of something, and it has never heard of it. Reading error messages is a core skill.
Quick quiz
What does console.log do?
Try it yourself
Change the program so it prints your own name and a calculation of your choice, then add a comment on each line.
Summary
- JavaScript is the language that makes web pages interactive.
console.logprints a value so you can see it.- Statements run from top to bottom. Comments start with
//or sit between/*and*/. - Error messages are clues, not failures.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading