Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Javascript: Inconsistent behavior with bound function in `some` array function

I'm trying to understand why this bound function is behaving differently when called in these different ways. Here's the example.

var str = 'sss';
var f = str.endsWith.bind(str);

// false?
console.log(['s', 'q', 'r'].some(f))
// true
console.log(['s', 'q', 'r'].some(value => f(value)))
// true
console.log(f('s'))

What am I missing, why isn't the first call returning true?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Array.some() passes 3 arguments to the callback function: the current array element, its index, and the array.

String.endswith() takes an optional second argument, which is used as the length of the string instead of its actual length.

So the first iteration calls

f('s', 0, ['s', 'q', 'q'])

which is equivalent to

"sss".endsWith('s', 0)

If you use 0 as the length of the string, it doesn't end with s, so this returns false.

When you use your function value => f(value), the extra arguments are discarded so the default length is used and the function operates as expected.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!