How do you verify if an array contains a specific value in JavaScript?
markdown Use **array.includes(value)** for primitive values (strings, numbers, booleans), or **array.some(item => …)`** when checking for objects matching a specific condition.
Primitives vs. Objects in Arrays
const roles = ["admin", "moderator", "member"];
// 1. Primitive values: use .includes()
console.log(roles.includes("moderator")); // true
// 2. Array of objects: use .some()
const users = [{ id: 1, name: "Fahad" }, { id: 2, name: "Zeeshan" }];
const hasZeeshan = users.some(user => user.name === "Zeeshan"); // true'
You must log in or register to comment.
