Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6003355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:06:34+00:00 2026-05-23T01:06:34+00:00

Given an array: var arr = [1,,2,5,6,,4,5,6,,]; Count how many empty values is has:

  • 0

Given an array:

var arr = [1,,2,5,6,,4,5,6,,];

Count how many empty values is has: (length – length after removing the empty values)

var empties = arr.length - arr.filter(function(x){ return true }).length;

// return 3

or something like this

arr.empties = arr.length;
arr.forEach(function(x){ arr.empties--  });

// arr.empties returns 3

Is this the best way or am I missing something?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:06:35+00:00Added an answer on May 23, 2026 at 1:06 am

    Based on your comments to another answer, it looks like you’re after the shortest method. Well, you might want to consider a variation of your own example:

    var empties = arr.length - arr.filter(String).length;
    

    All you’re doing is passing a native function rather than an anonymous function, saving a few precious bytes. Any native constructor or function will do, as long as it doesn’t return a boolean.


    You need to be more specific about what you would consider the ‘best way’. For instance, some methods will give better performance than others, some are more concise and some have better compatibility.

    The solutions you mention in the post require browsers to be compatible with the ECMAScript 5th Edition specification, so they won’t work in some older browsers (read: IE8 and lower).

    The “best” all-round approach is a simple loop. It’s not as concise as your methods, but it will no doubt be the fastest and most compatible:

    var arr = [1,,2,5,6,,4,5,6,,], count = 0, i = arr.length;
    
    while (i--) {
        if (typeof arr[i] === "undefined")
            count++;
    }
    

    This makes use of loop optimisations (using while and decrementing is faster than for).

    Another approach would be to sort the array so that undefined items are all at the end and use a loop to iterate backwards:

    var arr = [1,,2,5,6,,4,5,6,,], count = 0;
    arr.sort();
    while (typeof arr.pop() === "undefined") count++;
    
    alert(count); 
    //-> 3
    

    This approach would modify the original array and remove those items which may not be what you want. However, it may be much faster on very large arrays.

    Performance test suite
    http://jsperf.com/count-undefined-array-elements

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an array of items, each of which has a value and cost ,
Given following Ruby statements: (Read input and store each word in array removing spaces
I have a string. I need to replace all instances of a given array
Given an array of n Objects, let's say it is an array of strings
Given an array of characters which forms a sentence of words, give an efficient
Given an array of integers, what is the simplest way to iterate over it
Given an array like {one two, three four five}, how'd you calculate the total
How do you do it? Given a byte array: byte[] foo = new byte[4096];
Given a date/time as an array of (year, month, day, hour, minute, second), how
Given this piece of code: (void)someFunction(void) { int array[] = {1,2,3,4,5,6,7,8,9,10}; } Where are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.