These are the notes for JavaScript episode 1. You will learn what JavaScript does on every website, why Node.js was invented, how to write and run your first program, and how to read the real errors Node.js prints. Every example below runs as-is in the SyntaxPilot compiler (language: JavaScript, which runs your code with Node.js).
The problem: a page that can't react
Picture a restaurant with only a printed menu card. You can read it, but you can't order from it. Early websites were like that: text and pictures, and clicking did nothing.
What you need is a waiter: someone who listens, takes your order and updates the bill right away. On a website, that waiter is JavaScript. You tap Like, the heart turns red and the count goes up, without the page reloading. That is JavaScript.
HTML, CSS and JavaScript: a house
| Building a house | Building a website | Job |
|---|---|---|
| Bricks and walls | HTML | the structure: headings, buttons, images |
| Paint and decor | CSS | the look: colours, fonts, layout |
| Wiring and switches | JavaScript | the behaviour: react to clicks, typing, live updates |
- A program is a list of instructions the computer follows one by one, in order (like a recipe).
- JavaScript is a language for writing those instructions. It was created in 1995, in about ten days, to make web pages react.
- Java and JavaScript are different languages. Only the name is similar ("car" and "carpet").
The next problem: JavaScript was stuck in the browser
For years, JavaScript ran only inside a web browser (Chrome, Firefox, Safari), because the browser contained the engine that understands JavaScript. Save some JavaScript in a file on your laptop (main.js, where .js is the extension for JavaScript files), double-click it, and it just opens as plain text. Nothing runs, because there is no engine outside the browser.
The fix: Node.js, the engine outside the car
Chrome's JavaScript engine is called V8. In 2009, Ryan Dahl took V8 out of the browser and wrapped it in a program called Node.js, the same way someone in a village might take a tractor's engine out and use it to run a flour mill.
| In a village | In JavaScript |
|---|---|
| A tractor's engine | V8: the engine inside Chrome |
| Take the engine out, put it in its own frame | Node.js: V8 outside the browser |
| Now it runs a flour mill | JavaScript now runs servers, tools and scripts in your terminal |
Node.js is a runtime: a program that runs your JavaScript for you. The SyntaxPilot compiler runs your code with node main.js.
| JavaScript in the browser | JavaScript in Node.js |
|---|---|
| Changes web pages, reacts to clicks | Runs in a terminal or on a server |
Has document, window |
Has files, networks, process |
console.log prints to the browser's DevTools |
console.log prints to the terminal |
We learn the language in Node.js because there is nothing to set up and you see output instantly. Everything about the language itself works the same in the browser.
Your first try fails (on purpose)
The goal: show Hello, World! on the screen. The obvious attempt is to just type the words:
Hello, World!
main.js:1
Hello, World!
^
SyntaxError: Unexpected token '!'
Simpler, just one word:
Hello
main.js:1
Hello
^
ReferenceError: Hello is not defined
Why? JavaScript reads plain words as code, not as text. Hello on its own means "find the thing named Hello". Nothing has that name, so: not defined.
We need two things:
- A way to say "this is text, keep it exactly as it is": quotes.
- An instruction that means "show this on the screen":
console.log.
console.log: the watchman's register
| At a society gate | In JavaScript |
|---|---|
| The visitor register | console: the terminal, where output goes |
| "Write this down": one new line per entry | .log(...): write one line |
| Exactly what to write, word for word | "Hello, World!": the text, in quotes |
console.log("Hello, World!");
Output:
Hello, World!
Anatomy of the line
| Piece | Meaning |
|---|---|
console |
the console: the terminal where output appears |
. |
"its": the console's log |
log |
a function: a ready-made machine that does one job (writes one line) |
( ) |
round brackets after a function's name call it (run it); what goes inside is the argument |
"Hello, World!" |
a string: text in quotes, printed exactly as written (without the quotes) |
; |
the semicolon ends the statement (one complete instruction) |
Text vs numbers: whiteboard vs calculator
Write "5 + 3" on a whiteboard and it stays as written. Type 5 + 3 on a calculator and you get 8.
console.log("5 + 3");
console.log(5 + 3);
5 + 3
8
Each console.log prints on its own line, and the code runs top to bottom, one statement after another.
Printing several values, and the + trap
console.log("Age:", 21);
console.log("Age: " + 21);
console.log("Room " + 5 + 3);
Age: 21
Age: 21
Room 53
- A comma gives
console.logseveral values; it prints them with a space between. - A plus between text and a number glues them: the number becomes text.
- JavaScript works left to right:
"Room " + 5is"Room 5", then+ 3glues again:"Room 53". No error, just a wrong answer.
Fix it with brackets, which go first:
console.log("Room " + (5 + 3));
Room 8
This automatic gluing is called type coercion; episode 3 covers it in detail.
Capital letters matter
JavaScript is case sensitive, like a phone password: chai123 and Chai123 are different.
Console.log("Hi");
ReferenceError: Console is not defined
console.Log("Hi");
TypeError: console.Log is not a function
The console exists, but it has no function called Log with a capital L.
Grammar mistakes
console.log("Hi);
main.js:1
console.log("Hi);
^^^^^
SyntaxError: Invalid or unexpected token
console.log("Hi";
main.js:1
console.log("Hi";
^^^^
SyntaxError: missing ) after argument list
Every error starts with the file name and line number (main.js:1), shows the line, and points at the problem with ^ marks. Read the error name (SyntaxError, ReferenceError, TypeError) and the message after it. Errors are a teacher's red mark: they tell you what and where.
When does an error stop you?
A mistake on line 2 of 3. Does line 1 still run? It depends on the kind of mistake.
A missing name (found while running):
console.log("Line 1");
console.log(Hello);
console.log("Line 3");
Line 1
main.js:2
console.log(Hello);
^
ReferenceError: Hello is not defined
Line 1 ran, line 2 crashed, line 3 never ran.
Broken grammar (found while reading):
console.log("Line 1");
console.log("Hi";
console.log("Line 3");
main.js:2
console.log("Hi";
^^^^
SyntaxError: missing ) after argument list
Nothing printed, not even line 1.
| Moment | What Node does | Errors found here | Effect |
|---|---|---|---|
| 1. Read | checks the grammar of the whole file | SyntaxError |
nothing runs |
| 2. Run | runs statements top to bottom | ReferenceError, TypeError, … |
lines before the crash already ran |
Escape sequences
A backslash changes the meaning of the next character inside a string.
console.log("Line 1\nLine 2");
console.log("Name:\tAarav");
console.log("He said \"Hi\"");
Line 1
Line 2
Name: Aarav
He said "Hi"
| Sequence | Meaning |
|---|---|
\n |
new line |
\t |
tab |
\" |
a double quote inside a double-quoted string |
\\ |
a backslash |
Comments and semicolons
// My first JavaScript program
console.log("Hello"); // greets the user
/* This line is switched off:
console.log("Not printed"); */
console.log("Bye!")
Hello
Bye!
//starts a comment that runs to the end of the line./* ... */can cover many lines; handy for switching code off without deleting it.- The last line has no semicolon and still works: JavaScript inserts missing semicolons in most places (automatic semicolon insertion). It doesn't always guess right, so this course always writes them.
Common mistakes (and the real error messages)
| You wrote | What Node.js says | Why | Fix |
|---|---|---|---|
Hello |
ReferenceError: Hello is not defined |
plain words are names, not text | console.log("Hello"); |
Hello, World! |
SyntaxError: Unexpected token '!' |
not valid code | put the text in quotes inside console.log |
Console.log("Hi"); |
ReferenceError: Console is not defined |
case sensitive | console.log |
console.Log("Hi"); |
TypeError: console.Log is not a function |
case sensitive | console.log |
console.log("Hi); |
SyntaxError: Invalid or unexpected token |
the string never closed | add the closing " |
console.log("Hi"; |
SyntaxError: missing ) after argument list |
bracket not closed | add ) |
"Room " + 5 + 3 |
(no error) Room 53 |
+ glues text left to right |
"Room " + (5 + 3) |
Mini project: a profile card
"=".repeat(30) makes 30 copies of = (a string's repeat function, like a photocopier).
// Mini project: my profile card
console.log("=".repeat(30));
console.log(" Name : Aarav");
console.log(" City : Pune");
console.log(" Goal : JavaScript developer");
console.log("=".repeat(30));
==============================
Name : Aarav
City : Pune
Goal : JavaScript developer
==============================
Practice
- Print your name, age and city on one line using commas.
- Print a 3-line address with one
console.logusing\n. - Predict, then run:
console.log("Total: " + 10 + 5); - Make three errors on purpose (a capital letter, a missing quote, a missing bracket) and write down each error name.
Solutions and hints
console.log("Aarav", 21, "Pune");printsAarav 21 Pune.console.log("12 MG Road\nPune\n411001");Total: 105. Left to right:"Total: " + 10is"Total: 10", then+ 5glues"5". For 15, write"Total: " + (10 + 5).Console.log→ReferenceError: Console is not defined; missing quote →SyntaxError: Invalid or unexpected token; missing bracket →SyntaxError: missing ) after argument list.
Quick quiz
What does this print?
console.log("2 + 3", 2 + 3);
- A.
2 + 3 2 + 3 - B.
5 5 - C.
2 + 3 5 - D. Error
Answer
C. The first value is in quotes (whiteboard), so it prints as written. The second has no quotes (calculator): 5. The comma adds a space.
Interview questions
- What is the difference between Java and JavaScript?
- What is Node.js? Is it a language? (No: it's a runtime built on the V8 engine.)
- Where does
console.logoutput go in the browser, and in Node.js? - What is the difference between a
SyntaxErrorand aReferenceError? Which one stops the whole file from running? - Are semicolons required in JavaScript? What is automatic semicolon insertion?
Glossary
| Term | Meaning | Example |
|---|---|---|
| Program | a list of instructions the computer runs in order | a recipe for the computer |
| JavaScript | the language that makes web pages react; also runs on servers via Node.js | console.log("Hi"); |
| HTML / CSS | the structure / the look of a web page | walls / paint |
| Engine (V8) | the part that understands and runs JavaScript | V8 inside Chrome |
| Node.js | a runtime: V8 outside the browser, so JS runs in a terminal or server | node main.js |
| Runtime | a program that runs code written in a language | Node.js runs JavaScript |
.js file |
a file containing JavaScript | main.js |
| Terminal / console | the text window where output appears | the right side of the compiler |
| Function | a ready-made machine that does one job | console.log |
| Call | running a function by writing () after its name |
console.log(...) |
| Argument | a value you pass into a function | "Hi" in console.log("Hi") |
| String | text in quotes | "Hello, World!" |
| Statement | one complete instruction, usually ending in ; |
console.log("Hi"); |
| Syntax | the grammar rules of a language | brackets must close |
| Token | one piece of code: a word, number or symbol | ! in Hello, World! |
| Case sensitive | capital and small letters are different | console ≠ Console |
| SyntaxError | broken grammar, found before anything runs | missing ) |
| ReferenceError | using a name that doesn't exist | Hello is not defined |
| TypeError | using a value in a way it can't be used | console.Log is not a function |
| Escape sequence | a backslash code inside a string | \n, \t, \" |
| Comment | a note for humans that JavaScript ignores | // note, /* note */ |
| Type coercion | JavaScript converting a type automatically | "Room " + 5 → "Room 5" |
Summary
- JavaScript is the wiring that makes websites react; Java is a different language.
- Node.js is Chrome's V8 engine outside the browser: a runtime that runs JavaScript anywhere.
console.log(...)writes one line of output. Quotes = text, no quotes = calculated.- Commas print values with spaces;
+glues text left to right ("Room " + 5 + 3→Room 53). - Read errors by name and line number. A
SyntaxErrorstops everything; other errors stop at the crash. \n,\t,\", comments//and/* */, and always end statements with;.
Next episode: variables and data types. A chai stall's price goes up, one bill line is missed, and you invent let, const and the types of data JavaScript knows.