The shortest JS program
An empty file still does a lot.
- Even an empty file creates the Global Execution Context.
- The
windowobject is created — the global object in browsers. thisis created and points towindowat the global level.- Memory space + execution thread are set up, ready for your code.
window & the global scope
Where var lives, where let/const don't.
var x = 10at global scope → attached towindow.window.x === 10.let/constat global scope → Script scope, NOT onwindow.- Web APIs (
console.log,setTimeout,alert,fetch) are properties ofwindow— they’re not part of JS itself. globalThis(ES2020) = universal global object:windowin browser,globalin Node.
this — the eight contexts
How a function is called decides this.
- Global →
this === window(browser) orglobal(Node). - Regular function standalone →
window(non-strict) orundefined(strict). - Object method → the owning object.
- Arrow function → no own this — inherited from the enclosing lexical scope.
- Constructor (new) → the new object. Event handler → the DOM element. call/apply/bind → whatever you pass.
undefined vs not defined
A value vs an error.
undefined— declared, no value yet. Memory allocated. It’s a type.typeof undefined === "undefined".not defined— never declared. No memory. Access →ReferenceError: x is not defined.- Three states: declared + assigned (value), declared only (
undefined), never declared (error). typeofis safe on undeclared variables — still returns"undefined", no error.
undefined vs null
Engine default vs programmer intent.
undefined= JS hasn’t assigned a value.null= programmer chose no value.typeof null→"object"— a historical bug, preserved for back-compat.undefined == null→ true (loose).undefined === null→ false (types differ).- Never assign
undefinedmanually — usenullwhen you mean “empty.”
Falsy & truthy
The 8 falsy values — and the traps.
- Falsy:
false,0,-0,0n,"",null,undefined,NaN. - Truthy traps:
"0","false",[](empty array),{}(empty object) are all truthy. - Missing object property →
undefined(no error). Missing function arg →undefined. void 0→ a reliable way to getundefinedif something has shadowed it.
Comments
Comments are disabled in this environment. Set
PUBLIC_GISCUS_REPO,PUBLIC_GISCUS_REPO_ID, andPUBLIC_GISCUS_CATEGORY_IDto enable.