Hi there,

I was reading a book on the plane and something interesting came to my mind that needs an answer. The length property of objects is a read/write value. Let's say, you've got an array that has 6 elements in it and you change its length to 10 as shown below.

var array = ["hello", "world", "daniweb"];

alert(array.length);   // this will give you three

array.length = 10;  // Here is the line that changes the value of length

alert(array.length);  // this will give 10

Why would someone change the value of length property?

what is the advantage of doing that?

Recommended Answers

All 9 Replies

Interesting point :)

I tested you code and added some more lines

var array = ["hello", "world", "daniweb"];

alert(array.length);   // this will give you three
console.log(array); // ["hello", "world", "daniweb"]

array.length = 10;  // Here is the line that changes the value of length
alert(array.length);  // this will give 10
console.log(array); // ["hello", "world", "daniweb", undefined, undefined, undefined, undefined, undefined, undefined, undefined]

array.length = 2;  // Here is the line that changes the value of length
alert(array.length);  // this will give 2
console.log(array); // ["hello", "world"]

I viewed the output using Firebug

Just by changing the length manually, it add or remove elements

Then again I added following to the bottom of the above code snippet

array.length = 3;  // Here is the line that changes the value of length
alert(array.length);  // this will give 3
console.log(array); // ["hello", "world", undefined]

The initial "daniweb" element is now missing :) :) :)

I was wondering why some programmer would change the length of an array from 10, let's say, to 2 elements.

P.S when would you use it? for what purpose?

Hi, this feature could be use to discart some unwanted objects, after some filtering perhaps.

But I think the point is that JS is a very dynamic language, we can change almost anything at anytime.

In example, here's a strange thing to do, but it works:

<div id="test"></div>

<script type='text/javascript'>

alert(document.getElementById("test"));

document.getElementById = function(id) {
    alert("I don't want to find " + id);
    return undefined;
}

alert(document.getElementById("test"));

</script>

if you know you are removing eight of the ten elements, and you know that it has to work on computers with limited memory, perhaps.

Also, you may need to read about dynamic array and its purposes. I don't think JavaScript has an explicity dynamic array data type (such as ArrayList in Java). Then you may understand better about when and why you sometimes want to change your array size...

Why would someone change the value of length property?

what is the advantage of doing that?

there are some good practical uses of it, but you haven't come to the point of needing them.

there are some good practical uses of it, but you haven't come to the point of needing them.

What are they? and why is the length function not having brackets? It is a function and functions have brackets.

Not really, it's a property of the Object. Yet internally, it may, or meay not be a function.

Anyway, in more recent javascript you can write functions in a manner of object properties. For instance:
(I've recently posted this snip)

thisDay=
/*b.b. Troy III p.a.e*/{
    get Date(){return new Date().toLocaleDateString()},
    get Time(){return new Date().toLocaleTimeString()}
   }

With it you can call thisDay.Time same as in Array.lenth without brackets!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.