Skip to content
Codewilds

Lesson 2 of 34 · Fundamentals

JavaScript Variables: let, const and Data Types

easy10 XP3 min readFree lesson

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
  • const declares a variable that can't be reassigned. Use it for values that stay the same.
  • let declares 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: score and Score are different variables.
  • JavaScript convention is camelCase: playerName, maxHealth, isGameOver.
  • Pick descriptive names. daysUntilRaid beats d.

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 a ReferenceError. Declare first, use later.
  • Declaring the same name twice. let score = 1; let score = 2; is a SyntaxError. To change the value, drop the second let: score = 2;.
  • Reaching for const when the value changes. If you need += on it later, it must be let.

Assignment

You're creating a new hero for the realm of Scriptoria. Declare these variables at the top level of your code:

  1. heroName: a constant holding the string "Ada".
  2. isBrave: a constant holding the boolean true.
  3. level: a variable declared with let, starting at 1.
  4. On a later line, level the hero up by increasing level by 2, so it ends at 3.
  5. levelType: a constant holding the result of typeof 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

Need a quick reminder? The JavaScript cheat sheet has every topic of Learn JavaScript on one page.

Lesson last updated on .

Write code ↓
JavaScript+10 XP

Press Run to execute your code, or Submit to check it against the tests.