Data Types in JavaScript

Understand how data types work in JavaScript, including primitive types like number, string, and boolean. Learn the difference between strongly typed and implicitly typed languages, and explore how JavaScript dynamically assigns types to variables at runtime.
Data Types in JavaScript
Data types are classifications or categories that determine the type of data a variable can hold. They define the nature of the data and the possible values it can take. JavaScript is not a strongly typed language, but it is an implicitly typed language.
Understanding Strongly Typed vs. Implicitly Typed Languages
- Strongly Typed Language: In a strongly typed language, the data type of a variable is strictly defined and cannot change. The type system enforces rules about how different types can interact, ensuring that errors are caught early in the development process.
- Implicitly Typed Language: In an implicitly typed language like JavaScript, variables do not have a predefined data type. Instead, the type of a variable is determined by the value assigned to it. This means that a variable can hold values of any type, and its type can change dynamically at runtime.
Example of Implicit Typing in JavaScript:
JavaScript allows variables to change types based on the values assigned to them. Here’s how it works:
// Initialize a variable with a number
let A = 100;
document.write("Type of A: " + typeof A); // Outputs: "Type of A: number"
// Assign a string to the same variable
A = "coders";
document.write("Type of A: " + typeof A); // Outputs: "Type of A: string"
// Assign a boolean to the same variable
A = true;
document.write("Type of A: " + typeof A); // Outputs: "Type of A: boolean"In this example:
- The variable
Ais initially assigned a numeric value (100), making its typenumber. - When
Ais reassigned to the string"coders", its type changes tostring. - Finally, when
Ais reassigned to the boolean valuetrue, its type becomesboolean.
Key Takeaways:
- In JavaScript, variables are not bound to a specific data type.
- The data type of a variable is determined by the value assigned to it at any given moment.
- This flexibility allows for dynamic programming but also requires careful handling to avoid type-related errors.
JavaScript data types are mainly classified into two categories: primitive types and non-primitive types.
1. Primitive Types
- Primitive data types are simple and immutable, meaning their values cannot be changed once created.
- They are stored in the memory stack using the "Last In, First Out" (LIFO) method.
- Primitive types include
number,string,boolean,null,undefined,symbol, andbigint.
Example of Primitive Types in JavaScript:
When a primitive value is assigned to a variable, it is stored directly in the memory stack. Let’s see how this works:
// Initialize a variable with a number
let x = 100;
document.write("x = " + x); // Outputs: "x = 100"
// Reassign a new value to the variable x
x = 200;
document.write("x = " + x); // Outputs: "x = 200"Explanation:
- When we initialize the variable
xwith the value100, this value is stored in the memory stack. - When we reassign
xwith the value200, the original value100remains unchanged in the memory stack. However,xnow points to the new value200, which is also stored in the memory stack. - The stack follows the "Last In, First Out" (LIFO) principle. The value
200was the last value assigned toxand is the first value accessed whenxis referenced, hence the output is200.
Additional Primitive Types:
- String: Immutable and stored in the stack using the same LIFO method.
- Boolean: Represents logical values (
trueorfalse) and follows the same LIFO storage principle. - Null: Represents the intentional absence of any value.
- Undefined: Indicates that a variable has been declared but not assigned a value.
- Symbol: Represents a unique identifier.
- BigInt: Allows representation of integers larger than the safe integer limit for number types.
Key Takeaways:
- Primitive data types in JavaScript are simple and immutable.
- They are stored in the memory stack following the "Last In, First Out" (LIFO) method.
- Reassigning a variable with a new value does not alter the original value but points to a new location in the stack with the updated value.



