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: -1If the element is not found, indexOf() returns -1.
You can also use the
includes()method that was learned before!
Challenge
EasyCreate 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 belowThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False9Functions
Declare a FunctionArgumentsReturnRecap - Sigma FunctionFunction ExpressionDefault ParametersArrow FunctionsRecap - Validation Function12Arrays Part 2
Iterating Over ArraysThe forEach Methodfor...of LoopRecap - P CounterArray SlicingArray Methods Part 3Array Methods Part 4Membership TestingPractice on your own: Online JavaScript compiler