JavaScript: Data Types
JavaScript Data Types
1 Overview
📘 Data Types
Data types in JavaScript define the kind of values that can be stored and manipulated in a program. JavaScript has eight basic data types: seven primitive types (Number, BigInt, String, Boolean, null, undefined, Symbol) and one reference type (Object). JavaScript is dynamically typed, meaning variables can hold values of any type and can be reassigned to different types during execution.
2 Dynamic Typing
A value in JavaScript is always of a certain type. For example, a string or a number.
We can put any type in a variable. A variable can at one moment be a string and then store a number:
Programming languages that allow such things are called “dynamically typed”, meaning that data types exist, but variables are not bound to any of them.
3 Number
The number
type represents both integer and floating point numbers.
There are many operations for numbers: - Multiplication: *
- Division: /
- Addition: +
- Subtraction: -
- And more
3.1 Special Numeric Values
Besides regular numbers, there are “special numeric values” which also belong to this data type: Infinity
, -Infinity
, and NaN
.
3.1.1 Infinity
Infinity
represents the mathematical Infinity ∞. It is a special value that’s greater than any number.
We can get it as a result of division by zero:
Or just reference it directly:
3.1.2 NaN (Not a Number)
NaN
represents a computational error. It is a result of an incorrect or undefined mathematical operation:
NaN is sticky. Any further mathematical operation on NaN
returns NaN
:
So, if there’s a NaN
somewhere in a mathematical expression, it propagates to the whole result (exception: NaN ** 0
is 1
).
Math is “safe” in JavaScript
We can do anything: divide by zero, treat non-numeric strings as numbers, etc. The script will never stop with a fatal error. At worst, we’ll get NaN
as the result.
4 BigInt
In JavaScript, the number
type cannot safely represent integer values larger than (2^53-1)
(that’s 9007199254740991
), or less than -(2^53-1)
for negatives.
For most purposes, the ±(2^53-1)
range is quite enough, but sometimes we need really big integers, e.g., for cryptography or microsecond-precision timestamps.
BigInt
type was recently added to represent integers of arbitrary length.
A BigInt
value is created by appending n
to the end of an integer:
4.1 BigInt Operations
5 String
A string in JavaScript must be surrounded by quotes.
5.1 Three Types of Quotes
In JavaScript, there are 3 types of quotes:
- Double quotes:
"Hello"
- Single quotes:
'Hello'
- Backticks:
`Hello`
Double and single quotes are “simple” quotes. There’s practically no difference between them in JavaScript.
5.2 Template Literals (Backticks)
Backticks are “extended functionality” quotes. They allow us to embed variables and expressions into a string by wrapping them in ${…}
:
The expression inside ${…}
is evaluated and the result becomes a part of the string.
5.3 No Character Type
In some languages (C, Java), there is a special “character” type for a single character called char
.
In JavaScript, there is no such type. There’s only one type: string
. A string may consist of zero characters (be empty), one character, or many of them.
6 Boolean (Logical Type)
The boolean type has only two values: true
and false
.
This type is commonly used to store yes/no values: - true
means “yes, correct” - false
means “no, incorrect”
6.1 Boolean from Comparisons
Boolean values also come as a result of comparisons:
7 The “null” Value
The special null
value does not belong to any of the types described above.
It forms a separate type of its own which contains only the null
value:
In JavaScript, null
is not a “reference to a non-existing object” or a “null pointer” like in some other languages.
It’s just a special value which represents: - “nothing” - “empty” - “value unknown”
The code above states that age
is unknown.
8 The “undefined” Value
The special value undefined
also stands apart. It makes a type of its own, just like null
.
The meaning of undefined
is “value is not assigned”.
If a variable is declared but not assigned, then its value is undefined
:
8.1 Explicitly Assigning undefined
Technically, it is possible to explicitly assign undefined
to a variable:
But we don’t recommend doing that. Normally, one uses null
to assign an “empty” or “unknown” value to a variable, while undefined
is reserved as a default initial value for unassigned things.
9 Objects and Symbols
9.1 Objects
The object
type is special.
All other types are called “primitive” because their values can contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store collections of data and more complex entities.
9.2 Symbols
The symbol
type is used to create unique identifiers for objects.
Symbols are guaranteed to be unique. Even if we create many symbols with the same description, they are different values.
10 The typeof Operator
The typeof
operator returns the type of the operand. It’s useful when we want to process values of different types differently.
A call to typeof x
returns a string with the type name:
10.1 typeof Quirks
typeof Math
returns"object"
becauseMath
is a built-in objecttypeof null
returns"object"
- this is an officially recognized error intypeof
, kept for compatibility.null
is NOT an object.typeof alert
returns"function"
- functions belong to the object type, buttypeof
treats them differently
10.2 typeof Syntax
You may see two syntaxes:
Both are the same. typeof
is an operator, not a function. The parentheses are for grouping, not function calls.
11 Type Comparison Table
Type | typeof returns | Example |
---|---|---|
Number | “number” | 123 , 3.14 , Infinity |
BigInt | “bigint” | 123n |
String | “string” | "hello" , 'world' |
Boolean | “boolean” | true , false |
null | “object” ⚠️ | null |
undefined | “undefined” | undefined |
Object | “object” | {name: "John"} |
Symbol | “symbol” | Symbol("id") |
Function | “function” | function() {} |
12 Examples
12.1 Example 1: Type Checking
12.2 Example 2: Working with Different Types
12.3 Example 3: null vs undefined
12.4 Example 4: Template Literals
let name = "Alice";
let age = 25;
let city = "New York";
// Using template literals
let message = `My name is ${name}, I'm ${age} years old, and I live in ${city}.`;
console.log(message);
// Output: My name is Alice, I'm 25 years old, and I live in New York.
// With expressions
let price = 100;
let tax = 0.15;
console.log(`Total price: $${price + (price * tax)}`);
// Output: Total price: $115
12.5 Example 5: Special Values
13 Summary
- JavaScript has 8 basic data types
- 7 primitive types: Number, BigInt, String, Boolean, null, undefined, Symbol
- 1 reference type: Object
- JavaScript is dynamically typed - variables can change types
- Use
typeof
operator to check the type of a value null
represents “nothing” or “empty”undefined
represents “not assigned”- Strings can use single quotes, double quotes, or backticks (template literals)
- Special numeric values:
Infinity
,-Infinity
,NaN
BigInt
for very large integers (appendn
to the number)