JavaScript Basics: Make Web Pages Come Alive · Lesson 2 of 8

Lesson 2: Variables, Types and Template Strings

Store values with let and const, learn the basic types, and build sentences with template strings.

Lesson 2: Variables, Types and Template Strings

A variable is a name that holds a value. Modern JavaScript uses two keywords: const for values that will not change, and let for values that will.

const name = "Anita";
let score = 70;
score = score + 10;
console.log(name, score);

Output:

Anita 80

Trying to change a const causes an error, which protects you from accidents. Use const by default and let only when needed. (You will also see the older var in old code; avoid it in new code.)

The basic types

TypeExampleNotes
number42, 3.14one type for whole and decimal numbers
string"Pokhara"text; single or double quotes
booleantrue, falseyes/no values (lower case)
undefinedundefineda variable with no value yet
nullnulldeliberately empty
console.log(typeof 42);
console.log(typeof "hello");
console.log(typeof true);
console.log(typeof undefined);

Output:

number
string
boolean
undefined

Template strings

Wrap text in backticks ` and put values inside ${ } to build sentences.

const city = "Butwal";
const year = 2026;
console.log(`I live in ${city} and it is ${year}`);
console.log(`2 + 3 = ${2 + 3}`);

Output:

I live in Butwal and it is 2026
2 + 3 = 5

Turning text into numbers

const text = "25";
console.log(text + 5);
console.log(Number(text) + 5);

Output:

255
30

The first line joins text ("25" and "5" side by side). The second converts to a number first. Watching for this difference prevents many bugs.

Try it yourself

  1. Store your name and age in variables and print a sentence with a template string.
  2. Make a const for a price and a let for a quantity, then print the total.
  3. Print the type of each variable.

Next lesson: decisions with if.

Finished this lesson?Log in to save your progress and pick up where you left off.
Next lesson →

Comments

Loading comments…

MeroSir Team
A free MeroSir course. Run the examples yourself and try the exercises: you learn programming by doing. Spotted a mistake? Use the Report button above the lesson.