JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – includes method can really remove duplicates with forEach

JavaScript Quiz – includes method can really remove duplicates with forEach

Remove Duplicates –

In javascript array, different types of value or multiple value can be store stored. Even the same value can be stored multiple time. So, we need to remove duplicates from array.

Array includes method –

Javascript array includes method check whether the element exist in array and then return true on successful match otherwise return false.

It’s syntax look like –

arr.includes(valueToFind[, fromIndex])

For more detail about array includes method click here.

Array forEach Method –

Javascript array forEach method is used to iterate an array and it calls a function which run once for each element in a array.

It’s syntax looks like –

array.forEach(function(currentValue, index, arr), thisValue)

For more information related to array forEach method click here.

JavaScript Question

In this javascript quiz code, you need to find out that includes method can really remove duplicates from array with forEach method.

Firstly, we declare a variable array using let keyword and assign an javascript array to it which contain only number elements.

let array = [2, 5, 2, 1, 4, 1, 3];

In the second step, create another variable newArray using let and assign a blank array to it.

let newArr = [];

Now in the next step, we use forEach method to iterate an array variable.

array.forEach((el, i, arr){ … });

Inside the forEach passing a condition also.

if(!(newArr.includes(el))) { newArr.push(el); }

Finally, any idea what should be the last line which is console log of newArr will print.

Answer – [2, 5, 1, 4, 3]

Click To Run Code


Now, you the answer is [2, 5, 1, 4, 3]. But how, let me explain.

The condition which is in the forEach method places an important part. As it check whether the element in newArr variable or not.

If it return false then not logical operator ! convert it to true, means element not in the newArr then we push that element to newArr variable.

But if it return true the it converted to false, so element not added to newArr.

That way, we restrict the duplicate element from pushing into newArr variable.

At last, That’s why we get an answer “[2, 5, 1, 4, 3]“.

Let me know in the comment section, any alternative of it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – string split method really a case sensitive
  2. JS Quiz – does typeof of Symbol really give a string
  3. JavaScript Quiz – can setTimeout in the for loop print values
  4. JS Quiz – can reduceRight method really sum an array elements


To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
Share via
Copy link