CodingNic

Variables, Data Types & Operators

Data Types

Variables, Data Types & Operators 15 min read

Data Types

Objectives

By the end of this chapter, you should be able to:

  • Name JavaScript’s primitive data types
  • Create and recognize values of each type
  • Use typeof to check the type of a value
  • Explain the typeof null === 'object' quirk

💡 Why this matters: Every value in your program is one of a handful of types. Knowing which one you’re holding tells you what you can and can’t do with it.

What Is a Type?

Every value in JavaScript has a type: a category that determines what kind of data it is and what you can do with it. JavaScript has a small set of primitive types, meaning each one holds a single, simple value (as opposed to a collection of values, which you’ll meet in a later module).

Number

A single type covers all numbers in JavaScript, whether whole or decimal. There’s no separate “integer” type like in some languages.

javascript
const age = 30;
const price = 19.99;
const negative = -5;

console.log(age, price, negative);
// 30 19.99 -5

String

A string is text, wrapped in single quotes, double quotes, or backticks.

javascript
const first = "Priya";
const greeting = 'Hello there';
const template = `Hi, ${first}`;

console.log(first, greeting, template);
// Priya Hello there Hi, Priya

Backticks (`) create a template literal, which lets you embed a variable directly inside a string using ${}. That’s usually more readable than joining strings with +.

Boolean

A boolean is one of exactly two values: true or false. Booleans represent yes/no, on/off, is/isn’t.

javascript
const isLoggedIn = true;
const hasFinished = false;

console.log(isLoggedIn, hasFinished);
// true false

Undefined

undefined is what a variable holds when it’s been declared but never given a value.

javascript
let nextStep;
console.log(nextStep);
// undefined

You’ll also see undefined show up any time JavaScript expects a value but doesn’t have one, such as a missing object property (covered in a later module).

Null

null represents the deliberate, explicit absence of a value. Unlike undefined, which usually means “nothing was set yet,” null means “this was set to nothing on purpose.”

javascript
let selectedUser = null;
console.log(selectedUser);
// null

A common pattern: start a variable as null to mean “nothing selected yet,” then assign it a real value later once something is chosen.

Two Rare Types (Just Know They Exist)

JavaScript has two more primitive types you’re unlikely to need as a beginner:

  • symbol, used to create guaranteed-unique identifiers, mostly inside library code.
  • bigint, used for whole numbers too large for the regular number type to represent safely.

You don’t need to use either right now. They’re mentioned so the names aren’t a surprise if you come across them later.

Checking a Type with typeof

The typeof operator tells you the type of any value, returned as a string.

javascript
console.log(typeof 42);
// number

console.log(typeof "hello");
// string

console.log(typeof true);
// boolean

console.log(typeof undefined);
// undefined

The typeof null Quirk

Given everything above, you’d expect typeof null to be "null". It isn’t.

javascript
console.log(typeof null);
// object

This is a well-known bug in JavaScript, not something to imitate or explain away. It dates back to the very first version of the language in 1995 and has never been fixed, because fixing it would break too much existing code across the web. When you need to check specifically for null, compare the value directly instead of relying on typeof:

javascript
const selectedUser = null;
console.log(selectedUser === null);
// true

Try It

  1. Declare one variable for each type covered in this lesson (number, string, boolean, undefined, null) and log all five in a single console.log() call.
  2. Run typeof on each of the five variables you just created and log the results.
  3. Confirm for yourself that typeof null prints "object", and write a comment above it explaining why that’s a known quirk, not a rule to follow.

Recap

  • JavaScript’s core primitive types are number, string, boolean, undefined, and null.
  • symbol and bigint exist but are rarely needed as a beginner.
  • typeof returns a value’s type as a string.
  • typeof null returns "object", a historical bug in the language. Use === null to actually check for null.

Next lesson: operators, and how JavaScript converts values between types.