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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:46:02+00:00 2026-06-06T21:46:02+00:00

Lets say I have created an object and its properties in JavaScript as follows-

  • 0

Lets say I have created an object and its properties in JavaScript as follows-

   var obj = {};
   obj['bar'] = 123;
   obj['bar']['foo'] = new Array();
   obj['bar']['xyz'] = new Array();

After this, I push elements into the two arrays.
If I then write

   delete obj['bar'];

Will the two arrays also be deleted ?

  • 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-06T21:46:05+00:00Added an answer on June 6, 2026 at 9:46 pm

    Will the two arrays also be deleted ?

    They’ll be eligible for garbage collection, assuming nothing else has any references to them. And nothing will, based on that code. When and whether they’re actually cleaned up is up to the implementation.

    But note that they’re eligible for GC even before you remove bar, because your code is doing something quite odd. See the comments:

    // Creates a blank object, so far so good.
    var obj = {};
    
    // Sets the `bar` property to the number 123.
    obj['bar'] = 123;
    
    // Retrieves the value of `bar` (the primitive number 123) and
    // *converts* it into a `Number` instance (object), then
    // creates a property on that object called `foo`, assigning a
    // blank array to it. Because the Number object is never stored
    // anywhere, both it and the array are IMMEDIATELY eligible for
    // garbage collection. The value of `obj['foo']` is unaffected,
    // it remains a primitive number.
    obj['bar']['foo'] = new Array();
    
    // The same happens again, a temporary Number instance and
    // array are created, but both are IMMEDIATELY eligible for
    // garbage collection; the value of `obj['bar']` is unaffected.
    obj['bar']['xyz'] = new Array();
    

    So you didn’t even have to remove bar, the arrays were instantly eligible for garbage collection. This happens because in JavaScript, numbers are primitives that can be automatically promoted to Number objects — but that doesn’t affect the value of the property the primitive number is assigned to. So:

    var obj = {};
    obj['bar'] = 123;
    obj['bar']['foo'] = [];         // [] is a better way to write new Array()
    console.log(obj['bar']['foo']); // "undefined"
    

    If you change your obj['bar'] = line to:

    obj['bar'] = {};
    

    or

    obj['bar'] = []; // Arrays are objects
    

    …then the foo and xyz properties will not instantly disappear, and the arrays will stick around until bar is cleaned up.

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

Sidebar

Related Questions

Let's say I have created two objects from class foo and now want to
Lets say I have this code: <?php class hello { var $greeting = hello;
Lets say I have a class like this: Class User { var $id var
Lets say i have created a small game with XNA. Now i can change
Lets say I have a eventhandler like so: public SomethingHappended_Handler(object sender, EventArgs e) {
Lets say I have a FB app I've created, and for whatever reason the
Let's say I have created a program in C/C++ and have a source code.
Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)
Lets say I have a recursive function that creates lists within lists. It return
So lets say I have TABLE1 and TABLE2. CREATE TABLE TABLE1 ( first_id int

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.