Syntax & Basics 🧱
JavaScript basics are the building blocks for every app—from quick scripts to large frontends.
1) Variables & Declarations 🏷️
let→ block-scoped, reassignable (use for most variables).const→ block-scoped, cannot be reassigned (protects constants/refs).var→ function-scoped, hoisted; avoid unless maintaining legacy code.
let score = 0;
const API_URL = "https://api.example.com";
var legacyFlag = true;2) Data Types 🧪
- Primitives:
string,number,bigint,boolean,null,undefined,symbol. - Reference types:
object,array,function,Date, etc. - Use
typeofto inspect values.
typeof 42; // "number"
typeof null; // "object" (historical quirk)3) Operators ⚙️
- Arithmetic:
+,-,*,/,%,**. - Assignment:
=,+=,-=. - Comparison:
===(strict equality),!==,<,>. - Logical:
&&,||,??,!. - Optional chaining:
obj?.nested?.valueavoids crashes if missing.
4) Conditionals 🔀
if (score > 10) {
celebrate();
} else if (score > 5) {
cheer();
} else {
keepTrying();
}- Ternary:
const status = score > 10 ? "winner" : "player"; - Switch for multiple discrete values:
switch (command) {
case "start": start(); break;
case "stop": stop(); break;
default: help();
}5) Loops 🔁
forloop when you know the count.
for (let i = 0; i < 3; i++) {
console.log(i);
}while/do...whilefor condition-based loops.for...ofiterates over arrays/iterables;for...initerates object keys.
for (const user of users) {
greet(user);
}Key Takeaways ✅
- Prefer
const/let; reservevarfor legacy scopes. - Know the difference between primitives and references.
- Use strict equality (
===) to avoid surprise type coercion. - Loops and conditionals combine to control program flow.
Recap 🔄
Mastering declarations, data types, operators, branching, and looping gives you the language grammar you’ll reuse everywhere else in JavaScript.
Last updated on