Data Types
In JavaScript, data types define the kind of values that variables can hold and how they are processed. Understanding data types is fundamental to programming because it helps you manage and manipulate data effectively. JavaScript has a variety of data types, categorized into primitive and complex types.
Primitive Data Types
Primitive data types are the most basic types of data and are immutable (cannot be changed after creation). JavaScript has the following primitive data types:
Number
- Represents both integer and floating-point numbers.
- Example:
42
,3.14
,-100
- JavaScript does not differentiate between integers and floating-point numbers; they are all represented as floating-point numbers.
let age = 25; let temperature = -10.5;
String
- Represents a sequence of characters enclosed in single quotes (
'
), double quotes ("
), or backticks (`
for template literals). - Example:
"Hello, World!"
,'JavaScript'
let greeting = "Hello, World!"; let name = 'Alice'; let message = `Welcome, ${name}`;
- Represents a sequence of characters enclosed in single quotes (
Boolean
- Represents a logical value of
true
orfalse
. - Example:
true
,false
let isActive = true; let isCompleted = false;
- Represents a logical value of
Undefined
- Represents a variable that has been declared but not yet assigned a value.
- Example: A variable that is declared without initialization is
undefined
.
let result; console.log(result); // Output: undefined
Null
- Represents an intentional absence of value. It is a primitive value that signifies "nothing" or "empty".
- Example:
null
let data = null;
Symbol (ES6)
- Represents a unique and immutable value often used as object property keys. Symbols are created using the
Symbol
function. - Example:
Symbol('description')
let uniqueId = Symbol('id');
- Represents a unique and immutable value often used as object property keys. Symbols are created using the
BigInt (ES11)
- Represents integers with arbitrary precision. It is used for very large integers beyond the safe range of the
Number
type. - Example:
9007199254740991n
let bigNumber = 123456789012345678901234567890n;
- Represents integers with arbitrary precision. It is used for very large integers beyond the safe range of the
Complex Data Types
Complex data types are more advanced and can hold collections of values or more complex structures. JavaScript has the following complex data types:
Object
- Represents a collection of key-value pairs. Objects can hold multiple values in a single variable and can also include methods.
- Example:
let person = { name: "Alice", age: 30, greet: function() { return `Hello, ${this.name}`; } }; console.log(person.name); // Output: Alice console.log(person.greet()); // Output: Hello, Alice
Array
- Represents a list-like collection of values. Arrays are ordered and can hold items of any data type.
- Example:
let fruits = ["Apple", "Banana", "Cherry"]; console.log(fruits[1]); // Output: Banana
Function
- Functions are first-class objects that can be assigned to variables, passed as arguments, and returned from other functions.
- Example:
function add(a, b) { return a + b; } console.log(add(5, 3)); // Output: 8
Date
- Represents date and time. JavaScript provides the
Date
object to work with dates and times. - Example:
let now = new Date(); console.log(now); // Output: Current date and time
- Represents date and time. JavaScript provides the
RegExp
- Represents regular expressions used for pattern matching and string manipulation.
- Example:
let pattern = /hello/i; // Case-insensitive search for "hello" console.log(pattern.test("Hello World")); // Output: true
Type Conversion
JavaScript performs implicit type conversion (coercion) in many cases, converting between types automatically. You can also perform explicit type conversions using various methods:
To String
let num = 123; let str = String(num); // Explicit conversion
To Number
let str = "123"; let num = Number(str); // Explicit conversion
To Boolean
let value = 1; let bool = Boolean(value); // Explicit conversion
Type Checking
You can check the type of a variable using the typeof
operator:
console.log(typeof 42); // Output: "number"
console.log(typeof "Hello"); // Output: "string"
console.log(typeof true); // Output: "boolean"
console.log(typeof null); // Output: "object" (historical bug)
console.log(typeof undefined); // Output: "undefined"
console.log(typeof { name: "Alice" }); // Output: "object"
console.log(typeof [1, 2, 3]); // Output: "object"
console.log(typeof function() {}); // Output: "function"