Tutorials / JavaScript Fundamentals / What is JavaScript and where does it run?

What is JavaScript and where does it run?

12 min read javascript
Course contents (16 lessons)
  1. Getting started
  2. What is JavaScript and where does it run?
  3. Variables with let and const
  4. Values and operators
  5. Data types: numbers, strings, booleans, null and undefined
  6. Operators: arithmetic, comparison and logical
  7. Strings and template literals
  8. Making decisions and repeating work
  9. Conditionals: if, else and switch
  10. Loops: for, while and for...of
  11. Functions
  12. Functions: declarations, parameters and return values
  13. Arrow functions and callbacks
  14. Arrays and objects
  15. Arrays: creating, reading and changing lists
  16. Array methods: map, filter, find and reduce
  17. Objects: properties, methods and copying
  18. Scope and the browser
  19. Scope and closures, gently
  20. The DOM: selecting and changing elements
  21. Events: reacting to clicks, input and forms
  22. Final project
  23. 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.

  1. Open any page in Chrome or Edge.
  2. Press F12, or right-click and choose Inspect.
  3. Click the Console tab.
  4. 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 .js file 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.log works. Console.Log does not. JavaScript treats upper and lower case as different letters.

Try it yourself

  1. Open the browser console and run console.log("My name is ...") with your own name.
  2. Use the console as a calculator and work out 365 * 24, the hours in a year.
  3. Create hello.js and run it with Node. Print three lines: your name, your city and the result of 10 / 4.
  4. On purpose, change console.log to Console.log and read the error message you get.
javascript
// 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