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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:23:18+00:00 2026-05-13T11:23:18+00:00

I was reviewing some today, when I encountered the following convention : TestParam(1); function

  • 0

I was reviewing some today, when I encountered the following convention :

TestParam(1);

function TestParam(p){
    var p = p + 1;
    alert(p);   // alerts '2'
}

Now, obviously, the developer didn’t mean to delcare ‘p’ within the function, instead maybe meaning:

    p = p + 1;

But the code still worked, i.e. the value alerted was “2”. So it got me to thinking. What would happen in the following scenario:

var a = 1;
TestParam(a);
alert(a);  // alerts "1"

function TestParam(p){
    var p = p + 1;
    alert(p);   // alerts '2'
}

Again the alerts were as I suspected (as intimated in the comments above). So then I was curious as to what would happen if I used an object:

var a = { b: 1 };
TestParam(a);
alert(a.b);  //alerts 1

function TestParam(p) {
    var p = {b:p.b + 1};
    alert(p.b); //alerts 2
}

So, in this instance, JavaScript has ‘remembered’ the variable a, even though when it is passed to TestParam as p, p is redeclared.

Now if I were to have done the following within the function, then both alerts would have been “2”

    p.b++;
    //var p = {b:p.b + 1};

I.e. it would have modified the member b of the original object. I get this. Its the previous scenario that baffles me!

I realise this is quite a hypothetical question that is unlikely to carry much real-world usefulness, but it still made me quite curious, as to what is going on in the background, and how exactly JavaScript is scoping and referencing these variables.

Any thoughts?

  • 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-13T11:23:18+00:00Added an answer on May 13, 2026 at 11:23 am

    Variables are scoped to the enclosing function in JavaScript. And you can declare them as many times as you like.

    Objects are not tied to variables; many variables can refer to the same object. (This is sometimes called aliasing.) This is what’s happening in your last example. JavaScript has not “remembered” the variable a at all; rather, p and a refer to the same object, so when it is changed via p, you can see those changes via a.

    Here’s an example that might make more sense to you. Sometimes a person has more than one identity.

    var Clark = new Person;
    var Superman = Clark;  // Maybe not everybody needs to know these two are
                           // the same person, but they are.
    Clark.eyes = "brown";
    alert(Superman.eyes);  // should be brown, right?
    

    Knowing the rules of the language you’re using has tremendous “real-world usefulness”. Not understanding these two rules is the source of a lot of confusion and bugs.

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

Sidebar

Related Questions

I was just reviewing some old code and found the following (inside foo.asp): Const
I was reviewing some SQL queries and I saw a select statement that looked
I'm reviewing some code for a friend and say that he was using a
When reviewing our codebase, I found an inheritance structure that resembles the following pattern:
I recall reviewing someone else's PHP code once and he had a function or
While cleaning some code today written by someone else, I changed the access modifier
Some I'm reviewing some code within my team's code base, where we traverse over
I am just reviewing some code I wrote to communicate with the serial port
I'm reviewing some code and feel suspicious of the technique being used. In a
Whilst reviewing some Qt C++ code I came across this: class Foo { Q_OBJECT

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.