Array length

node v8.17.0
version: 2.0.0
endpointsharetweet
//* 1. Array itself is also an object. However, adding custom properties to an array doesn't affect its length property. function one() { // Example of using array for pagination feature: const arr = [1, 2, 3, 4]; arr['total'] = 100; arr['pageIndex'] = 1; arr['pageSize'] = 15; console.log(`arr as Array: ${arr}`); console.log(`arr.length=${arr.length}`); console.log('arr as Object:'); for (let i of Object.keys(arr)) { console.log(`${i}: ${arr[i]}`); } } one(); //*/ /* 2. Array length reflects the biggest index, not the number of elements. function two() { const arr = [0, 1, 2, 3]; arr[8] = 'eight'; arr[100] = 'hundred'; console.log('arr as Array:', arr); console.log('arr.length=', arr.length); } two(); //*/
Loading…

no comments

    sign in to comment