Understanding data types is crucial in JavaScript as it determines how values are stored and manipulated in your code. In this comprehensive chapter, we'll explore data types in JavaScript from the ground up, covering basic types to more advanced concepts, with detailed explanations and illustrative examples.
Data Type | Description |
---|---|
Primitive | |
Number | Numeric values (integers, decimals) |
String | Sequences of characters (text) |
Boolean | Logical values (true or false) |
Undefined | Variable declared but not assigned |
Null | Intentional absence of a value |
BigInt | Integers too large for normal numbers (ES2020+) |
Non-Primitive | |
Object | Unordered collections of key-value pairs |
Array | Ordered collections of items with indexes |
Symbol | Unique and immutable identifiers (rarely used directly) |
JavaScript offers a set of primitive data types that represent fundamental kinds of data:
Represent numeric values, both integers (whole numbers) and floating-point numbers (decimals). They are stored in 64-bit floating-point format according to the IEEE 754 standard.
let age = 30;
let pi = 3.14159;
console.log(typeof age); // Output: number
console.log(typeof pi); // Output: number
Represent sequences of characters, often used for text. Strings are immutable (unchangeable) in JavaScript.
let name = "Alice";
let greeting = 'Hello, world!';
console.log(typeof name); // Output: string
console.log(typeof greeting); // Output: string
Represent logical values: true
or false
. They are used to indicate conditions or choices.
let isLoggedIn = true;
let isNightTime = false;
console.log(typeof isLoggedIn); // Output: boolean
console.log(typeof isNightTime); // Output: boolean
Represents a variable that has been declared but not yet assigned a value.
let uninitialized;
console.log(typeof uninitialized); // Output: undefined
Represents the intentional absence of a value. It’s different from undefined
as it’s explicitly used to indicate that there is no value.
let emptyVariable = null;
console.log(typeof emptyVariable); // Output: object (historical quirk)
BigInt is a new data type introduced in ES2020, allowing for the representation of arbitrarily large integers.
let bigNum = 123456789012345678901234567890n;
console.log(typeof bigNum); // Output: bigint
JavaScript also supports non-primitive data types that allow you to group and organize data:
Unordered collections of key-value pairs. Keys are typically strings, and values can be of any data type.
let person = {
name: "Bob",
age: 35,
skills: ["JavaScript", "Python"]
};
console.log(typeof person); // Output: object
Ordered collections of items, where each item has an index (starting from 0). Elements can be of any data type, and arrays are mutable (changeable).
let colors = ["red", "green", "blue"];
console.log(typeof colors); // Output: object (historical quirk)
console.log(colors[1]); // Output: green (accessing by index)
Introduced in ECMAScript 6, the symbol
data type represents a unique and immutable value that can be used as an identifier for object properties.
let sym = Symbol("description");
console.log(typeof sym); // Output: symbol
In JavaScript, functions are not considered data types in the same way as numbers, strings, or objects. They are a fundamental building block for creating reusable blocks of code, but they don’t hold data themselves.
The table should focus on data types that represent and store information. So because function is not mention in the Table
Lets see the Function Now
In JavaScript, functions are first-class citizens, meaning they can be assigned to variables, passed as arguments, and returned from other functions.
let add = function(a, b) {
return a + b;
};
console.log(typeof add); // Output: function
Understanding data types in JavaScript is fundamental for writing efficient and bug-free code. By knowing the various data types and their characteristics, you can better manage your data, perform operations effectively, and avoid common pitfalls. Whether you're working with basic types like numbers and strings or more complex types like objects and functions, a solid grasp of data types is essential for becoming proficient in JavaScript programming.Happy coding !❤️