2.1 Variables
Variables are one of the most fundamental concepts in JavaScript and in any other programming language. In programming, a variable is used to temporarily store data in the memory of a computer. We store data at a specific memory location and give that location a name, so we can read or update the value later through that name. Think of variables as boxes: the value is what you put inside, and the variable name is the label on the box that helps you find it again.
Declaring a variable with let
Before ES6, JavaScript used the var keyword to declare variables. It came with several issues that we will cover later. Since ES6, the best practice is to use let:
let name;
console.log(name); // undefined
name = 'Brahim';
Initially, name is undefined because we declared it without assigning a value. We then initialize it with a string — single quotes are the most common style for strings in JavaScript. Don't forget to end each statement with a semicolon.
Naming rules and conventions
- A variable name cannot be a reserved keyword (like
let,if,else,var). - Names should be meaningful — avoid
let aorlet bwhich carry no intent. - A name cannot start with a number (e.g.
let 2nameis invalid). - Names cannot contain spaces or hyphens. For multiple words, use camelCase:
let myName;. - Names are case sensitive:
nameandNameare two different variables.
You can declare several variables on a single line by separating them with commas, or — and this is the cleaner modern style — declare each variable on its own line. That's it for this video on variables. In the next one we will talk about constants.
Summary
Variables are fundamental containers that temporarily store data in computer memory, named so they can be accessed later. Think of a variable like a labeled box: the box holds your data (the value), and the label is the variable name. In modern JavaScript, the `let` keyword is the best practice for declaring variables, replacing the older `var` keyword which has issues explained in subsequent lessons.
Key points
- A variable stores data in memory with a given name as a reference label, analogous to putting items in labeled boxes
- Use the `let` keyword to declare variables in modern JavaScript (preferred over the older `var`); always end declarations with a semicolon
- Variable names must follow strict rules: cannot be reserved keywords (like `let`, `if`, `var`), must be meaningful and descriptive, cannot start with a number, cannot contain spaces, and are case-sensitive
- Use camelCase naming convention: start with lowercase, capitalize the first letter of each subsequent word (e.g., `myName` instead of `my_name`)
- You can declare multiple variables on separate lines using `let` repeatedly, or on a single line separated by commas
- Variables initialized without a value return `undefined`; assign a value after declaration to store actual data
FAQ
What is the difference between declaring and initializing a variable?
Declaration means creating a variable with `let variableName;` (which initially equals `undefined`). Initialization means assigning it a value: `let variableName = 'value';`. You can do both in one statement.
Why can't variable names start with numbers or contain spaces?
JavaScript syntax rules prevent this. Names like `2names` cause syntax errors. If you need multiple words, use camelCase notation (e.g., `myFirstName`) instead of spaces.
Is camelCase mandatory for JavaScript variables?
No, but it is the modern convention and best practice in JavaScript. It makes code more readable and consistent with industry standards.