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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:47:05+00:00 2026-05-26T15:47:05+00:00

If I recall correctly from Crockford’s Javascript: The Good Parts, he is not in

  • 0

If I recall correctly from Crockford’s “Javascript: The Good Parts”, he is not in favor of using the ++ or — operators, but I also tend to recall he doesn’t provide an especially strong argument against them.

Below is a use of these operators that I’ve found helpful in keeping my code as concise as possible particularly when dealing with functions/methods that return -1 when 0 is the first possible valid return value (along with positive integers). I’ll be interested in other atypical uses of ++ and/or — that make a strong argument in favor of using these operators when they make sense.

I do not consider this a duplicate of Why avoid increment ("++") and decrement ("–") operators in JavaScript? but rather it’s corollary: when to not avoid them, but rather use them to your advantage. Of course I could be mistaken and there could be some reason I’m not thinking of as to why the following is fraught with danger despite seeming to me to be elegant — if I’m missing something sub-optimal about the following, I’d like to know that too

var substrIdx = str.indexOf(substr);
if (++substrIdx) {
  doSomething(--substrIdx);
}
  • 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-26T15:47:05+00:00Added an answer on May 26, 2026 at 3:47 pm

    If I’m missing something sub-optimal about the following, I’d like to know that too

    var substrIdx = str.indexOf(substr);
    if (++substrIdx) {
      doSomething(--substrIdx);
    }
    

    It’s sub-optimal. It is confusing to humans, requires shipping more code over the browser, and requires the interpreter to do more work at runtime.

    Someone trying to figure out what it is, has to reason about the state of substrIdx at different points in the code — "it obeys the contract of indexOf here, but there it’s one greater than the contract of indexOf, and after the if it doesn’t have a clear relationship to substr.".

    It’s also longer than

    var substrIdx = str.indexOf(substr);
    if (substrIdx>=0) {
      doSomething(substrIdx);
    }
    

    is harder for minifiers to minify because substrIdx is no longer a single assignment var.
    A good minifier will turn the latter into something like

    var substrIdx = str.indexOf(substr);
    substrIdx<0||doSomething(substrIdx);
    

    but fewer will do the job with the unnecessary assignment. They compare pretty well under closure compiler:

    var a=str.indexOf(substr);++a&&doSomething(--a)
    

    vs

    var a=str.indexOf(substr);a>=0&&doSomething(a)
    

    because closure compiler misses the opportunity to turn a>=0&& into a<0|| but the increment and decrement is still longer.

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

Sidebar

Related Questions

If I recall correctly, there where at least to desktop programas from sun which
If I recall correctly that when I used yield inside using SqlConnection blocks I
In Office 2003 if I recall correctly you had a record macro option. In
I recall reading somewhere that using references to smart pointers can cause memory corruption.
I recall when I first read Pragmatic Programmer that they suggested using scripting languages
If I recall correctly, tail recursive functions always have an easy non-recursive equivalent. Since
I recall hearing or reading somewhere that SELECT * from TABLE where date >=
I have a Google Appengine app requesting pages from another server using urllib2 POSTs.
I need to create a cv::Mat from a buffer, but I don't want the
From what I can recall, ActiveX components are executable files that conform to some

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.