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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:31:30+00:00 2026-06-01T23:31:30+00:00

I already know how to make this code work, but my question is more

  • 0

I already know how to make this code work, but my question is more about why does it work like this, as well as am I doing stuff right.

The simplest example I can make to showcase my issue is this :

Lets say I have a function that increments the value of an input field by 10 on the press of a button.

var scopeTest = {

    parseValue : function( element, value ) {
        value = parseInt( element.val(), 10 );
        //Why does this not return the value?
        return value;
    },


    incrementValue : function( element, button, value ) {
        button.on('mousedown', function (e) {
            //Execute the parseValue function and get the value
            scopeTest.parseValue( element, value ); 
            //Use the parsed value      
            element.val( value + 10 );    
            e.preventDefault(); 
        });               
    },


    init : function () { 
        var element = $('#test-input'),
            button = $('#test-button'),
            value = ''; 

        this.incrementValue( element, button, value );
    }

};

scopeTest.init();

The above code doesnt work because the parseValue method doesn’t properly return the value var when executed inside the incrementValue method.

To solve it apparently I have to set the scopeTest.parseValue( element, value ); parameter to the value variable like this:

value = scopeTest.parseValue( element, value );

Than the code works.

But my question is why? Why is this extra variable assignment step necessary, why the return statement is not enough? Also I am doing everything right with my functions/methods, or is this just the way JavaScript works?

Working example here => http://jsfiddle.net/Husar/zfh9Q/11/

  • 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-01T23:31:32+00:00Added an answer on June 1, 2026 at 11:31 pm

    This is mostly a scope issue. The pass-by-* issue is strange to discuss because the sender variable and the called functions variable have the same name. I’ll try anyway.

    A variable has a scope in which it is visible. You can see it as a place to store something in. This scope is defined by the location of your function. Meaning where it is in your source code (in the global scope or inside a function scope). It is defined when you write the source code not how you call functions afterwards.

    Scopes can nest. In your example there are four scopes. The global scope and each function has a scope. The scopes of your functions all have the global scope as a parent scope. Parent scope means that whenever you try to access a name/variable it is searched first in the function scope and if it isn’t found the search proceeds to the parent scope until the name/variable is found or the global scope has been reached (in that case you get an error that it can’t be found).

    It is allowed to define the same name multiple times. I think that is the source of your confusion. The name “value” for your eyes is always the same but it exists three times in your script. Each function has defined it: parseValue and incrementValue as parameter and init as local var. This effect is called shadowing. It means that all variables with name ‘value’ are always there but if you lookup the name one is found earlier thus making the other invisible/shadowed.

    In this case “value” is treated similar in all three functions because the scope of a local var and a parameter is the same. It means that as soon as you enter one of the methods you enter the function scope. By entering the scope the name “value” is added to the scope chain and will be found first while executing the function. And the opposite is true. If the function scope is left the “value” is removed from the scope chain and gets invisible and discarded.

    It is very confusing here because you call a function that takes a parameter “value” with something that has the name “value” and still they mean different things. Being different there is a need to pass the value from one “value” to the other. What happens is that the value of the outer “value” is copied to the inner “value”. That what is meant with pass-by-value. The value being copied can be a reference to an object which is what most people make believe it is pass-by-reference. I’m sorry if that sounds confusing but there is too much value naming in here.

    The value is copied from the outer function to the called function and lives therefor only inside the called function. If the function ends every change you did to it will be discarded. The only possibility is the return your “side effect”. It means your work will be copied back to a variable shortly before the function gets discarded

    To other alternative is indeed leaving of parameters and work with the scope chain, e.g. the global scope. But I strongly advize you not to do that. It seems to be easy to use but it produces a lot of subtle errors which will make your life much harder. The best thing to do is to make sure variables have the most narrow scope (where they are used) and pass the values per function parameters and return values.

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

Sidebar

Related Questions

I think I already know the answer to this but thought I would ask
I guess you already know the question ;) Does the performance wizard only exist
I think i already know the answer to this, but i cannot find anything
This question may have been asked already - but unfortunately, I could not find
this may seem like a overly complicated question, but it has me driving me
I already asked this question, but in a way that confused people. So what
I know this flavor of question has been asked multiple times, but I've spent
I already know that most implementations use a single thread, but is there anything
This is probably a noob question that I will get slated for but here
I've already found this question on SO (which is exactly the same problem I'm

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.