Master runtime state inspection: how to examine variable values, object state, and data structures during execution. Use when debugging, understanding variable contents, or when code behavior is unexpected. Triggers especially when user says "inspect variable", "check state", "what's in this object", "variable value", "runtime inspection" or "debug state".
Inspect-state is a behavioral specification skill that encapsulates the methodology for systematically examining runtime state to understand why code behaves as it does.
This skill embodies the discipline of developers who've added 50 console.logs instead of strategically inspecting state and know the difference debugging efficiency makes.
"Code behavior is determined by state. When behavior is unexpected, the state is wrong somewhere. Your job is to find WHERE the state became wrong, not just WHAT is wrong."
┌─────────────────────────────────────────────────────────────┐
│ STATE vs BEHAVIOR │
│ │
│ BEHAVIOR: What code DOES │
│ STATE: What code KNOWS │
│ │
│ When behavior is wrong... │
│ → State is wrong somewhere │
│ → Find WHERE state became wrong │
│ → Fix THAT │
│ │
│ Don't just patch behavior. │
│ Fix the state. │
└─────────────────────────────────────────────────────────────┘
| Question | Approach |
|---|---|
| What's in this variable? | Console.log or debugger |
| Where did this value come from? | Trace backwards |
| When did this change? | Watch/breakpoint |
| Why is this undefined? | Inspect caller, check conditions |
| What changed this object? | Break on modification |
┌─────────────────────────────────────────────────────────────┐
│ STATE INSPECTION POINTS │
│ │
│ 1. ENTRY: What did I receive? │
│ └─ function(x) → x is what? │
│ │
│ 2. TRANSFORM: What changed? │
│ └─ const y = x + 1 → y is x + 1? │
│ │
│ 3. BRANCH: What triggered this path? │
│ └─ if (condition) → What is condition? │
│ │
│ 4. EXIT: What am I returning? │
│ └─ return result → result is what? │
└─────────────────────────────────────────────────────────────┘
// Quick state dump
console.log('Variable:', variable);
console.log('Object:', JSON.stringify(obj, null, 2));
console.log('Type:', typeof variable);
console.log('Keys:', Object.keys(obj));
// Structured inspection
console.table([{ name: 'a', value: 1 }, { name: 'b', value: 2 }]);
// Grouped inspection
console.group('Function X');
console.log('Input:', input);
console.log('Processing...');
console.log('Output:', output);
console.groupEnd();
// Breakpoint at critical point
function process(input) {
debugger; // Execution stops here
// Then use browser DevTools:
// - Scope panel: see local variables
// - Watch: add specific expressions
// - Console: run expressions
}
// INSPECT STATE TRANSITION
function transform(data) {
console.log('BEFORE:', data); // What came in?
const result = doSomething(data);
console.log('AFTER:', result); // What came out?
// If before === after but expected different, doSomething didn't work
// If after === unexpected, find what changed in doSomething
return result;
}
function handleUser(user) {
// INSPECT THE BRANCH CONDITION
console.log('user.isAdmin:', user.isAdmin); // Is this what you think?
if (user.isAdmin) {
// INSPECT STATE IN BRANCH
console.log('Entered admin branch');
// What admin-specific state exists?
} else {
console.log('Entered regular branch');
}
}
// Inspect nested structure
console.log('user:', user);
console.log('user.profile:', user.profile);
console.log('user.profile.settings:', user.profile?.settings);
// Or use structured inspect
const inspect = (obj, path = '') => {
for (const key of Object.keys(obj)) {
console.log(`${path}${key}:`, obj[key]);
if (typeof obj[key] === 'object' && obj[key] !== null) {
inspect(obj[key], `${path}${key}.`);
}
}
};
inspect(user);
// Before modification
console.log('BEFORE user:', { ...user });
// Make change
user.name = 'New Name';
// After modification
console.log('AFTER user:', { ...user });
// Or use Proxy for automatic tracking
const tracked = new Proxy(original, {
set(target, key, value) {
console.log(`Setting ${String(key)} from ${target[key]} to ${value}`);
target[key] = value;
return true;
}
});
// Using DevTools:
// 1. Find the variable in Scope
// 2. Right-click → "Log when value changes" or "Break on value change"
// Or in code:
// For objects, use Proxy
const watchObject = (obj, name) => {
return new Proxy(obj, {
set(target, key, value) {
console.log(`[WATCH ${name}] ${String(key)} changed`);
target[key] = value;
return true;
}
});
};
const watchedUser = watchObject(user, 'user');
watchedUser.name = 'Changed'; // Logs: [WATCH user] name changed
// Issue: Variable is undefined
// Inspect:
// 1. Where should it come from?
// 2. Did that code run?
// 3. Did something reset it?
// Issue: Variable is null
// Inspect:
// 1. Explicitly set to null?
// 2. Failed API response?
// 3. Failed query?
// Debug
console.log('x is:', x, 'type:', typeof x);
// undefined = never assigned
// null = explicitly null
// Issue: State not updating
// Symptom: "I changed X but Y still has old value"
// Common causes:
// 1. Closure capturing old value
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100); // All log 3!
}
// 2. React state not syncing
// Need to check if setState actually ran
// 3. Cached value not invalidated
// console.log('Cache before:', cache.get('key'));
// Make change
// console.log('Cache after:', cache.get('key'));
// Issue: "I changed obj2 but obj1 changed too!"
// Cause: Both point to same object
const obj1 = { x: 1 };
const obj2 = obj1; // Same reference!
obj2.x = 2;
console.log(obj1.x); // 2 - obj1 was modified too!
// Inspect references
console.log('obj1 === obj2:', obj1 === obj2); // true = same reference
console.log('obj1:', obj1);
console.log('obj2:', obj2);
// STEP 1: Where should it come from?
function getUser(id) {
return db.users.find(id); // Should return user
}
// STEP 2: Inspect at the source
const user = getUser(id);
console.log('user from db:', user); // undefined??
// STEP 3: Check the input
console.log('id passed:', id); // Wrong id?
// STEP 4: Check the query
console.log('db.users:', db.users); // Empty?
// Continue until found...
// Symptom: user.name works but user.email doesn't
// Expected: { name: 'John', email: '[email protected]' }
// Actual: { name: 'John' }
// Inspect the object
console.log('user keys:', Object.keys(user));
console.log('user:', user);
// Found: email was never set
// Look at where user was created
const user = { name: 'John' }; // Missing email!
// STEP 1: Where does array come from?
const results = query.filter(item => item.active);
// STEP 2: Inspect before filter
console.log('Before filter:', query);
console.log('Query length:', query.length);
// STEP 3: Inspect filter condition
console.log('Item:', item, 'item.active:', item.active);
// STEP 4: Or use more specific filter
const results = query.filter(item => {
console.log('Checking:', item);
return item.active;
});
STATE INSPECTION CHECKLIST
===========================
□ 1. What should the state be?
□ 2. What is the actual state?
□ 3. Where should it come from?
□ 4. Did that code execute?
□ 5. Did the value change?
□ 6. Is there a reference issue?
□ 7. Is there stale state?
FOR OBJECTS:
□ Inspect keys: Object.keys(obj)
□ Inspect values: Object.values(obj)
□ Inspect entries: Object.entries(obj)
□ Check for undefined: obj.missingKey
FOR ARRAYS:
□ Check length: arr.length
□ Check contents: arr[0], arr[1]
□ Check for undefined: arr[1000]
FOR REFERENCES:
□ Check equality: obj1 === obj2
□ Clone to test: JSON.parse(JSON.stringify(obj))
// BAD: Where did this log come from?
console.log('Value:', value);
// GOOD: Clear source
console.log('[processUser] value:', value);
console.log('[getUserFromDB] value:', value);
console.log('[validateUser] value:', value);
// BAD: Messy output
console.log('user', user, 'order', order);
// GOOD: Structured output
console.log({
action: 'processOrder',
user,
order,
result,
});
// Only log in debug mode
const DEBUG = process.env.DEBUG === 'true';
function process(data) {
if (DEBUG) console.log('[process] input:', data);
const result = doWork(data);
if (DEBUG) console.log('[process] output:', result);
return result;
}
// BAD: Noise, not signal
console.log('1');
console.log('2');
console.log('a');
console.log('b');
// Now you can't find anything
// GOOD: Strategic logging
console.log('Before critical operation:', importantState);
// BAD: Checked wrong variable
console.log('userId:', userId); // Correct
// But bug is in user.email...
// GOOD: Check systematically
console.log('user:', user); // See everything
// BAD: Adding to production code
if (data.length > 0) {
// Now behavior changed
}
// GOOD: Use debugger
debugger; // Read without modifying
Never assume what state is. Inspect it.
Change one thing, inspect one thing.
State came from somewhere. Trace backwards to find the source.
Console.log for quick checks. Debugger for deep inspection.
State inspection is always comparing "what I expected" to "what I got".
debug-session — Uses state inspection during debuggingtrace-execution — Often inspect state at trace pointsunderstand-error — Errors guide what to inspectreproduce-bug — State inspection for isolationinspect-state — This IS this skill (self-referential)