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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:03:46+00:00 2026-06-13T08:03:46+00:00

Does object in javascript pass by reference? If yes why this code not working.

  • 0

Does object in javascript pass by reference?
If yes why this code not working.

function change(myObj)
{
 myObj={};
 myObj.a=2;
}
 o={a:1};
change(o);
alert(o.a); //alert 1

but when I do

function change(myObj)
{

 myObj.a=2;
}
 o={a:1};
change(o);
alert(o.a); //alert 2
  • 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-13T08:03:47+00:00Added an answer on June 13, 2026 at 8:03 am

    I’ll explain this thoroughly

    o={a:1};
    first you set global variable o to be a reference of new anonymous object that have attribute variable a with value = 1 name this object {a:1} as ‘1A’

    change(o);
    now you call function change and javascript check of typeof variable o and it’s ‘object’
    actually it’s should be ‘reference that pointed to object’ so the reference of object {a:1} is pass into function change by the way if variable is primitive it will pass only by value

    function change(myObj){
    now function change create variable myObj with typeof ‘undefined’ by default and then change to ‘object’ because it got reference parameter and now the variable myObj is a reference variable that pointed to object ‘1A’ => {a:1} and myObj is visible only in function change
    and global variable o maybe still point to object ‘1A’ => {a:1} if myObj is just a copy of reference to object ‘1A’ => {a:1} by language design

    myObj={};
    now the reference variable myObj is point to new anonymous empty object {} name this object as ‘2B’

    myObj.a=2;
    }

    now you use reference myObj to set object ‘2B’ => {} to have new attribute a with value = 2
    and end the scope of function that mean global can’t see object ‘2B’ => {a:2}

    alert(o.a); //alert 1
    variable still point or may be point back to object {a:1} ,reference o can’t lose it point,
    because object ‘2B’ => {a:2} can’t be seen outside function change
    and will be destroyed by garbage collection because it’s lost the reference
    and object ‘1A’ => {a:1} can’t be destroyed by garbage collection
    because variable o still point at it that why you call o you receive object ‘1A’ => {a:1}

    sorry for my bad grammar but I try my best to make it easy to read.

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

Sidebar

Related Questions

Firefox 3.5 does not allow creating java OBJECT tag with Javascript (document.write)? this technique
Possible Duplicate: JavaScript losing “this” object reference with private/public properties Why does the second
I have a javascript that does this (http is your XMLHttpRequest object) var r
I wanna pass an object from jquery using a selector to a javascript function
In Javascript I'm trying to pass a class member to a jQuery function, but
I have the following situation in JavaScript: <a onclick=handleClick(this, {onSuccess : 'function() { alert(\'test\')
This is my javaScript code. (function() { window.onload = function() { // Creating a
Does anyone have an explanation of how javascript could do this. Somehow this function
var object = {foo: 'bar'}; Does JavaScript have a way to determine that object.foo
Initialising the Foo object does run the method func(), but the value of self.a

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.