When repeating values, why typeof (value) returns "string" when value is a number? Javascript

I use Google Chrome for this test: Unlike intuition, the first cycle thrice signals a "string", and the second cycle thrice signals a "number".

numarray = [1, 2, 3];

//for-each loop
for(num in numarray) 
    alert(typeof(num));

//standard loop
for(i=0; i<numarray.length; i++) 
    alert(typeof(numarray[i]));

I expected both loops to warn the "number" 3 times. How is the first loop implemented in javascript? In other words, if for-each is syntactic sugar, what is its equivalent using a standard loop?

Also, is there a way to iterate over the namespace of objects using a standard loop? I want to touch each of the methods and attributes of objects using a loop of the second kind. I am new to Javascript and any help is much appreciated, thanks.

+5
1

, "", , , num , numarray . , num typeof num, , 0, 1 2, , .

for in, , for . JavaScript . typeof.

Edit:

, - for in, .

filip-fku , for in, . , , . :

var myName = {
  first: 'Jimmy',
  last: 'Cuadra'
};

for (var prop in myName) {
  console.log(prop + ': ' + myName[prop]);
}

// prints:
// first: Jimmy
// last: Cuadra

, for in . , , for in:

for (var prop in obj) {
  if (obj.hasOwnProperty(prop)) {
    // do something
  }
}

, , , , .

+6

All Articles