JavaScript Basics
JavaScript = dynamic, interpreted, single-threaded language for web
What is JavaScript?
JavaScript is a high-level, interpreted programming language that:
- Runs in browsers (client-side)
- Runs on servers (Node.js)
- Is dynamically typed
- Supports multiple paradigms (OOP, functional, procedural)
Key Characteristics
Dynamic · Single-threaded · Event-driven
| Feature | Meaning |
|---|---|
| Dynamic typing | Types determined at runtime |
| Single-threaded | One call stack, one execution |
| Event-driven | Asynchronous via event loop |
| Prototype-based | Objects inherit from prototypes |
| First-class functions | Functions are values |
Data Types
Primitives + Objects
Primitives (7 types)
stringnumberbooleanundefinednullsymbol(ES6)bigint(ES2020)
Objects
- Arrays
- Functions
- Objects
- Dates
- RegExp
"JavaScript has 7 primitive types and objects, which are reference types."
Variable Declarations
var = old, let/const = modern
| Declaration | Scope | Hoisting | Reassignment |
|---|---|---|---|
var | Function | Yes | Yes |
let | Block | Temporal dead zone | Yes |
const | Block | Temporal dead zone | No |
"Use const by default, let when reassignment is needed, and avoid var."
Hoisting
Declarations move to top
varandfunctiondeclarations are hoistedletandconstare in temporal dead zone- Only declarations hoisted, not initializations
"Hoisting moves variable and function declarations to the top of their scope before execution."
Scope
Global · Function · Block
- Global scope: Accessible everywhere
- Function scope:
varscoped to function - Block scope:
letandconstscoped to{}
"Scope determines where variables are accessible, with block scope being preferred for let and const."
Functions
Function declarations vs expressions
// Declaration (hoisted)
function greet() {}
// Expression (not hoisted)
const greet = function () {};
// Arrow function (ES6)
const greet = () => {};
"Function declarations are hoisted, while expressions and arrow functions are not."
🧠 Ultra-Short Cheat Sheet
Dynamic typing
Single-threaded + event loop
7 primitives + objects
var/let/const
Hoisting
Scope (global/function/block)
Closures
this binding
Arrow functions