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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:55:19+00:00 2026-05-11T02:55:19+00:00

So I was playing around the other day just to see exactly how mass

  • 0

So I was playing around the other day just to see exactly how mass assignment works in JavaScript.

First I tried this example in the console:

a = b = {}; a.foo = 'bar'; console.log(b.foo); 

The result was ‘bar’ being displayed in an alert. That is fair enough, a and b are really just aliases to the same object. Then I thought, how could I make this example simpler.

a = b = 'foo'; a = 'bar'; console.log(b); 

That is pretty much the same thing, isn’t it? Well this time, it returns foo not bar as I would expect from the behaviour of the first example.

Why does this happen?

N.B. This example could be simplified even more with the following code:

a = {}; b = a; a.foo = 'bar'; console.log(b.foo);  a = 'foo'; b = a; a = 'bar'; console.log(b);

(I suspect that JavaScript treats primitives such as strings and integers differently to hashes. Hashes return a pointer while ‘core’ primitives return a copy of themselves)

  • 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. 2026-05-11T02:55:19+00:00Added an answer on May 11, 2026 at 2:55 am

    In the first example, you are setting a property of an existing object. In the second example, you are assigning a brand new object.

    a = b = {}; 

    a and b are now pointers to the same object. So when you do:

    a.foo = 'bar'; 

    It sets b.foo as well since a and b point to the same object.

    However!

    If you do this instead:

    a = 'bar'; 

    you are saying that a points to a different object now. This has no effect on what a pointed to before.

    In JavaScript, assigning a variable and assigning a property are 2 different operations. It’s best to think of variables as pointers to objects, and when you assign directly to a variable, you are not modifying any objects, merely repointing your variable to a different object.

    But assigning a property, like a.foo, will modify the object that a points to. This, of course, also modifies all other references that point to this object simply because they all point to the same object.

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

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer like others have mentioned, you could store it as a… May 11, 2026 at 12:46 pm
  • added an answer This really depends on what the requirements are of the… May 11, 2026 at 12:46 pm
  • added an answer With MikTeX, pdflatex has this command-line option: -interaction=MODE Set the… May 11, 2026 at 12:46 pm

Related Questions

So I was playing around a little more with attributes in .NET, and realized
So I was listening to the latest Stackoverflow podcast ( episode 19 ), and
So I was writing some code today that basically looks like this: string returnString
So I was wondering if there are any major differences between the various implementations
Hi im learning LISP and well, all day i program php for a living,
I have a script to extract certain data from a much bigger table, with
I make an AJAX request like so using JQuery: $.ajax({ type: GET, url: getvideo.php,
When using System.Windows.Forms.ShowDialog(IWin32Window) , should I be able to pass in an IWin32Window representing

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.