🍪 TechCookies
HomeDSASystem DesignJSMy Progress
Free
Log inStart free
TechCookies — Practice · Learn · PrepareTechCookies — Practice · Learn · Prepare
ConceptsPracticeSD challengesPricingPrivacyTermsContact
© 2026 TechCookies
JS›Variables & Types
📦Variables & TypesbeginnerFreeJS
varletconsttypeofprimitives
Learn
0/5 done~27 min
1
The three declaration keywords
Read
~5 min
🔒
The typeof gotchas every interviewer asks about
Exceptions
Complete previous first
🔒
Predict the output
Predict
Complete previous first
🔒
True or false?
T/F
Complete previous first
🔒
Fix the broken code
Fix
Complete previous first
1 / 5~5 min
The three declaration keywords

JavaScript has three ways to declare a variable, and choosing the wrong one is one of the most common interview mistakes.

var   → function-scoped, hoisted, re-declarable   (avoid)
let   → block-scoped, not hoisted*, reassignable   (default choice)
const → block-scoped, not hoisted*, not reassignable (prefer for values that don't change)

Block scope vs function scope — the most important distinction:

if (true) {
  var x = 1;   // leaks out of the block!
  let y = 2;   // stays inside
}
console.log(x);  // 1 — var ignores block boundaries
console.log(y);  // ReferenceError — let respects them

JavaScript's seven primitive types

JavaScript has exactly 7 primitive types. Memorise them — interviewers ask this directly:

TypeExampletypeof returns
string"hello""string"
number42, NaN, Inf"number"
bigint9007199254740991n"bigint"
booleantrue, false"boolean"
undefinedundefined"undefined"
symbolSymbol("id")"symbol"
nullnull"object" ← ⚠️

Everything else — arrays, functions, dates, regular expressions — is an object at runtime.

The const trap

const means the binding can't be reassigned — not that the value is immutable:

const nums = [1, 2, 3];
nums.push(4);       // ✓ fine — mutating the array, not reassigning the binding
nums = [1, 2, 3, 4]; // ✗ TypeError: Assignment to constant variable

const config = { debug: false };
config.debug = true; // ✓ fine — mutating the object
config = {};         // ✗ TypeError

For true immutability, use Object.freeze() — but note it's shallow.

Which to use when

  • const — default. Use it whenever you won't reassign.
  • let — when you need to reassign (loop counters, accumulator variables).
  • var — never in new code. Only in old codebases you're maintaining.

The next section covers the typeof operator — and its famous exceptions.

Read through to continue
Notes
🔍
Loading…
Sections
0/5 done~27 min
1
The three declaration keywords
Read
~5 min
🔒
The typeof gotchas every interviewer asks about
Exceptions
Complete previous first
🔒
Predict the output
Predict
Complete previous first
🔒
True or false?
T/F
Complete previous first
🔒
Fix the broken code
Fix
Complete previous first
Notes
Notes
🔍
Loading…