惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
M
MIT News - Artificial intelligence
S
SegmentFault 最新的问题
博客园 - 叶小钗
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
博客园_首页
U
Unit 42
月光博客
月光博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work Top 15 Reinforcement Learning Questions That Will Appear in Exams The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026
undefined vs undeclared, and how typeof behaves
Ashish Ghild · 2026-04-17 · via DEV Community

🧩 1. What is undefined?

let a;
console.log(a); // undefined

Enter fullscreen mode Exit fullscreen mode

✅ Meaning
A variable is declared in memory but not assigned a value

🔬 Behind the scenes

During creation phase:

a → undefined

Enter fullscreen mode Exit fullscreen mode

So:
Variable exists in memory
Value is default-initialized to undefined

❌ 2. What is undeclared?

console.log(b); // ReferenceError

Enter fullscreen mode Exit fullscreen mode

❌ Meaning
Variable was never declared at all

🔬 Behind the scenes

b → ❌ not present in memory

Enter fullscreen mode Exit fullscreen mode

👉 Engine cannot find it → throws ReferenceError

⚖️ Key Difference

⚙️ 3. What is typeof?

typeof is an operator that returns the type of a value as a string.

Examples

typeof 10        // "number"
typeof "hello"   // "string"
typeof true      // "boolean"
typeof undefined // "undefined"

Enter fullscreen mode Exit fullscreen mode

🔥 Special Case (Important)

typeof b // "undefined"

Enter fullscreen mode Exit fullscreen mode

👉 Even though b is undeclared!

Why does typeof not throw error?

Normally:

b // ❌ ReferenceError

Enter fullscreen mode Exit fullscreen mode

But:

typeof b // "undefined" ✅

Enter fullscreen mode Exit fullscreen mode

🧠 4. Why typeof undefined and typeof undeclared are same?
Because:

typeof undeclared → "undefined"
typeof undefined  → "undefined"

Enter fullscreen mode Exit fullscreen mode

👉 But internally they are NOT the same thing

🔬 Internal Reason
For declared variable:

let a;
typeof a // "undefined"

Enter fullscreen mode Exit fullscreen mode

👉 Engine:

Find a → value is undefined → return "undefined"

Enter fullscreen mode Exit fullscreen mode

For undeclared variable:
typeof b

👉 Engine does something special:

Check if variable exists
IF NOT → return "undefined" instead of throwing error

Enter fullscreen mode Exit fullscreen mode

🛡️ 5. Safety Guard Feature of typeof

This is intentional design in JavaScript.

🎯 Purpose

Allow safe checks for variables that may not exist

✅ Example

if (typeof someVar !== "undefined") {
  console.log("exists");
}

Enter fullscreen mode Exit fullscreen mode

👉 Safe even if someVar is never declared

❌ Without typeof

if (someVar !== undefined) {
  // ❌ ReferenceError
}

Enter fullscreen mode Exit fullscreen mode

🔬 Internal Behavior (Spec-Level Concept)
Normally variable access:

ResolveBinding(name)

If not found:

→ ReferenceError

Enter fullscreen mode Exit fullscreen mode

But typeof does:

If binding not found → return "undefined"

Enter fullscreen mode Exit fullscreen mode

👉 Special handling in spec

Real-World Use Case

Feature detection

if (typeof window !== "undefined") {
  // browser environment
}

Enter fullscreen mode Exit fullscreen mode

👉 Used in frameworks like Next.js

⚠️ Important Edge Case
typeof null // "object" ❌

👉 Historical bug in JavaScript

🧠 Mental Model

Think like this:

Normal access → strict (error if not found)
typeof → safe (never throws for variables)

Enter fullscreen mode Exit fullscreen mode

🎯 Final Takeaways
undefined → declared but no value
undeclared → not defined at all
typeof:
returns type as string
never throws error for undeclared variables
acts as a safety guard

🔥 Interview-Level Answer

typeof is a safe operator in JavaScript that returns the type of a value and uniquely does not throw a ReferenceError when used on undeclared variables, instead returning "undefined".