What is JavaScript, and setup
JavaScript is the programming language that makes a web page interactive, and with Node.js the same language also runs on your computer and on servers. This note covers what JavaScript is, running it in the browser and in Node, and the basic setup (VS Code, Node LTS, npm).
1.1 What is JavaScript?
- Think of a website as a house: HTML is the structure (walls, rooms), CSS is the look (paint, decoration).
- JavaScript is the electricity: it makes things actually work, e.g. the cart updating on a button click or a form getting submitted.
- It is a programming language that makes a web page interactive, and almost every website uses it.
- Every browser already has JavaScript built in, so you need no installation to start.
- Step 1
1.HTML: structure
- Step 2
2.CSS: look and style
- Step 3
3.JavaScript: behaviour, interactivity
JavaScript = the behaviour of a web page
1.2 First code in the browser console
- In Chrome right click on any page, choose Inspect (or press F12) and open the Console tab.
- console.log prints a message; 2 + 3 gives 5; "Price: " + 499 * 2 multiplies first and then joins the text.
console.log("Hello, JavaScript!");console.log(2 + 3);console.log("Price: " + 499 * 2);> Hello, JavaScript!> 5> Price: 9981.3 Browser vs Node.js
- Every browser has a JavaScript engine inside it, like V8 in Chrome, which reads your code and runs it.
- Node.js takes the same V8 engine out of the browser and puts it on your computer.
- In Node you can read files and build servers.
- Node has no web page, so browser things like document and window are not available there.
- Step 1
1.You write JavaScript code
- Step 2
2.Browser: V8 engine + web page (document, window)
- Step 3
3.Node.js: V8 outside the browser (files, servers)
- Step 4
4.Not in Node: document, window
Same language, only the environment changes
| Point | Browser | Node.js |
|---|---|---|
| Engine | V8 (Chrome) and others | V8 |
| Runs where | Inside a web page | On your computer / a server |
| Can do | DOM, clicks, forms (document, window) | Read files, build servers |
| Not available | Direct file system access | document, window |
| How to run | DevTools Console | node first.js |
1.4 Setup: VS Code and Node.js
- VS Code: a free editor for writing code.
- Node.js: download from nodejs.org and choose the LTS version.
- LTS = Long Term Support, the stable version that companies use.
- Check the setup in a terminal: if both commands show a version number, you are ready.
node -vnpm -v1.5 Running a file with Node
- Open a folder in VS Code and create first.js with the same three lines as in the console.
- In the VS Code terminal run node first.js: you hand the file to Node to run.
- The output is the same as in the browser, because the language is the same; only the environment changed.
console.log("Hello, JavaScript!");console.log(2 + 3);console.log("Price: " + 499 * 2);$ node first.jsHello, JavaScript!5Price: 9981.6 npm and package.json
- npm (Node Package Manager) comes free with Node.
- npm init -y creates package.json: the ID card of the project (name, version, scripts).
- A "start" script lets you run npm start instead of typing node first.js.
- Every React and Next.js project runs from this file, and libraries are installed with npm.
npm init -y{ "name": "my-first-app", "version": "1.0.0", "scripts": { "start": "node first.js" }}$ npm start> my-first-app@1.0.0 start> node first.jsHello, JavaScript!5Price: 998- Step 1
1.Create a project folder, open it in VS Code
- Step 2
2.Write first.js
- Step 3
3.npm init -y creates package.json
- Step 4
4.Add "start": "node first.js" in scripts
- Step 5
5.npm start: Node runs first.js
1.7 Checkpoint: document in Node
- page.js reads the h1 of the page from document and prints its text.
- Run it in Node and you get an error: Node has no web page, so document is not defined.
- The same code works in the browser with an HTML page; always know which environment your code runs in.
const heading = document.querySelector("h1");console.log(heading.textContent);$ node page.jsReferenceError: document is not defined1.8 Common mistakes
- Typing just total in a file: the browser console shows the last value automatically, but Node prints nothing while running a file.
- Fix: pass the value to console.log, then the output is 998.
- Opening a single file instead of the whole project folder in VS Code.
- Running browser-only code (document, window) in Node.
// Wrongconst price = 499;const total = price * 2;total;// Rightconst price = 499;const total = price * 2;console.log(total);1.9 Misconceptions and setup rules
- Java and JavaScript are completely different languages; the similar name only came from marketing in the 1990s.
- JavaScript is not only for the browser: servers, mobile apps and desktop apps use it (VS Code itself is built with JavaScript and TypeScript).
- The official standard is ECMAScript, with a new version every year; this course teaches modern JavaScript.
- Rules: one folder per project, always Node LTS, the DevTools console is your best friend, install Prettier for auto formatting.
1.10 Interview tips
- "What is JavaScript?": a high-level, dynamically typed programming language that makes web pages interactive.
- It is single-threaded and runs on an engine like V8.
- It runs in the browser and, with Node.js, on the server too.
- Its standard is ECMAScript, updated every year.