How do you verify if an array contains a specific value in JavaScript? | A1Lab@FahadM to JavaScriptEnglish •
edit-2
12 days ago

How do you verify if an array contains a specific value in JavaScript?

message-square
0

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'