Lesson 2 of 34 · Fundamentals
JavaScript Variables: let, const and Data Types
Programs need to remember things: a player's name, a score, whether a door is locked. Variables are how JavaScript stores those values so you can use and update them later.
In this lesson you'll learn the two modern ways to declare variables, let and const, and meet JavaScript's basic data types.
Declaring variables with const and let
A variable is a name attached to a value. You create one with a declaration:
const heroName = "Ada";
let score = 0;
console.log(heroName); // Ada
console.log(score); // 0
constdeclares a variable that can't be reassigned. Use it for values that stay the same.letdeclares a variable whose value can change later.
Updating a let variable is called reassignment:
let score = 0;
score = 10; // replace the value
score = score + 5; // use the old value to compute a new one
score += 5; // shorthand for score = score + 5
console.log(score); // 20
Trying to reassign a const throws an error:
const gravity = 9.81;
gravity = 10; // TypeError: Assignment to constant variable.
A good rule of thumb: start with const, and switch to let only when you actually need to reassign. That way, anyone reading your code immediately knows which values change.
You may also see var in older code. It's the original way to declare variables, but it has confusing scoping rules. Modern JavaScript uses let and const, and so will we.
Naming variables
- Names can contain letters, digits,
_and$, but can't start with a digit. - Names are case-sensitive:
scoreandScoreare different variables. - JavaScript convention is camelCase:
playerName,maxHealth,isGameOver. - Pick descriptive names.
daysUntilRaidbeatsd.
Basic data types
Every value in JavaScript has a type. The most common primitive types are:
const name = "Ada"; // string: text in quotes
const level = 7; // number: integers and decimals
const isOnline = true; // boolean: true or false
let weapon; // undefined: declared, but no value yet
const target = null; // null: intentionally "nothing"
You can ask JavaScript for a value's type with the typeof operator. It returns the type's name as a string:
console.log(typeof "Ada"); // string
console.log(typeof 7); // number
console.log(typeof true); // boolean
console.log(typeof weapon); // undefined
Watch out for look-alikes: 7 is a number but "7" is a string, and true is a boolean but "true" is a string. Quotes always make a string.
Common mistakes
- Using a variable before declaring it.
console.log(x); let x = 1;fails with aReferenceError. Declare first, use later. - Declaring the same name twice.
let score = 1; let score = 2;is aSyntaxError. To change the value, drop the secondlet:score = 2;. - Reaching for
constwhen the value changes. If you need+=on it later, it must belet.
Assignment
You're creating a new hero for the realm of Scriptoria. Declare these variables at the top level of your code:
heroName: a constant holding the string"Ada".isBrave: a constant holding the booleantrue.level: a variable declared withlet, starting at1.- On a later line, level the hero up by increasing
levelby2, so it ends at3. levelType: a constant holding the result oftypeof level.
The tests read your variables directly, and they also check that heroName really is a const and that level can be reassigned.
Variables and data types in other languages
- Python Variables and Data Types · Python
- Go Variables: var, := and Multiple Assignment · Go
- Java Variables and Primitive Data Types · Java
- C# Variables and Data Types · C#
Need a quick reminder? The JavaScript cheat sheet has every topic of Learn JavaScript on one page.
Lesson last updated on .