Learn / Programming / JavaScript / Variables and Types

JavaScript · Lesson 2 of 6

Variables and Types

let, const, primitive types, template strings and the equality traps.

  • Beginner
  • 13 min read
  • 3 objectives

Before this lessonLesson 1: Hello, JavaScript

What you will learn

  • Choose const or let
  • Name the primitive types
  • Use === not ==

You create variables with const and let. const means the name cannot be reassigned; let allows reassignment. A good habit: use const by default and switch to let only when the value must change. Avoid the old var; its scoping rules cause bugs.

Storing information

Programs are mostly about moving information around: a name typed in a form, a price from a server, a score in a game. A variable is a named place to keep that information so you can use it later. In modern JavaScript you create one with const (a value you will not reassign) or let (a value that may change).

const siteName = "stackcone";
let visitors = 10;
visitors = visitors + 1;
console.log(siteName, visitors);
Output
stackcone 11

Try to reassign a const and JavaScript refuses with a TypeError. That is a feature: it protects you from accidentally overwriting something. A good habit is to write const by default and switch to let only when you truly need to change the value.

const name = "Ada";
let count = 0;
count += 1;
// name = "Bob";   // TypeError: assignment to constant
console.log(name, count);
Output
Ada 1

The types

  • number: all numbers, integers and decimals alike (42, 3.14). Also NaN and Infinity.
  • string, boolean, undefined (declared but no value), null (deliberately empty), bigint and symbol.
  • object: everything else, including arrays and functions.
console.log(typeof 42, typeof "hi", typeof true);
console.log(typeof undefined, typeof null, typeof [], typeof {});
Output
number string boolean
undefined object object object

Template strings

Backticks create template literals: embed any expression with ${...} and write multi-line text without escapes.

const items = 3, price = 4.5;
console.log(`${items} items cost $${(items * price).toFixed(2)}`);
Output
3 items cost $13.50

Equality: always use ===

== converts types before comparing, giving odd results. === compares value and type and behaves predictably.

console.log(0 == "0", 0 === "0");
console.log(null == undefined, null === undefined);
console.log(0.1 + 0.2 === 0.3);
Output
true false
true false
false

The last line is not a JavaScript quirk but floating-point math: decimals are stored in binary, so 0.1 + 0.2 is 0.30000000000000004. Compare money in whole cents.

Truthy and falsy

In a condition, these are false: false, 0, "", null, undefined, NaN. Everything else, including "0" and [], is true. Use ?? to fall back only for null/undefined, and ?. to read nested values safely.

const user = { profile: null };
console.log(user.profile?.email ?? "no email");
Output
no email

The typeof operator

When unsure what you are holding, ask. typeof returns the type as text. There is one famous quirk: typeof null says "object", a bug from 1995 that can never be fixed.

console.log(typeof 42, typeof 3.14);
console.log(typeof "hi", typeof true);
console.log(typeof undefined, typeof null);
console.log(typeof [1, 2], Array.isArray([1, 2]));
Output
number number
string boolean
undefined object
object true

undefined versus null

undefined means "no value has been given yet": a variable you declared but never set, or a property that does not exist. null means "I deliberately have nothing here." You will mostly meet undefined as JavaScript's way of saying you asked for something that was not there.

let notSet;
const user = { name: "Ada" };
console.log(notSet);
console.log(user.email);
console.log(user.email ?? "no email on file");
Output
undefined
undefined
no email on file

Numbers: what to watch for

console.log(0.1 + 0.2);
console.log((0.1 + 0.2).toFixed(2));
console.log(10 / 3, 10 % 3);
console.log(Number("42"), Number("abc"));
console.log(Number.isNaN(Number("abc")));
Output
0.30000000000000004
0.30
3.3333333333333335 1
42 NaN
true

Like most languages, JavaScript stores decimals in binary, so 0.1 + 0.2 is not exactly 0.3. For money, count whole cents as integers or round with toFixed. NaN ("not a number") is what you get when a conversion fails; it is the only value not equal to itself, so test it with Number.isNaN.

Converting between types on purpose

console.log(String(123) + "!");
console.log(Number("7") + 1);
console.log(parseInt("42px"), parseFloat("3.5kg"));
console.log(Boolean(""), Boolean("text"), Boolean(0), Boolean([]));
Output
123!
8
42 3.5
false true false true

Key takeaways

  • Use const by default and let when a value must change; avoid var.
  • Primitive types: string, number, boolean, undefined, null, bigint, symbol.
  • typeof reveals a type; Number.isNaN checks failed conversions.
  • Always compare with ===, and know which values are falsy.
// Write your solution here
Up next · Lesson 3Functions and ScopeDeclarations, arrow functions, closures and how scope works.