Values and Data Types
In this chapter, you will learn what a value is and what a data type is. A value is a single piece of data, like the number 7 or the word "hello". A data type is the kind of value it is. JavaScript uses the type to decide what it can do with a value — for example, you can do math with numbers, but not with words.
Why Types Matter
Every value in JavaScript has exactly one type. When you combine values, JavaScript follows rules based on those types. If you know the type, you can predict what your code will do. If you don't, you get surprises — like "2" + 2 turning into the string "22" instead of the number 4.
The Core Types
Strings
A string is text wrapped in quotes. It can hold letters, numbers, spaces — anything you can type.
const greeting = "Hello, world!";
const name = 'Ada';You can use double quotes or single quotes. Backticks (` ``) also make strings and let you insert values inside them:
const name = 'Ada';
const message = `Hi, ${name}!`; // "Hi, Ada!"Numbers
A number is a value for math. JavaScript has one number type for both whole numbers and decimals.
const age = 30; // whole number
const price = 9.99; // decimalBooleans
A boolean is a value that is either true or false. Booleans power decisions, like "is the user logged in?"
const isLoggedIn = true;
const hasPermission = false;null and undefined
These two types both mean "no value," but in different ways:
nullmeans "intentionally empty." A programmer set it on purpose.undefinedmeans "not set yet." If you create a variable and never give it a value, it isundefined.
let nextStep; // undefined — nothing assigned yet
nextStep = null; // null — deliberately empty for nowChecking a Type with typeof
The typeof operator tells you the type of any value as a string:
console.log(typeof "hi"); // "string"
console.log(typeof 42); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (a famous old bug!)Putting Types Together
Try this in your browser console (press F12 and click the Console tab):
console.log(2 + 2); // 4 — number plus number
console.log("2" + 2); // "22" — string plus number makes a string!
console.log("Hi " + 5); // "Hi5"
console.log(10 > 3); // true — comparison gives a booleanNotice "2" + 2: when you use + with a string, JavaScript converts the other value to a string and glues them together. With any other math operator, it tries to convert to a number instead:
console.log("6" - 2); // 4
console.log("6" * 2); // 12The safe habit: keep your types consistent, and use typeof when you are unsure.
Exercise
What does typeof "42" return, and why?
Show answer
It returns "string". The quotes make "42" a string of characters, not the number 42. Remove the quotes and typeof 42 returns "number".
Exercise
What is the difference between null and undefined?
Show answer
undefined means a value has not been set yet. null means a value was deliberately set to "nothing." Both mean "no value," but null is a choice a programmer makes.
Spotted a mistake, or code that does not work?