JavaScript has different data types used to represent and manipulate various kinds of values. Understanding data types is essential for effective coding. The main data types in JavaScript are string
, number
, boolean
, null
, undefined
, and object
. This article explains each data type with examples.
A string
represents text and is created by enclosing characters within single quotes, double quotes, or backticks.
Example: Declaring and using strings
let greeting = "Hello, World!"; console.log(greeting); // Output: Hello, World! let name = 'Alice'; console.log("Hello, " + name); // Output: Hello, Alice let age = 25; console.log(`Age is ${age}`); // Output: Age is 25
Strings are commonly used for displaying and manipulating text in applications. JavaScript provides many methods for working with strings, such as length
, toUpperCase()
, and concat()
.
The number
data type represents both integer and floating-point numbers. JavaScript numbers are capable of representing large integers and decimals.
Example: Declaring and using numbers
let count = 10; console.log(count); // Output: 10 let price = 19.99; console.log(price); // Output: 19.99 let total = count * price; console.log("Total:", total); // Output: Total: 199.9
Numbers in JavaScript can be used in mathematical operations like addition, subtraction, multiplication, and division.
A boolean
represents a logical entity and can have only two values: true
or false
. Booleans are commonly used in conditional statements to control the flow of the program.
Example: Using booleans in conditional statements
let isLoggedIn = true; console.log(isLoggedIn); // Output: true let age = 20; let isAdult = age >= 18; console.log("Is adult:", isAdult); // Output: Is adult: true
Boolean values are often the result of comparison operations like ===
, >
, and <
.
null
represents the intentional absence of any object value. It is often used to indicate that a variable has been explicitly set to have no value.
Example: Using null
let emptyValue = null; console.log(emptyValue); // Output: null let user = { name: "Alice", age: 25 }; user = null; // User object is now null console.log(user); // Output: null
Null is often used to indicate "no value" or "empty" state for variables that may later hold objects or other data.
undefined
represents a variable that has been declared but not yet assigned a value. By default, JavaScript initializes variables to undefined
if they are not assigned a value.
Example: Using undefined
let unassigned; console.log(unassigned); // Output: undefined function sayHello(name) { console.log("Hello " + name); } sayHello(); // Output: Hello undefined
undefined
is a common result when accessing non-existing properties or variables that haven’t been initialized.
An object
is a complex data type that allows storing collections of data in key-value pairs. Objects are useful for representing structured data and can contain other data types, including arrays, functions, and other objects.
Example: Creating and using an object
let person = { name: "Alice", age: 25, isStudent: true }; console.log(person.name); // Output: Alice console.log(person["age"]); // Output: 25 person.city = "New York"; // Adding a new property console.log(person); // Output: { name: "Alice", age: 25, isStudent: true, city: "New York" }
Objects allow easy grouping and access of related data, making them useful for representing entities like users, products, or configurations.
JavaScript offers several data types to store and manipulate various kinds of values, from simple text and numbers to more complex structures like objects. Understanding data types is fundamental to working effectively in JavaScript and handling data efficiently in programs.