Remove all characters of first string from second JavaScript


Let’s say, we have two strings that contains characters in no specific order. We are required to write a function that takes in these two strings and returns a modified version of the second string in which all the characters that were present in the first string are omitted.

Following are our strings −

const first = "hello world";
const second = "hey there";

Following is our function to remove all characters of first string from second −

const removeAll = (first, second) => {
   const newArr = second.split("").filter(el => {
      return !first.includes(el);
   });
   return newArr.join("");
};

Let's write the code for this function −

Example

const first = "hello world";
const second = "hey there";
const removeAll = (first, second) => {
   const newArr = second.split("").filter(el => {
      return !first.includes(el);
   });
   return newArr.join("");
};
console.log(removeAll(first, second));

Output

The output in the console will be −

yt

Updated on: 31-Aug-2020

245 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements