What is JavaScript and where does it run?
Course contents (16 lessons)
- Getting started
- What is JavaScript and where does it run?
- Variables with let and const
- Values and operators
- Data types: numbers, strings, booleans, null and undefined
- Operators: arithmetic, comparison and logical
- Strings and template literals
- Making decisions and repeating work
- Conditionals: if, else and switch
- Loops: for, while and for...of
- Functions
- Functions: declarations, parameters and return values
- Arrow functions and callbacks
- Arrays and objects
- Arrays: creating, reading and changing lists
- Array methods: map, filter, find and reduce
- Objects: properties, methods and copying
- Scope and the browser
- Scope and closures, gently
- The DOM: selecting and changing elements
- Events: reacting to clicks, input and forms
- Final project
- Project: build a to-do list in the browser
What JavaScript is
JavaScript is a programming language. It started in 1995 as a way to make web pages react to the user. Today it also runs on servers, in command-line tools and inside desktop and mobile apps.
A web page is built from three layers. HTML describes the content. CSS describes how it looks. JavaScript describes how it behaves: what happens when you click a button, submit a form or scroll down.
One thing to clear up early. JavaScript and Java are different languages. They share a few letters in the name and some curly-brace syntax, but nothing more. If you search for help, add "JavaScript" to your query or you will land on Java answers.
Where JavaScript runs
JavaScript needs an engine to run. There are two places you will use one every day.
- The browser. Chrome, Edge, Firefox and Safari all contain a JavaScript engine. Code running here can read and change the web page, but it cannot read files on your computer.
- Node.js. Node takes the same engine that Chrome uses and lets you run JavaScript outside the browser. Code here can read files, start web servers and talk to databases, but there is no web page to change.
The language is the same in both places. What changes is the set of tools around it. The browser gives you document and window. Node gives you fs and http. We will use both in this course.
Run your first line in the browser
You do not need to install anything for this step.
- Open any page in Chrome or Edge.
- Press
F12, or right-click and choose Inspect. - Click the Console tab.
- Type the line below and press Enter.
console.log("Hello from the browser");
The console prints your message. console.log is the function you will use most while learning. It prints whatever you give it, so you can check what your code is doing.
You can also type a plain expression such as 2 + 3 and the console shows the result straight away. I use this all the time to test small ideas before I put them in a file.
Run JavaScript with Node.js
Install the LTS version of Node from nodejs.org. Then check it worked by opening a terminal and typing:
node --version
Create a file called hello.js with this content:
// hello.js
const name = "Ghulam";
console.log("Hello, " + name);
console.log("2 + 3 =", 2 + 3);
Save it and run it from the terminal:
node hello.js
You should see two lines of output. Here is what the code does. The first line starts with //, which makes it a comment. The engine ignores comments, and they are there for humans. The const line stores the text "Ghulam" under the name name. Then console.log prints a greeting, joining the two pieces of text with a +.
Adding JavaScript to a web page
For a real page, you put your code in a script file and link it from the HTML:
<!DOCTYPE html>
<html>
<body>
<h1>My page</h1>
<script src="app.js"></script>
</body>
</html>
The script tag sits at the end of body so the page content is loaded before your code runs. You will see why that matters in the DOM lesson. You can also write code between <script> and </script>, but a separate file is easier to maintain.
A good setup
Use Visual Studio Code. It is free, understands JavaScript well and has a built-in terminal. The Live Server extension reloads the page each time you save, which is handy.
Common mistakes
- Typing code in the wrong place. Code goes in a
.jsfile or the console, not in a Word document or in the visible text of an HTML page. - Smart quotes. If you copy code from a document, curly quotes can sneak in. JavaScript only accepts straight quotes:
"and'. - Ignoring the console. When something does not work, the browser console shows a red error message with the file and line number. Read it. It usually points straight at the problem.
- Case sensitivity.
console.logworks.Console.Logdoes not. JavaScript treats upper and lower case as different letters.
Try it yourself
- Open the browser console and run
console.log("My name is ...")with your own name. - Use the console as a calculator and work out
365 * 24, the hours in a year. - Create
hello.jsand run it with Node. Print three lines: your name, your city and the result of10 / 4. - On purpose, change
console.logtoConsole.logand read the error message you get.
// hello.js
const name = "Ghulam";
console.log("Hello, " + name);
console.log("2 + 3 =", 2 + 3);
This course is free to read. Create a free account to enrol, save your progress and pick up where you left off.
Sign up free I already have an account