Quick answer: JavaScript data types come in eight kinds — string, number, bigint, boolean, undefined, null, symbol and object — and you declare them with three keywords: const, let and the older var. Use const by default and let only when the value must be reassigned; avoid var. string, number, bigint, boolean, undefined, null, symbol and object.

const, let and var

const name = "Ada";      // cannot be reassigned
let count = 0;           // can be reassigned
count = count + 1;

var old = "avoid";       // function-scoped, legacy
  • const — a binding you will not reassign. Note that a const array or object can still have its contents changed; only the binding is fixed.
  • let — a binding you will reassign, such as a counter or an accumulator.
  • var — the pre-2015 form. It is function-scoped rather than block-scoped and can surprise you.

    Use it only in legacy code.

const list = [1, 2];
list.push(3);            // allowed — contents changed, binding unchanged
// list = [];            // TypeError — cannot reassign a const

The primitive JavaScript data types

TypeExamplesNotes
string"hello", 'hi', backticksBackticks allow interpolation
number42, 3.14, NaNOne type for integers and decimals
bigint9007199254740993nFor integers beyond the safe range
booleantrue, falseResult of comparisons
undefined—Declared but not assigned
null—Deliberately empty
symbolSymbol("id")A unique key
object{}, [], functionsEverything that is not a primitive

number being one type is worth remembering: 1 === 1.0 is true, integer division produces decimals (7 / 2 is 3.5), and very large values lose precision, which is why bigint exists.

Strings and template literals

const user = "Ada";
const greeting = `Hello, ${user}!`;      // template literal
const multi = `line one
line two`;                                // real multiline strings
const { length } = user;                  // 3

Prefer backticks with ${...} over concatenation with +. Interpolation keeps the message readable and avoids accidental type coercion when a value is not a string.

JavaScript data types: truthy, falsy and the equality question

Some values are treated as false in a condition. The list is short and worth memorising: false, 0, -0, 0n, "", null, undefined, NaN. Everything else is truthy — including empty arrays and empty objects, which surprises people.

if ([]) console.log("runs");        // empty array is truthy
if (0)   console.log("skipped");    // 0 is falsy

"" == 0        // true  — loose equality coerces
"" === 0       // false — strict equality does not

null == undefined   // true
null === undefined  // false

Use === and !== almost always. Loose == performs type coercion with a long rule table, and the only place it beats strict equality is the deliberate value == null check, which matches both null and undefined at once.

Number and string conversion between JavaScript data types

Number("42")        // 42
Number("")          // 0
parseInt("42px", 10) // 42 — stops at the first non-digit
String(42)          // "42"
(42).toFixed(2)     // "42.00"
[1,2,3].join(", ")  // "1, 2, 3"

Prefer explicit Number() and String() over relying on implicit coercion. And remember that NaN is the only value not equal to itself: test it with Number.isNaN(value), never with ===.

Next in the series: functions. To learn JavaScript properly with a mentor, Ampersand Academy teaches web development one-to-one.

Frequently asked questions

When should I use const instead of let in JavaScript?

Use const by default. Switch to let only when you must reassign the variable, such as a counter. const still allows you to change an array’s or object’s contents; it only prevents rebinding.

What is the difference between == and === in JavaScript?

=== compares value and type without coercion. == coerces the operands to a common type first, which produces surprising results such as empty string equalling zero. Prefer === and !==.

What are the falsy values in JavaScript?

false, 0, -0, 0n, empty string, null, undefined and NaN. Everything else is truthy, including empty arrays and empty objects, which often surprises beginners.

Why is typeof null equal to object?

It is a long-standing bug from the language’s first version that cannot be fixed without breaking the web. Check for null explicitly with value === null instead of relying on typeof.

How do I check for NaN in JavaScript?

Use Number.isNaN(value). NaN is the only value not equal to itself, so value === NaN is always false and will never detect it.

Last updated on · Written by