Skip to content
Educora
Beginner12 min2 / 27

Variables and data types

Learn to create variables with `let` and `const`, name them well and recognise JavaScript's basic data types.

Check yourself
In this lesson you will learn
  • Declare variables with let and const and assign values to them
  • Give variables valid and meaningful names
  • Find out the type of a value with typeof

A game remembers your name, your score and which level you are on. An online shop knows how many items are in your basket and what they cost in total. Programs remember information in variables — think of them as boxes with a label on them.

Creating a variable: let

Definition
Variable

A named place in memory that stores a value. You can read the value by its name and change it when needed.

We declare a variable with the keyword let and assign it a value with the = sign. Careful: in programming = does not mean “equals” — it means “store the value on the right in the variable on the left”. That is why score = score + 5 is a perfectly valid statement: 5 is added to the old value.

JavaScript
let score = 0;
console.log('Start:', score);

score = 10;
console.log('After level 1:', score);

score = score + 5;
console.log('After bonus:', score);
▸ Expected output
Start: 0
After level 1: 10
After bonus: 15

We write let only when we create the variable for the first time. For later changes the name and the new value are enough.

Constants: const

Some values never change while a program runs: the name of a country, the number of days in a week, a tax rate. For these we use const (short for constant). A variable created with const cannot be assigned a new value.

JavaScript
const country = 'Azerbaijan';
const capital = 'Baku';
let temperature = 18;

temperature = 21;
console.log(capital, 'is the capital of', country);
console.log('Temperature:', temperature);
▸ Expected output
Baku is the capital of Azerbaijan
Temperature: 21
JavaScript
const capital = 'Baku';
capital = 'Ganja';
Expected output
Uncaught TypeError: Assignment to constant variable.
Trying to give a const variable a new value ends with an error.

Naming variables

A name may contain letters, digits and the signs _ and $, but it cannot start with a digit or contain spaces. Keywords such as let, const or if cannot be names. In JavaScript, names made of several words are written in camelCase: the first word starts with a small letter, every next word with a capital.

NameAllowed?Why
userName✓camelCase, meaningful
totalPrice2✓a digit may come at the end
2ndPlace✗starts with a digit
first name✗contains a space
const✗it is a keyword
x1✓allowed, but it says nothing about its content

Data types and typeof

Every value has a type. The type decides what you can do with the value: numbers can be added, text can be turned into capital letters. The typeof operator returns a value's type as a string.

TypeExampleWhat it stores
number42, 3.14, -7whole and decimal numbers — all one type
string'Leyla', '42'text, in quotes
booleantrue, falsea logical value: true or false
undefinedundefinedno value has been given yet
nullnullan intentionally “empty” value
There are two rarer types too — bigint (very large integers) and symbol. You will meet objects in later lessons.
JavaScript
console.log(typeof 42);
console.log(typeof 3.14);
console.log(typeof 'Leyla');
console.log(typeof '42');
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof null);
▸ Expected output
number
number
string
string
boolean
undefined
object

JavaScript is a dynamically typed language: the same variable can hold a number first and a string later — the type belongs to the value, not to the variable. undefined means “nothing has been stored yet”, while null means “there is deliberately nothing here”.

JavaScript
let value = 42;
console.log(value, typeof value);
value = 'forty-two';
console.log(value, typeof value);

let winner;
console.log('Before the game:', winner);
winner = null;
console.log('Draw, no winner:', winner);
winner = 'Murad';
console.log('Winner:', winner);
▸ Expected output
42 number
forty-two string
Before the game: undefined
Draw, no winner: null
Winner: Murad
Exercise

Increase temperature by 5 degrees. Then print two lines: the city and the new temperature (Baku 23), and then the types of both variables with typeof (string number).

Exercise · JavaScript
let city = 'Baku';
let temperature = 18;
// 1) increase temperature by 5
// 2) print the city and the temperature
// 3) print the types of both variables
▸ Expected output
Baku 23
string number

Key points

  • A variable is a named box that stores a value; = assigns a value to it.
  • const is for values that stay the same, let for values that change; var is outdated.
  • Names don't start with a digit, contain no spaces and are written in camelCase.
  • Basic types: number, string, boolean, undefined, null; typeof shows the type.
  • JavaScript is dynamically typed: the type belongs to the value, not the variable.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
Which keyword should you choose for a value that will never change?