JavaScript: The Complete Language Guide
Chapter 2 / 7· 20 min read· 0 cards

Data Types, Variables and Operators

JavaScript's core types, arithmetic, the crucial === operator, and modern template literals.

The core data types

JavaScript has a handful of basic data types you'll use constantly. The main ones:

  • Number — all numbers, whole or decimal: 42, 3.14, -7 (JavaScript has just one number type, unlike Java's int/double split)
  • String — text, in single or double quotes: "hello", 'world'
  • Booleantrue or false
  • undefined — a variable declared but not given a value
  • null — a deliberate "no value"
let age = 19;              // number
let name = "Mehul";        // string
let isStudent = true;      // boolean
let address;               // undefined (no value assigned)
let middleName = null;     // null (deliberately empty)

The difference between undefined and null trips people up: undefined means "no value has been set yet", while null means "intentionally empty". You'll mostly create null yourself when you want to clearly say "nothing here on purpose".


Arithmetic operators

The maths operators work as you'd expect, with one pleasant difference from Java and C — division gives a real decimal answer:

console.log(7 + 3);    // 10
console.log(7 - 3);    // 4
console.log(7 * 3);    // 21
console.log(7 / 3);    // 2.333... -- real division, no integer trap!
console.log(7 % 3);    // 1 -- remainder (modulo)
console.log(2 ** 3);   // 8 -- exponent (2 to the power 3)

Because JavaScript has only one number type, 7 / 3 gives 2.333... directly — no integer-division surprise like in Java or C. The modulo % (remainder) is especially useful: n % 2 === 0 checks if a number is even.


The critical operator: === versus ==

This is one of the most important things to learn correctly in JavaScript. There are two equality operators, and you should almost always use the triple equals ===:

console.log(5 === 5);      // true  -- strict equality
console.log(5 === "5");    // false -- different types (number vs string)

console.log(5 == "5");     // true  -- DON'T use this! loose equality
console.log(0 == "");      // true  -- surprising and confusing!
console.log(0 == false);   // true  -- more confusion

The double equals == does loose comparison — it tries to convert types before comparing, producing baffling results like 0 == "" being true. The triple equals === does strict comparison — it checks both value and type, with no surprises. The rule is simple and firm: always use === and !==. Make it a habit from day one and you'll avoid a whole category of bugs that plague beginners.


Truthy and falsy

JavaScript has a useful concept: every value is either "truthy" or "falsy" when used in a condition. The falsy values are few — memorise them: false, 0, "" (empty string), null, undefined, and NaN. Everything else is truthy. This lets you write clean checks:

let name = "";
if (name) {
    console.log("Name was provided");
} else {
    console.log("Name is empty");   // this runs — "" is falsy
}

let count = 5;
if (count) {   // truthy because it's not 0
    console.log("We have items");
}

This "truthiness" is used everywhere in real JavaScript to check whether a value exists or is empty without writing out a full comparison.


Template literals: the modern way to build strings

Joining strings with + gets clumsy. Modern JavaScript has a far cleaner tool — the template literal. Use backticks (`) instead of quotes, and drop variables right inside with ${...}:

const name = "Mehul";
const age = 19;

// Old way — clumsy
console.log("Hi " + name + ", you are " + age + " years old.");

// Modern way — clean template literal (note the backticks)
console.log(`Hi ${name}, you are ${age} years old.`);

// You can even do math inside
console.log(`Next year you'll be ${age + 1}.`);

Template literals are cleaner, easier to read, and let you put expressions right inside the text. They also span multiple lines naturally. From here on, prefer them for any string that includes variables — you'll use them constantly in modern JavaScript. Next, we'll dive into functions, the building blocks of every program.

Reading mode · scroll to read at your own pace

Finished "Data Types, Variables and Operators"?

Mark this chapter complete so you can pick up exactly where you left off. Your progress saves locally — sign in to sync across devices.

Was this chapter clear?

Try it yourself — open the Code Playground15+ languages — Python, JavaScript, Java, C++, SQL & more — full IDE-style editor, instant run. Your code is auto-saved per language.