Menu
Coddy logo textTech

Membership Testing

Part of the Fundamentals section of Coddy's JavaScript journey. Lesson 70 of 77.

You can check whether an element is in an array or not using the indexOf() method:

const arr = ["a", "b", "c"];
console.log(arr.indexOf("b")); // Output: 1
console.log(arr.indexOf("d")); // Output: -1

If the element is not found, indexOf() returns -1.

You can also use the includes() method that was learned before!

challenge icon

Challenge

Easy

Create a program that receives two arrays of numbers as input (given) and prints a new array of all elements that are in the first array but NOT in the second array.

Try it yourself

let arr1 = inp[0].split(", ").map(Number);
let arr2 = inp[1].split(", ").map(Number);
// Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online JavaScript compiler