Back to Tutorials
📝 Beginner

Variables and Data Types

5 min read | Basics

Variables and Data Types

JavaScript has three ways to declare variables: var, let, and const.

let and const (ES6+)

let name = "John";     // Can be reassigned
const age = 25;        // Cannot be reassigned

Data Types

JavaScript has 7 primitive types:

// String
let greeting = "Hello, World!";

// Number let count = 42; let price = 19.99;

// Boolean let isActive = true;

// undefined let notDefined;

// null let empty = null;

// Symbol (ES6) let sym = Symbol('unique');

// BigInt (ES2020) let bigNumber = 9007199254740991n;

typeof Operator

typeof "hello"     // "string"
typeof 42          // "number"
typeof true        // "boolean"
typeof undefined   // "undefined"
typeof null        // "object" (historical bug)
typeof {}          // "object"
typeof []          // "object"
typeof function(){} // "function"

Best Practices

  • Use const by default
  • Use let when you need to reassign
  • Avoid var in modern JavaScript