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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:43:20+00:00 2026-05-25T00:43:20+00:00

i wanted to count steps of an infinite recursive function in node.js (v0.4.10), using

  • 0

i wanted to count steps of an infinite recursive function in node.js (v0.4.10), using a global value incrementation. however, the count always pretends to be zero

> c = 0
> (function f() { c++; console.log(c); f() })();
1
2
...
18648
RangeError: Maximum call stack size exceeded
> c 
0

logging the c value to console from within the function shows that the value really is incremented, but somehow it is finally reset after that stack accident. even if global.c is used instead of c.

is it the correct bahavior? what is happening here? e.g. in chromium (v14), c holds the final count as expected.

UPDATE

it turns out the above is valid in interactive mode only. when the code is executed from file, and the function in enclosed in try-catch block (to prevent premature exit), the c value is correct..

c = 0;
try {
  (function f() { c++; f() })();
} catch(e) {};
console.log(c);

however, there is stil a difference between interactive node.js and chromium javascript console, where the value survives the unhandled exception

  • 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-25T00:43:20+00:00Added an answer on May 25, 2026 at 12:43 am

    Each interactive mode behaves a little bit different than standard execution of JavaScript file (Isaac Schlueter wrote somewhere: “I’ve never seen a REPL that didn’t have at least a tiny bit of magic sprinkled on it“). The interactive mode must evaluate the text, which usually runs the code in a different context.

    In case of Node.JS each command is executed in its own context, then REPL takes care to back to us with the results. If we fail in the command due to some errors, the values of our variables remain unchanged (because changed variables ‘die’ in the context of execution). When we run JavaScript file standard way we don’t have this separate context of execution and we directly change our variables.

    You can change your example to manually create an error and play wit it:

    c=0;
    (function f(x){ c++; if(x) f(x-1); else throw new Error();  })(10);
    

    The final value of c will be 0 in REPL as long as the code executes with an error. Because of the error REPL can’t update global value of c to the value calculated in the execution context of our test function.

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

Sidebar

Related Questions

I wanted to use count() function in ColdFusion Query object. Here is my code
I wanted to write a regex to count the number of spaces/tabs/newline in a
I wanted to generate one fix view using interface builder, but the size of
I want to count how many cells that have a value given that a
I just wanted to know: will the retain count of an object be incremented
I have a UIView which retain count is three, and I just wanted to
I wanted to use a method as part of my model to count all
I am using jquery to get a count of children elements within a parent
I want to use xpath to count occurances of certain node in xml document.
I am using libsvm to predict sentiment. I wanted to know what format the

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.