undefinedfix
Sign in

Automatic conversion of JS data subscript

Tapuzi edited in Tue, 30 Nov 2021
var arr = [1,2,3,4,5,6,7]
for(var j in arr){
   console.log(j+1);
   console.log(j-1);
}

When j + 1 is output, j is automatically converted to string processing. The output J-1 is processed digitally. What is the reason?

2 Replies
sbarb
commented on Tue, 30 Nov 2021

The variables in the for in loop are all strings (index), + can be used as a string connector, you can change it to console.log (+j+1);

kcomf
commented on Wed, 01 Dec 2021

It's upstairs. In addition, array traversal with for in is not recommended. Develop the habit of writing standard code.

lock This question has been locked and the reply function has been disabled.