What is Data Types?
In JavaScript, data types are categories of data that tell the interpreter what type of data it is working with. For example, the number 5 is a numeric data type, while "Hello" is a string data type. JavaScript has different types of values that can be assigned to variables. These are categorized into primitive and non-primitive (reference) data types.
JavaScript is a dynamically typed language, which means that the type of a variable is determined at runtime (during program execution) rather than at compile time (before execution).
Primitive Data Types
Primitive data types are the most basic data types in JavaScript. They are immutable (cannot be altered). The primitive data types in JavaScript are:
- Number: Represents numeric values (integers and floating-point numbers).
- String: Represents sequences of characters, enclosed in single, double, or backticks.
- Boolean: Represents two values: `true` or `false`.
- Undefined: Represents a variable that has been declared but not assigned a value.
- Null: Represents the intentional absence of any value or object.
- Symbol: Represents unique and immutable values (introduced in ES6).
- BigInt: Represents large integers beyond the safe limit of `Number` (introduced in ES11).
Examples of Primitive Data Types
// Number
let age = 25;
console.log(age); // Output: 25
// String
let name = "Prakash";
console.log(name); // Output: Prakash
// Boolean
let isDeveloper = true;
console.log(isDeveloper); // Output: true
// Undefined
let x;
console.log(x); // Output: undefined
// Null
let y = null;
console.log(y); // Output: null
// Symbol
let uniqueId = Symbol("id");
console.log(uniqueId); // Output: Symbol(id)
// BigInt
let bigNumber = 9007199254740991n;
console.log(bigNumber); // Output: 9007199254740991nNon-Primitive Data Types
Non-primitive data types are objects, which can hold multiple values and methods. These include:
- Object: A collection of key-value pairs.
- Array: A special type of object used to store ordered collections of values.
- Function: A reusable block of code that can be executed whenever called.
Examples of Non-Primitive Data Types
// Object
let person = { name: "Prakash", age: 25 };
console.log(person.name); // Output: Prakash
// Array
let numbers = [1, 2, 3, 4, 5];
console.log(numbers[0]); // Output: 1
// Function
function greet() {
return "Hello, world!";
}
console.log(greet()); // Output: Hello, world!How to Check the Data Type of a Variable
In JavaScript, you can use the `typeof` operator to check the data type of a variable. Here's an example:
let num = 42;
console.log(typeof num); // Output: 'number'
let text = "Hello";
console.log(typeof text); // Output: 'string'
let isActive = true;
console.log(typeof isActive); // Output: 'boolean'
let obj = {};
console.log(typeof obj); // Output: 'object'
let func = function() {};
console.log(typeof func); // Output: 'function'Note : that when checking for **null**, JavaScript returns `'object'`, which is a bug in JavaScript:
let a = null;
console.log(typeof a); // Output: 'object'Key Differences Between Primitive and Non-Primitive Types
- Primitive: Stored directly in the memory location and immutable.
- Non-Primitive: Stored as a reference in memory and mutable.
Takeaways
Understanding data types is crucial for writing effective JavaScript programs. Always ensure you use the right data type for the task at hand, and remember the key differences between primitive and non-primitive data types.