Why JavaScript doesn't warn me when I use arr.lenght (misspelt) instead of arr.length in a loop? I also use...
up vote
7
down vote
favorite
I spent hours just to find out that I misspelt the word .length
with .lenght
. It can run normally with no warning at all. Why...?
I use 'use strict'
and run on Node 10.13.0.
Code:
'use strict';
let arr = [1, 2, 3, 4];
for(let i = 0; i < arr.lenght; i++) {
console.log(arr[i]);
}
javascript
add a comment |
up vote
7
down vote
favorite
I spent hours just to find out that I misspelt the word .length
with .lenght
. It can run normally with no warning at all. Why...?
I use 'use strict'
and run on Node 10.13.0.
Code:
'use strict';
let arr = [1, 2, 3, 4];
for(let i = 0; i < arr.lenght; i++) {
console.log(arr[i]);
}
javascript
2
You could easily add new properties toarr
object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actuallyi < undefined
everytime. I'll suggest you to read stackoverflow.com/questions/1335851/….
– mmontoya
1 hour ago
Simple answer is...because it's a loosely typed language. Everyone has made property name typos. Lick your wounds and move on
– charlietfl
1 hour ago
add a comment |
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I spent hours just to find out that I misspelt the word .length
with .lenght
. It can run normally with no warning at all. Why...?
I use 'use strict'
and run on Node 10.13.0.
Code:
'use strict';
let arr = [1, 2, 3, 4];
for(let i = 0; i < arr.lenght; i++) {
console.log(arr[i]);
}
javascript
I spent hours just to find out that I misspelt the word .length
with .lenght
. It can run normally with no warning at all. Why...?
I use 'use strict'
and run on Node 10.13.0.
Code:
'use strict';
let arr = [1, 2, 3, 4];
for(let i = 0; i < arr.lenght; i++) {
console.log(arr[i]);
}
'use strict';
let arr = [1, 2, 3, 4];
for(let i = 0; i < arr.lenght; i++) {
console.log(arr[i]);
}
'use strict';
let arr = [1, 2, 3, 4];
for(let i = 0; i < arr.lenght; i++) {
console.log(arr[i]);
}
javascript
javascript
edited 1 hour ago
CertainPerformance
68.8k143453
68.8k143453
asked 1 hour ago
MangoLato
383
383
2
You could easily add new properties toarr
object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actuallyi < undefined
everytime. I'll suggest you to read stackoverflow.com/questions/1335851/….
– mmontoya
1 hour ago
Simple answer is...because it's a loosely typed language. Everyone has made property name typos. Lick your wounds and move on
– charlietfl
1 hour ago
add a comment |
2
You could easily add new properties toarr
object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actuallyi < undefined
everytime. I'll suggest you to read stackoverflow.com/questions/1335851/….
– mmontoya
1 hour ago
Simple answer is...because it's a loosely typed language. Everyone has made property name typos. Lick your wounds and move on
– charlietfl
1 hour ago
2
2
You could easily add new properties to
arr
object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined
everytime. I'll suggest you to read stackoverflow.com/questions/1335851/….– mmontoya
1 hour ago
You could easily add new properties to
arr
object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined
everytime. I'll suggest you to read stackoverflow.com/questions/1335851/….– mmontoya
1 hour ago
Simple answer is...because it's a loosely typed language. Everyone has made property name typos. Lick your wounds and move on
– charlietfl
1 hour ago
Simple answer is...because it's a loosely typed language. Everyone has made property name typos. Lick your wounds and move on
– charlietfl
1 hour ago
add a comment |
4 Answers
4
active
oldest
votes
up vote
11
down vote
accepted
Because when you try to get a property that does'nt exist, it returns undefined
, and 0 < undefined
is false
.
Take into account that javascript is not a strongly typed language. You can add new properties by simply giving it a value arr.something=123
, even you can set arr.length=7
but it's not a good idea.
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])}
That makes sense, thanks.
– MangoLato
1 hour ago
add a comment |
up vote
2
down vote
The the upper bound of the loop is specified as lenght
, a typo for the local variable length. At runtime, lenght
will evaluate to undefined
, so the check i < lenght
will always fail, and the loop body is never executed.
add a comment |
up vote
1
down vote
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.
add a comment |
up vote
1
down vote
You could easily add new properties to arr
object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined
everytime because you're calling a property that haven't been created on the object. I'll suggest you to read What does "use strict" do in JavaScript, and what is the reasoning behind it?.
New contributor
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
Because when you try to get a property that does'nt exist, it returns undefined
, and 0 < undefined
is false
.
Take into account that javascript is not a strongly typed language. You can add new properties by simply giving it a value arr.something=123
, even you can set arr.length=7
but it's not a good idea.
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])}
That makes sense, thanks.
– MangoLato
1 hour ago
add a comment |
up vote
11
down vote
accepted
Because when you try to get a property that does'nt exist, it returns undefined
, and 0 < undefined
is false
.
Take into account that javascript is not a strongly typed language. You can add new properties by simply giving it a value arr.something=123
, even you can set arr.length=7
but it's not a good idea.
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])}
That makes sense, thanks.
– MangoLato
1 hour ago
add a comment |
up vote
11
down vote
accepted
up vote
11
down vote
accepted
Because when you try to get a property that does'nt exist, it returns undefined
, and 0 < undefined
is false
.
Take into account that javascript is not a strongly typed language. You can add new properties by simply giving it a value arr.something=123
, even you can set arr.length=7
but it's not a good idea.
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])}
Because when you try to get a property that does'nt exist, it returns undefined
, and 0 < undefined
is false
.
Take into account that javascript is not a strongly typed language. You can add new properties by simply giving it a value arr.something=123
, even you can set arr.length=7
but it's not a good idea.
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])}
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])}
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])}
edited 57 mins ago
answered 1 hour ago
eag845
414310
414310
That makes sense, thanks.
– MangoLato
1 hour ago
add a comment |
That makes sense, thanks.
– MangoLato
1 hour ago
That makes sense, thanks.
– MangoLato
1 hour ago
That makes sense, thanks.
– MangoLato
1 hour ago
add a comment |
up vote
2
down vote
The the upper bound of the loop is specified as lenght
, a typo for the local variable length. At runtime, lenght
will evaluate to undefined
, so the check i < lenght
will always fail, and the loop body is never executed.
add a comment |
up vote
2
down vote
The the upper bound of the loop is specified as lenght
, a typo for the local variable length. At runtime, lenght
will evaluate to undefined
, so the check i < lenght
will always fail, and the loop body is never executed.
add a comment |
up vote
2
down vote
up vote
2
down vote
The the upper bound of the loop is specified as lenght
, a typo for the local variable length. At runtime, lenght
will evaluate to undefined
, so the check i < lenght
will always fail, and the loop body is never executed.
The the upper bound of the loop is specified as lenght
, a typo for the local variable length. At runtime, lenght
will evaluate to undefined
, so the check i < lenght
will always fail, and the loop body is never executed.
answered 1 hour ago
fuzz
15k17108183
15k17108183
add a comment |
add a comment |
up vote
1
down vote
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.
add a comment |
up vote
1
down vote
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.
add a comment |
up vote
1
down vote
up vote
1
down vote
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.
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.
answered 1 hour ago
Rohan Dhar
624213
624213
add a comment |
add a comment |
up vote
1
down vote
You could easily add new properties to arr
object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined
everytime because you're calling a property that haven't been created on the object. I'll suggest you to read What does "use strict" do in JavaScript, and what is the reasoning behind it?.
New contributor
add a comment |
up vote
1
down vote
You could easily add new properties to arr
object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined
everytime because you're calling a property that haven't been created on the object. I'll suggest you to read What does "use strict" do in JavaScript, and what is the reasoning behind it?.
New contributor
add a comment |
up vote
1
down vote
up vote
1
down vote
You could easily add new properties to arr
object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined
everytime because you're calling a property that haven't been created on the object. I'll suggest you to read What does "use strict" do in JavaScript, and what is the reasoning behind it?.
New contributor
You could easily add new properties to arr
object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actually i < undefined
everytime because you're calling a property that haven't been created on the object. I'll suggest you to read What does "use strict" do in JavaScript, and what is the reasoning behind it?.
New contributor
New contributor
answered 1 hour ago
mmontoya
1138
1138
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53586954%2fwhy-javascript-doesnt-warn-me-when-i-use-arr-lenght-misspelt-instead-of-arr-l%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You could easily add new properties to
arr
object, JavaScript won't warn you about it, instead it will try to find the property you're calling, and if it didn't find anything such result will be undefined, so the comparison is actuallyi < undefined
everytime. I'll suggest you to read stackoverflow.com/questions/1335851/….– mmontoya
1 hour ago
Simple answer is...because it's a loosely typed language. Everyone has made property name typos. Lick your wounds and move on
– charlietfl
1 hour ago