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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:23:55+00:00 2026-05-29T20:23:55+00:00

If in javascript object properties are passed by reference why this doesnt work: var

  • 0

If in javascript object properties are passed by reference why this doesnt work:

var myObj={a:1}

function myFun(x){
x=2;
}

myFun(myObj.a);

// value myObj.a is still 1

but on the other hand if you do this:

var myObj={a:1}

function myFun(x){
x.a=2;
}

myFun(myObj);

// this works myObj.a is 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-05-29T20:23:57+00:00Added an answer on May 29, 2026 at 8:23 pm

    Primitive values are passed by value. Objects are passed by reference.

    Object properties are passed by based on their data type.

    Here you are passing an integer – x represents the value 1. Assigning x the value 2 does not reference the original object.

    Let’s say the property you pass in is an array. And the 2nd function I call receives an array and you make changes to that array. Then the changes will persist to the object because the object’s property contains a reference to the array you modified. You didn’t technically modify the object at all… you just modified the array which is referenced in the object. When you pass an object property to a function, it’s not aware that it belongs to an object at all.

    See example, similar to yours:

    var myObj={a:[1]}
    
    function fn1(x){
     x=2; //Overwrites x in this scope to the new primitive 2. 
          //This isn't reflected in myObj because x is not a 
          //reference to myObj.a  it is a reference to the array 
          //that myObj.a contains (the [1]).
    }
    
    function fn2(x){
     x.push(2);
    }
    
    fn1(myObj.a); //myObj.a is [1]
    fn2(myObj.a); //myObj.a is [1,2]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a JavaScript object, built this way: function foo() { this.length = 0;
I have a JavaScript object that looks something like this: var myTextOptions = {
I am having a problem identifying a javascript object function A(x, y){ this.boo =
I have a JavaScript object that I'd like to add some properties to, but
How do I enumerate the properties of a JavaScript object? I actually want to
Possible Duplicate: Can Read-Only Properties be Implemented in Pure JavaScript? I have an Object
Hallo all. I got a javascript object with some propeties let's say function Animal()
I've created a JavaScript object to hold onto a value set by a user
Given an Array Literal inside a JavaScript Object, accessing its own object's properties does
i have a javascript object declared something like: var myObject = {a:{b: c, d:

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.