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 6681871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:38:21+00:00 2026-05-26T04:38:21+00:00

I want to compare each string in an Array with a given string. My

  • 0

I want to compare each string in an Array with a given string. My current implementation is:

function startsWith(element) {
    return element.indexOf(wordToCompare) === 0;
}
addressBook.filter(startsWith);

This simple function works, but only because right now wordToCompare is being set as a global variable, but of course I want to avoid this and pass it as a parameter. My problem is that I am not sure how to define startsWith() so it accepts one extra parameter, because I dont really understand how the default parameters it takes are passed. I’ve tried all the different ways I can think of and none of them work.

If you could also explain how the passed parameters to ‘built in’ callback functions (sorry, I dont know of a better term for these) work that would be great

  • 1 1 Answer
  • 0 Views
  • 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-26T04:38:22+00:00Added an answer on May 26, 2026 at 4:38 am

    Make startsWith accept the word to compare against and return a function which will then be used as filter/callback function:

    function startsWith(wordToCompare) {
        return function(element) {
            return element.indexOf(wordToCompare) === 0;
        }
    }
    
    addressBook.filter(startsWith(wordToCompare));
    

    Another option would be to use Function.prototype.bind [MDN] (only available in browser supporting ECMAScript 5, follow a link for a shim for older browsers) and “fix” the first argument:

    function startsWith(wordToCompare, element) {
        return element.indexOf(wordToCompare) === 0;
    }
    
    addressBook.filter(startsWith.bind(this, wordToCompare));
    

    I dont really understand how the default parameters it takes are passed

    There is nothing special about it. At some point, filter just calls the callback and passes the current element of the array. So it’s a function calling another function, in this case the callback you pass as argument.

    Here is an example of a similar function:

    function filter(array, callback) {
        var result = [];
        for(var i = 0, l = array.length; i < l; i++) {
            if(callback(array[i])) {  // here callback is called with the current element
                result.push(array[i]);
            }
        }
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that I want to compare against an array of strings,
I want to read a String sentence from a Textfield and compare each letter
I want to compare the current value of an in-memory Hibernate entity with the
I want to compare the first few bytes in byte[] with a string. How
I want to compare several strings to each other, and find the ones that
The problem is that I want to compare the characters in a string, one
I want to compare an array of comparables. The simplest way seems the following
I want to compare two Java Map s by a simple hash. Each object
I want to compare two ms-access .mdb files to check that the data they
I want to compare and contrast the various source control systems out there. Any

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.