JS MASTER CLASS /// VARIABLES & TYPES /// LET CONST VAR /// SCOPE & HOISTING /// JS MASTER CLASS /// VARIABLES & TYPES /// LET CONST VAR /// SCOPE & HOISTING ///

Data Types & Variables

Learn to store information in memory using var, let, and const. Understand block scope and primitive types.

variables.js
1 / 12
12345

Tutor:Variables are the foundation of programming. They are like containers in your computer's memory where you can store data. In JavaScript, we have three keywords to declare them: var, let, and const.


Skill Matrix

UNLOCK NODES BY MASTERING JS CONCEPTS.

Concept: Declaring Variables

Store data with let and const. Avoid legacy var.

System Check

Which keyword allows you to declare a variable whose value cannot be reassigned?


Community Holo-Net

Discuss Scope & Hoisting

ACTIVE

Got caught in the Temporal Dead Zone? Share your JS bugs and get help from peers.

JS Types & Variables

Author

Pascual Vila

Fullstack Instructor // Code Syllabus

JavaScript uses variables to hold data in memory. ES6 modernized this by bringing us let and const to replace the problematic var. Let's break down how they work.

let & const vs var

var is function-scoped. This means if you declare a var inside an if block, it can leak out to the surrounding function. It also undergoes Hoisting, meaning its declaration is moved to the top of the code, resulting in an undefined value if accessed early.

let and const are block-scoped (bounded by {}). They solve the leakage problem. Furthermore, if you try to use them before declaration, you hit the Temporal Dead Zone (TDZ) and get a ReferenceError. Always prefer const for values that won't change, and let for variables that will be reassigned (like loop counters).

Primitive Data Types

JavaScript distinguishes between Primitives and Reference Types (Objects). Primitives are immutable values passed by value. There are 7 primitive types:

  • String: Text surrounded by quotes.
  • Number: Integers and floating point numbers.
  • Boolean: true or false.
  • Undefined: A variable declared but not assigned.
  • Null: Explicitly empty or non-existent value.
  • BigInt: Very large integers.
  • Symbol: Unique identifiers.
View Full Transcript+

In this lesson transcript, we explore the historical context of ES6 (ECMAScript 2015) which introduced block scoping. By minimizing the scope of variables, developers reduce cognitive load and prevent accidental overwrites. We also dive into how memory handles primitive types directly on the call stack, versus reference types which are stored in the heap.

JS Core Glossary

var

Function-scoped variable declaration. Can be hoisted and reassigned. Considered legacy.

snippet.js

let

Block-scoped variable declaration. Can be reassigned but not re-declared in the same block.

snippet.js

const

Block-scoped constant. Cannot be reassigned. Must be initialized upon declaration.

snippet.js

Hoisting

JavaScript's default behavior of moving declarations to the top of the current scope before code execution.

snippet.js

Scope

The current context of execution. Determines the accessibility of variables (Global, Function, or Block).

snippet.js

Primitive Type

Data that is not an object and has no methods (String, Number, Boolean, Null, Undefined, Symbol, BigInt).

snippet.js