JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – filter array method based on the element size

JavaScript Quiz – filter array method based on the element size

Array Filter Method –

Javascript filter method is used to filter an array based on the condition provided to it. Once the array filter method filters an array it returns a new array.

If no element pass the condition then it return an javascript empty array.

It’s syntax look like –

let newArray = array.filter(callback(element[, index, [array]])[, thisArg])

Note : Original array not going to change as you get a new filtered array.

For detail information about javascript array filter method click here.

JavaScript Question

In this javascript quiz code, you need to find the output of filterArray length after the main filtering a main array based on condition provided to it.

In the first step, we declare a variable array using the const keyword and assign an array to it which contains 3 elements.

const array = [“JS”, “Startup”, “Like”];

Now, we verify the length of the array and and it showing the 3.

console.log(array.length); // output => 3

In the next step, we declare an another variable filterArray and assign main array to it with filter method.

const filterArray = array.filter(el => { … });

And we also pass a condition which checks whether the element size is greater than 3 and if return their result to filter method;

return el.size > 3;

Finally, we check the console log of the length of filterArray variable. Any guess what should be the correct answer.

Answer – 0

Click To Run Code

JavaScript Question

Now, you know the answer is 0. But why, it should be 2 as “like” & “jsstartup” size if more than 3. Let me explain it.

Just think twice, does the javascript string really have a property called size. Actually, string have a length property to get the length of the string.

Do we really have size property in string ?

So, when you use size property with the string it returns undefined. So, every time you are comparing with undefined and it returns false.

el.size = undefined

undefined > 3 = false

As, every condition fails due which filterArray have an empty array which you check also by console log.

const filterArray = [];

Note: Some property gives the same feeling as the existing property but it might not be in your technology.

At last, That’s why we get an answer “0“.

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 – can in operator check value or index in an array
  2. JS Quiz – can really instanceof operator work on string literal
  3. JavaScript Quiz – Function can be used as class using new operator
  4. JS Quiz – Reverse array element but in the same position

Conclusion

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
0 Shares
Share via
Copy link