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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:27:07+00:00 2026-05-27T13:27:07+00:00

This is how JsFromHell defines a function to find sum of a numeric array

  • 0

This is how JsFromHell defines a function to find sum of a numeric array (http://jsfromhell.com/array/sum)

sum = function(o){
    for(var s = 0, i = o.length; i; s += o[--i]);
    return s;
};
//sum([1, 2, 3, 4, 5, 6, 7, 8, 9])

Can someone explain what’s happening within second part of the for loop? What’s the meaning of “i;”? It appears like its same as i >= 0. But that returns a NaN.

  • 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-27T13:27:07+00:00Added an answer on May 27, 2026 at 1:27 pm

    JavaScript has various ways of coercing non-Boolean values to true or false. One of them has to do with numbers: zero is false, any other number is true.

    For strings, an empty string is false, others are true. The null value is coerced to false, as is the somewhat zen-like “undefined” non-value.

    You could write that code:

    for (var s = 0, i = o.length; i > 0; s += o[--i]);
    

    and it might be even more efficient. (Or it might not be; it’s the kind of micro-optimization that only library maintainers should worry about, since next week the browser vendors may rev their interpreters and flip the situation on its head.)

    Finally, if you’re getting a NaN, it means that you don’t really have an array of numbers. If there’s a single thing in the array that can’t cleanly be converted to a numeric value in the third part of the “for” loop, you’ll get a NaN result. edit — oh wait, I see; you tried i >= 0 and not i > 0. That means the loop will try to access o[-1] which is undefined. That’ll give you a NaN when you try to convert it to a number.

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

Sidebar

Related Questions

This onclick function: VVi.$row['id']..onclick = function() { alert('1'); var ResultI = '.$row['id'].'; location.href='visuals.php?ResultI=' +
This seems 101-level, but I can't find an answer! (instead I find links to
This is probably basic stuff, but I can't find the clear answer anywhere.. Lets
This is a bit of a long shot, but if anyone can figure it
This most be the second most simple rollover effect, still I don't find any
This link describes a function on a Money object for adding funds together of
This is mostly a philosophical question about the best way to interpret the HTTP
this is my code to turn a byte[] array into an image unsafe {
This is the blurb accompanying the Audio Services function AudioServicesSetProperty. Its a bit over
This walkthrough says you can create a WPF datagrid in one line but doesn't

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.