Skip to content
beginnerPhase 32 · JavaScript Fundamentals

Variables

Compare var, let, and const declarations, scoping rules, and best practices.

30m
0 problems
Topic Progress0%

var let const

var let const

var (Legacy)

var name = 'John';
var name = 'Jane'; // Allowed (re-declaration)
name = 'Bob';      // Allowed (re-assignment)

Characteristics:

  • Function-scoped (not block-scoped)
  • Hoisted to top of function
  • Can be re-declared and reassigned
  • Global when declared outside functions

let (Modern)

let age = 25;
// let age = 30;  // Error: re-declaration
age = 26;         // Allowed (re-assignment)

Characteristics:

  • Block-scoped
  • Not hoisted (Temporal Dead Zone)
  • Cannot be re-declared
  • Can be reassigned

const (Modern)

const PI = 3.14159;
// PI = 3.14;  // Error: re-assignment
// const PI;   // Error: missing initializer

Characteristics:

  • Block-scoped
  • Not hoisted (Temporal Dead Zone)
  • Cannot be re-declared or reassigned
  • Must be initialized

When to Use Each

// Use const by default
const API_URL = 'https://api.example.com';

// Use let when value changes
let counter = 0;
counter++;

// Avoid var in modern code
// var is only needed for very old browser support

Common Mistakes

// Wrong: using var in loops
for (var i = 0; i < 5; i++) {
  setTimeout(() => console.log(i), 1000);
}
// Output: 5, 5, 5, 5, 5

// Correct: using let in loops
for (let i = 0; i < 5; i++) {
  setTimeout(() => console.log(i), 1000);
}
// Output: 0, 1, 2, 3, 4

Variable Naming

Variable Naming

Rules

// Must start with letter, _, or $
let name = 'John';    // Valid
let _private = true;  // Valid
let $element = null;  // Valid
let 1name = 'Error';  // Invalid

// Can contain letters, digits, _, $
let firstName = 'John';  // Valid
let user_name = 'Jane';  // Valid
let user-name = 'Error'; // Invalid (hyphen)

// Case sensitive
let Name = 'John';
let name = 'Jane';  // Different variable

Conventions

// camelCase (most common)
let firstName = 'John';
let getUserData = function() { };

// PascalCase (classes, components)
class UserComponent { }

// UPPER_SNAKE_CASE (constants)
const MAX_RETRIES = 3;
const API_BASE_URL = 'https://api.example.com';

// _prefix (private convention)
let _internalState = {};

Reserved Words

// Cannot use as variable names
let if = 'Error';       // Error
let class = 'Error';    // Error
let return = 'Error';   // Error
let const = 'Error';    // Error

// These are reserved for future use
let implements = 'Error';
let interface = 'Error';
let package = 'Error';
let private = 'Error';
let protected = 'Error';
let public = 'Error';
let static = 'Error';

Meaningful Names

// Bad
let x = 10;
let data = {};
let temp = null;

// Good
let itemCount = 10;
let user = {};
let currentUser = null;

Scope Introduction

Scope Introduction

Scope determines where variables are accessible.

Global Scope

let globalVar = 'I am global';

function example() {
  console.log(globalVar); // Accessible
}

Function Scope

function example() {
  var localVar = 'I am local';
  console.log(localVar); // Accessible
}

console.log(localVar); // Error: localVar is not defined

Block Scope

if (true) {
  let blockLet = 'I am block scoped';
  var blockVar = 'I am function scoped';
}

console.log(blockVar);  // Works (var is function scoped)
console.log(blockLet);  // Error: blockLet is not defined

Scope Chain

let outer = 'outer';

function outerFunc() {
  let middle = 'middle';
  
  function innerFunc() {
    let inner = 'inner';
    console.log(outer);  // Accessible
    console.log(middle); // Accessible
    console.log(inner);  // Accessible
  }
  
  innerFunc();
}

Temporal Dead Zone

// TDZ for let and const
console.log(myVar);  // undefined (hoisted)
var myVar = 5;

console.log(myLet);  // Error: Cannot access before initialization
let myLet = 10;

Practice Problems

0/3solved
Build Variables Component

Create a reusable React component implementing Variables. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
Variables Testing

Write unit and integration tests for Variables using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
Variables Performance

Optimize Variables for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. What is the difference between var and let?

Question 1 options

2. Can you reassign a const variable?

Question 2 options

3. What is the Temporal Dead Zone?

Question 3 options

4. Which naming convention is used for constants?

Question 4 options

5. What happens when you use var in a for loop?

Question 5 options

Flashcards

Question

What is the difference between var, let, and const?

Answer

var: function-scoped, hoisted, re-declarable. let: block-scoped, TDZ. const: block-scoped, TDZ, not re-assignable.

Question

When should you use const vs let?

Answer

Use const by default. Use let when you need to reassign the variable.

Question

What is scope?

Answer

Scope determines where variables are accessible in your code.

Question

What is block scope?

Answer

Variables declared with let/const are only accessible within the block they're declared in.

Question

What is the Temporal Dead Zone?

Answer

The period between hoisting and declaration where let/const variables exist but throw an error if accessed.

Revision Notes

Key Takeaways

  • 1.const should be the default choice
  • 2.let is for variables that change
  • 3.var is function-scoped, let/const are block-scoped
  • 4.The Temporal Dead Zone prevents accessing let/const before declaration
  • 5.Use descriptive variable names

Interview Tips

  • Explain the difference between var, let, and const
  • Know what the Temporal Dead Zone is
  • Understand function vs block scope
  • Explain why var in loops causes issues

Cheat Sheet

Variables Cheat Sheet

var vs let vs const

var x = 5;   // Function-scoped, hoisted
let y = 10;  // Block-scoped, TDZ
const z = 15; // Block-scoped, TDZ, not re-assignable

Naming Rules

  • Must start with letter, _, or $
  • Case sensitive
  • No reserved words
  • camelCase convention

Scope

  • Global: accessible everywhere
  • Function: accessible within function
  • Block: accessible within block (let/const)

Best Practices

  • Use const by default
  • Use let when reassignment needed
  • Avoid var in modern code
  • Use descriptive names