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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:51:52+00:00 2026-06-12T18:51:52+00:00

I was looking at some JavaScript recently and came across the following routine. Can

  • 0

I was looking at some JavaScript recently and came across the following routine. Can anyone explain to me how it works?

function groupConsecutive(numbers, successor) {
successor || ( successor = function(n) { return n + 1; });
var groups = [];
return _.each(numbers, function(number) {
    if (groups.length === 0) {
        groups.push([number]);
    } else { 
        successor.call(this, _.last(_.last(groups))) === number ? _.last(groups).push(number) : groups.push([number]);
    }
}, this), groups;
}

Specifically I’m trying to understand the successor || { bit. If I use “use strict” here it throws an “Expected an assignment or function call and instead saw an expression” error, and so I believe it should because successor is never defined prior to the function being called. So to my way of thinking whenever the routine starts up, the successor function is undefined. This routine is using the underscore.js library.

  • 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-06-12T18:51:54+00:00Added an answer on June 12, 2026 at 6:51 pm

    The expression uses a side effect of the || operator. As the operator uses short circuit evaluation, the second operand is only evaluated if the first evaluates to false.

    So, this line:

    successor || ( successor = function(n) { return n + 1; });
    

    does the same thing as:

    if (!successor) {
      successor = function(n) { return n + 1; };
    }
    

    The strict mode does right in throwing an error, as the line contains an expression that is evaluated but then the result is discarded. Normally that means that you forgot to do something with the result.

    You can use the || operator in strict mode if you use this form:

    successor = successor || function(n) { return n + 1; };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for some Javascript jedi-master to assist in creating a function that adds
I am looking for some javascript or php code where it allows anyone to
when I'm looking for some sites Javascript code, I see this function hrefLeftMenu() {
I am looking at some javascript code and it has this in a function:
I've recently started messing about with Javascript and can see some nice applications for
I am looking for some native JavaScript, or jQuery plugin, that meets the following
I'm an experienced Java programmer but am looking at some JavaScript/HTML5 stuff for the
To debug some JavaScript code, I am looking for JavaScript code (preferably just js,
I'm looking for the best way to escape some Javascript text in PHP, and
I am looking for some simple library for javascript to manage animations. I need

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.