Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to create a unique ID for each object in JavaScript?
In JavaScript, creating unique IDs for objects is essential for tracking and identifying instances. The most reliable method is using Symbol(), which generates guaranteed unique identifiers.
Using Symbol() for Unique IDs
The Symbol() function creates a unique symbol every time it's called, even with the same description. This makes it perfect for generating unique object identifiers.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Unique Object IDs</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.result {
font-size: 18px;
font-weight: 500;
color: rebeccapurple;
margin: 20px 0;
}
button {
padding: 10px 20px;
font-size: 16px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Create Unique IDs for Objects</h1>
<div class="result"></div>
<button class="btn">Check ID Uniqueness</button>
<h3>Click the button to verify that each object has a unique ID</h3>
<script>
let resEle = document.querySelector(".result");
let btnEle = document.querySelector(".btn");
// Create objects with unique Symbol IDs
let obj1 = {
id: Symbol("UID"),
name: "Object 1",
value: 100
};
let obj2 = {
id: Symbol("UID"),
name: "Object 2",
value: 200
};
btnEle.addEventListener("click", () => {
if (obj1.id === obj2.id) {
resEle.innerHTML = "IDs are NOT unique!";
} else {
resEle.innerHTML = `
<strong>IDs are unique!</strong><br>
Object 1 ID: ${obj1.id.toString()}<br>
Object 2 ID: ${obj2.id.toString()}
`;
}
});
</script>
</body>
</html>
Alternative Methods
Here are other approaches for generating unique IDs:
// Method 1: Using Date.now() + Math.random()
function generateUniqueId() {
return Date.now().toString(36) + Math.random().toString(36).substr(2);
}
let obj1 = { id: generateUniqueId(), data: "test1" };
let obj2 = { id: generateUniqueId(), data: "test2" };
console.log("Object 1 ID:", obj1.id);
console.log("Object 2 ID:", obj2.id);
console.log("Are IDs unique?", obj1.id !== obj2.id);
// Method 2: Using crypto.randomUUID() (Node.js 14.17+)
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
let obj3 = { id: crypto.randomUUID(), data: "test3" };
console.log("UUID ID:", obj3.id);
}
Object 1 ID: l4d2k7j1g7h Object 2 ID: l4d2k7j3m9n Are IDs unique? true UUID ID: 550e8400-e29b-41d4-a716-446655440000
Comparison of Methods
| Method | Uniqueness | Readability | Use Case |
|---|---|---|---|
Symbol() |
Guaranteed | Low | Object properties, internal tracking |
Date.now() + Math.random() |
Very High | Medium | General purpose IDs |
crypto.randomUUID() |
Guaranteed | High | Standard UUIDs for APIs |
Key Points
-
Symbol()creates truly unique identifiers that cannot be duplicated - Even with the same description, each Symbol is unique
- Symbols are not enumerable in
for...inloops - Use
crypto.randomUUID()for standard UUID format
Output
Conclusion
Use Symbol() for guaranteed unique object IDs. For human-readable IDs, combine Date.now() with Math.random() or use crypto.randomUUID() for standard UUID format.
Advertisements
