In JavaScript, variables store values, data types describe what kind of value that is (number, string, boolean and others), and operators combine or compare values. You declare variables with let and const, and the language figures out the type of each value while the program runs. This guide on javascript variables data types is aimed at beginners, so every term is explained with a small example.
Modern JavaScript code uses const by default, let when a value must change, and avoids the older var.
A variable is a named container for a value. JavaScript has three ways to declare one.
| Keyword | Reassign? | Scope | Use today? |
|---|---|---|---|
const | No | Block | Yes, by default |
let | Yes | Block | Yes, when the value changes |
var | Yes | Function | Avoid in new code |
Block scope means a variable exists only inside the nearest pair of curly braces. Note that const stops reassignment of the variable, but an object or array held by a const can still be changed internally.
Names are case sensitive, can contain letters, digits, _ and $, and cannot start with a digit. By convention, use camelCase such as firstName.
JavaScript has seven primitive types and one structured type.
| Type | Example | Notes |
|---|---|---|
number | 42, 3.14 | Integers and decimals, 64-bit floating point |
bigint | 9007199254740993n | Integers of any size |
string | "hello", 'hi', `hi` | Text |
boolean | true, false | Logic values |
undefined | undefined | A variable with no value yet |
null | null | Intentional empty value |
symbol | Symbol("id") | Unique identifiers |
object | { a: 1 }, [1, 2] | Objects, arrays, functions and more |
Use typeof to check a value:
typeof null returns "object", which is a long-standing quirk of the language, so compare with === null instead. Because all numbers are floating point, 0.1 + 0.2 gives 0.30000000000000004; for money, store whole cents as integers.
Strings can use single quotes, double quotes or backticks. Backticks create template literals, which can span lines and embed expressions.
Strings are immutable: methods such as toUpperCase() return a new string rather than changing the original.
Operators work on values, called operands.
Comparison operators return booleans. Prefer strict equality.
=== compares value and type, while == converts types first, which causes surprises. Use === and !== almost always.
The logical operators are && (and), || (or) and ! (not). They work on truthy and falsy values: the falsy values are false, 0, "", null, undefined, NaN and 0n; everything else is truthy.
The nullish coalescing operator ?? gives a default only for null or undefined, which is safer than || when 0 or an empty string is a valid value. Optional chaining ?. stops safely when part of a path is missing.
JavaScript converts types automatically in some cases, which is called coercion.
To avoid surprises:
Number(), String() and Boolean().NaN with Number.isNaN(value), because NaN === NaN is false.=== instead of ==.let or const before its declaration throws an error.Both are block scoped. let allows reassignment, while const does not. Use const unless you know the value must change.
It is a bug from the first version of JavaScript that cannot be fixed without breaking existing sites. Check for null with value === null.
undefined means a value has not been assigned, while null is an intentional empty value that you set yourself.
No. var is function scoped and hoisted in confusing ways. let and const are clearer and safer in modern code.
Declare variables with const by default and let when they change. Know the primitive types (number, bigint, string, boolean, undefined, null, symbol) and objects, use typeof carefully, and prefer template literals for text. Use === for comparison, ?? and ?. for missing values, and convert types explicitly to avoid coercion surprises.