JavaScript Course

JavaScript Variables: var, let, const

A self-study guide for Python 3 compiled from the materials on this site. Primarily intended for those who want to learn the Python programming language from scratch.

Declaring Variables

JavaScript has three ways to declare variables:

var oldWay = "avoid this in modern JS";
let myName = "Alice";       // can be reassigned
const PI = 3.14159;         // cannot be reassigned

let vs const

Featureletconst
Can be reassigned?✅ Yes❌ No
Must be initialized?NoYes
Block scoped?✅ Yes✅ Yes

Variable naming rules

let firstName = "Alice";    // camelCase — recommended
let _private = 42;          // underscore OK
let $price = 9.99;          // dollar sign OK
// let 2fast = true;        // ❌ cannot start with number

Data Types

let num = 42;               // number
let str = "Hello";          // string
let bool = true;            // boolean
let nothing = null;         // null
let undef;                  // undefined
let obj = { name: "Bob" };  // object
let arr = [1, 2, 3];        // array (also object)