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

  • SEARCH
  • Home
  • 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 7542597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:12:35+00:00 2026-05-30T08:12:35+00:00

I was killing time reading the underscore.string functions, when I found this weird shorthand:

  • 0

I was killing time reading the underscore.string functions, when I found this weird shorthand:

function count (str, substr) {
  var count = 0, index;
  for (var i = 0; i < str.length;) {
    index = str.indexOf(substr, i);
    index >= 0 && count++; //what is this line doing?
    i = i + (index >= 0 ? index : 0) + substr.length;
  }
  return count;
}

Legal: Think twice before using the function above without giving credit to underscore.string


I put the line alone here, so you don’t waste time finding it:

index >= 0 && count++;

I have never seen anything similar to that. I am clueless in what is doing.

  • 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-30T08:12:37+00:00Added an answer on May 30, 2026 at 8:12 am
    index >= 0 && count++;
    

    First part: index >= 0

    returns true if index has a value that is greater than or equal to 0.

    Second part: a && b

    most C-style languages shortcut the boolean || and && operators.

    For an || operation, you only need to know that the first operand is true and the entire operation will return true.

    For an && operation, you only need to know that the first operand is false and the entire operation will return false.

    Third Part: count++

    count++ is equivalent to count += 1 is equivalent to count = count + 1

    All together now

    If the first operand (index >= 0) of the line evaluates as true, the second operand (count++) will evaluate, so it’s equivalent to:

    if (index >= 0) {
      count = count + 1;
    }
    

    JavaScript nuances

    JavaScript is different from other C-style languages in that it has the concept of truthy and falsey values. If a value evaluates to false, 0, NaN, "", null, or undefined, it is falsey; all other values are truthy.

    || and && operators in JavaScript don’t return boolean values, they return the last executed operand.

    2 || 1 will return 2 because the first operand returned a truthy value, true or anything else will always return true, so no more of the operation needs to execute. Alternatively, null && 100 will return null because the first operand returned a falsey value.

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

Sidebar

Related Questions

This is a question that arose mostly of pure curiosity (and killing some time).
Simple question but this is killing my time. Any simple solution to add 30
Killing the processs while obtaining this information would be fine.
I have a function that is responsible for killing a child process when the
This may not really sound hard to do but it is currently killing me.
A colleague once said that God is killing a kitten every time I write
Don't know whats exactly going on, but it's definitely killing my time for nothing.
This is my first time trying to use both ARC and Core Data. I
This is seriously killing me. I'm trying to convert a Unix timestamp (1236268800, which
I've been killing myself all day for this one bug. I can't tell you

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.