Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't JavaScript warn me when I use arr.lenght (misspelt) instead of arr.length in a loop? I also use strict mode

Tags:

javascript

I spent hours just to find out that I misspelt the word .length as .lenght. It can run normally with no warning at all. Why...?

I use 'use strict' and run on Node.js 10.13.0.

Code:

'use strict';  let arr = [1, 2, 3, 4];  for(let i = 0; i < arr.lenght; i++) {    console.log(arr[i]);  }
like image 930
MangoLato Avatar asked Dec 03 '18 03:12

MangoLato


People also ask

What does .length mean in JavaScript?

The length property returns an integer value that represents the number of characters in the string. If the string is an empty string, the length property will return 0.

How does array length work JavaScript?

What exactly is the JavaScript Array length property. By definition, the length property of an array is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array. The value of the length is 232. It means that an array can hold up to 4294967296 (232) elements.

Why is array length not a method JavaScript?

Length is not a method, it is a property. It doesn't actually do anything but return the length of an array, a string, or the number of parameters expected by a function. When you use . length, you are just asking the JavaScript interpreter to return a variable stored within an object; you are not calling a method.

Can JavaScript array length be negative?

No, you cannot use a negative integer as size, the size of an array represents the number of elements in it, –ve number of elements in an array makes no sense.


2 Answers

Because when you try to get a property that doesn't exist, it returns undefined, and 0 < undefined is false.

let arr = [1, 2, 3, 4];  console.log(arr.lenght) // undefined  console.log(arr.qwerty) // undefined  console.log(arr.lenght < 9999) // false  console.log(arr.lenght > 9999) // false    arr.length = 7 // <-- it's not a good idea  for(let i = 0; i < arr.length; i++) {console.log(arr[i])}

EDIT

I said 'javascript is not a strongly typed language' and it is true. But this way of adding new properties it is a feature of prototype-based programming, as @Voo said.

I also said .length=7 it's a bad idea. After reading a little more, in this case I still think it's a little weird to increase the length property after adding elements. Maybe it's fine to truncate, delete elements or empty an array, although in the latter case I would prefer arr=[] instead of arr.length=0.

There are some interesting examples about length property in the Mozilla documentation.

A JavaScript array's length property and numerical properties are connected. Several of the built-in array methods (e.g., join(), slice(), indexOf(), etc.) take into account the value of an array's length property when they're called. Other methods (e.g., push(), splice(), etc.) also result in updates to an array's length property.

var fruits = []; fruits.push('banana', 'apple', 'peach'); console.log(fruits.length); // 3 

When setting a property on a JavaScript array when the property is a valid array index and that index is outside the current bounds of the array, the engine will update the array's length property accordingly:

fruits[5] = 'mango'; console.log(fruits[5]); // 'mango' console.log(Object.keys(fruits));  // ['0', '1', '2', '5'] console.log(fruits.length); // 6 

Increasing the length.

fruits.length = 10; console.log(Object.keys(fruits)); // ['0', '1', '2', '5'] console.log(fruits.length); // 10 

Decreasing the length property does, however, delete elements.

fruits.length = 2; console.log(Object.keys(fruits)); // ['0', '1'] console.log(fruits.length); // 2 
like image 138
eag845 Avatar answered Oct 12 '22 22:10

eag845


JavaScript arrays are treated as objects (though they are instances of Array). Hence, when you write arr.lenght, it treats lenght as a property of an object that is undefined. Hence, you don't get an error.

It simply tries to get a property that is undefined. Also, in your case, the loop just does not execute as the condition of the loop is never satisfied.

like image 22
Rohan Dhar Avatar answered Oct 12 '22 22:10

Rohan Dhar