Introduction & Syntax
JavaScript Basics · Study material
JavaScript is the language of the web: every browser runs it, and with Node.js it also runs servers, command-line tools and APIs. It is usually the first language a front-end, full-stack or MERN developer is tested on – and its basics hide rules that interviewers love: automatic semicolon insertion, why typeof null is "object", what strict mode changes, and why a single syntax error stops a whole script. This topic covers how JavaScript runs and its core syntax. Then test yourself with 60 practice questions – 15 MCQs, 20 predict-the-output puzzles, 15 debugging questions and 10 coding problems – every one run on Node.js. Each example below has a Run button: try it in your browser, edit it and run it again.
What JavaScript is
| Term | Meaning |
|---|---|
| ECMAScript | the language specification, updated every year by the TC39 committee (ES6 = ES2015 was the big update) |
| JavaScript engine | the program that runs JavaScript: V8 (Chrome, Edge, Node.js), SpiderMonkey (Firefox), JavaScriptCore (Safari) |
| Runtime | engine + extra APIs: the browser adds document, window, fetch; Node.js adds fs, process, http |
| JIT compilation | modern engines parse the code, then compile hot parts to machine code while it runs |
| Single-threaded | one call stack runs your code; the event loop handles timers, network and events |
| Dynamically typed | variables have no declared type; values do |
A program needs no main() or class: statements run from the top of the file to the bottom.
// A JavaScript program is a list of statements, run from top to bottom
console.log("Hello, LearnFrenzy!"); // prints one line
console.log("JavaScript", 2026, true); // several values: separated by a space
const language = "JavaScript";
console.log(`I am learning ${language}`); // template literal: ${ } inserts a value
Hello, LearnFrenzy! JavaScript 2026 true I am learning JavaScript
Printing with console.log()
console.log(...values) prints its arguments separated by spaces and ends the line. Strings print without quotes on their own, but inside arrays and objects Node.js and the browser console show them in quotes. console.log itself returns undefined.
// console.log prints any value – Node and browser consoles show arrays and objects like this
console.log(42, "42"); // a number and a string look alike when printed on their own…
console.log([1, "two", true]); // …but inside an array the string keeps its quotes
console.log({ name: "Asha", marks: 91 });
console.log(console.log === console.log, typeof console.log);
42 42
[ 1, 'two', true ]
{ name: 'Asha', marks: 91 }
true functionStatements, semicolons and ASI
Statements end with a semicolon. If you leave one out, the parser applies Automatic Semicolon Insertion: at a line break where the next token cannot continue the statement, it inserts one. Two rules matter in interviews:
- After
return,break,continueandthrow, a line break always ends the statement – soreturnon its own line returnsundefined. - A line that starts with
(,[or`is joined to the previous line –let b = afollowed by[1, 2].forEach(…)becomesa[1, 2].forEach(…).
// Automatic Semicolon Insertion (ASI): a line break right after return ends the statement
function broken() {
return // ← ASI inserts a semicolon here
"result"; // never reached
}
function fixed() {
return "result"; // keep the value on the same line as return
}
console.log(broken());
console.log(fixed());
undefined result
Comments, identifiers and reserved words
// … comments to the end of the line; /* … */ spans several lines but cannot be nested – the first */ ends it. Identifiers may contain letters, digits, _ and $, must not start with a digit and are case-sensitive. Reserved words such as class, const, function, return, typeof and new cannot be names; undefined, NaN and Infinity are not reserved but are read-only globals. By convention variables and functions use camelCase, classes PascalCase and constants UPPER_SNAKE_CASE.
// Identifiers: letters, digits, _ and $ – not starting with a digit. Case matters.
const $price = 499;
const _discount = 50;
const finalPrice2 = $price - _discount;
const total = 1, Total = 2, TOTAL = 3; // three different variables
console.log(finalPrice2);
console.log(total + Total + TOTAL);
449 6
Template literals
Strings may use single quotes, double quotes or backticks. Only backticks (template literals) evaluate ${expression} and keep line breaks.
const name = "Rahul";
const marks = 87;
// Backticks: ${expression} is evaluated, and the string may span several lines
const report = `Student: ${name}
Marks: ${marks}/100
Result: ${marks >= 40 ? "Pass" : "Fail"}`;
console.log(report);
console.log('Single quotes do not interpolate: ${marks}');
Student: Rahul
Marks: 87/100
Result: Pass
Single quotes do not interpolate: ${marks}typeof at a glance
JavaScript has seven primitive types – string, number, bigint, boolean, undefined, symbol and null – and objects (arrays and functions are objects). typeof returns a string naming the type, with two famous exceptions: typeof null is "object" and typeof a function is "function". The Data Types topic covers them in depth.
// typeof returns the type of a value as a string
console.log(typeof 3.14, typeof "hi", typeof true);
console.log(typeof undefined, typeof 10n, typeof Symbol("id"));
console.log(typeof null); // "object" – a bug kept since 1995 for compatibility
console.log(typeof function () {}, typeof [], typeof {});
number string boolean undefined bigint symbol object function object object
Strict mode
"use strict"; at the top of a script or function turns silent mistakes into errors: assigning to an undeclared variable throws a ReferenceError, this is undefined in a plain function call, duplicate parameter names and the with statement are syntax errors. ES modules and class bodies are always strict, so modern code is strict by default.
// Modules (and this example) are always in strict mode: mistakes throw instead of failing silently
try {
undeclared = 10; // no let/const/var – a ReferenceError in strict mode
} catch (e) {
console.log("Caught:", e.name);
}
function whoAmI() {
return this; // a plain call in strict mode: this is undefined
}
console.log(whoAmI());
Caught: ReferenceError undefined
A script is parsed before it runs
The engine parses the whole script first. A SyntaxError anywhere – a missing bracket, a reserved word used as a name, a let declared twice in the same scope – stops the script before line 1 runs. Errors such as ReferenceError and TypeError happen at run time, only when the faulty line is reached.
Common mistakes
- Value on the line after return – the function returns
undefined. - Leaving out semicolons before ( or [ – the lines join and throw a TypeError.
- Wrong case –
console.Logis not a function;Priceandpriceare different names. - ${ } inside quotes – only backticks interpolate.
- "5" + 3 – + with a string joins text: "53", not 8.
- Using document in Node.js – the DOM belongs to the browser, not to the language.
Key points to remember
- JavaScript implements ECMAScript; V8 runs it in Chrome and Node.js.
- console.log prints its arguments separated by spaces and returns undefined.
- A line break after return always ends the statement.
- Identifiers are case-sensitive and may use $ and _.
- Only template literals (backticks) interpolate ${ }.
- typeof null is "object"; typeof a function is "function".
- Modules and classes are always strict; "use strict" makes scripts strict.
- Syntax errors stop the whole script before it starts.
Frequently asked questions
Is JavaScript compiled or interpreted?
Both. Engines parse the source, start running it with an interpreter and compile frequently used code to machine code just in time (JIT). There is no compile step you run yourself.
Is JavaScript the same as Java?
No. The names are a 1995 marketing decision. Java is a statically typed, class-based language compiled to bytecode; JavaScript is dynamically typed, prototype-based and runs in browsers and Node.js.
Should I write semicolons?
Most style guides do, and it avoids the ASI traps above. If a team leaves them out, a line must never start with ( [ or ` without a leading semicolon.
Where can I learn more?
Read What is JavaScript?, let, const and var and JavaScript Data Types in the JavaScript tutorial, then practise with the exercises below.