Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
How to get length of updated array using property in JS?

I created a variable to hold the total number of elements in the array using myArray.length. Then I use myArray.push() to add an element to the array.

the original array has 3 elements, so the .length returns 3, which is correct. But when I added an element to the array and I console.log(the Var which contains the .length property ), it just does not work. the Var still shows 3 for the array. However, if I use xxx.length, the number is correct now.

var collection = function() {
  //two properties
  var items = ["basketball", "football", "soccer"];
  var number = items.length;

  console.log(items);
  console.log("the number of the items in the array is " + number);

  // two private methods
  var addItem = function(newItem) {
    items.push(newItem);
    console.log(items);
  };

  var showNumber = function() {
    console.log(number) //this one returns 3, which is not correct
    console.log("the number of the items in the array is " + items.length);
  }; // this one returns 4, which is correct



  // two public methods
  return {
    getItem: addItem,
    getNumber: showNumber,
  };
}();


collection.getItem("pingpong");
collection.getNumber();

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!