Logic behind for loop


#1

Do I see it right that there is a while loop within the for loop? I know the while loop from python and I'm confused now. I'm sure I'll get it further in the lessons but I'm wondering if I'm the only one (coming from python) finding the construction of the for loop rather ugly.


#2

@2freet,
the for-loop:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

the while-loop:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while

the do-while-loop:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html
= http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript


#3

Yeah I see now.. I was just confused that there was a (sort of while-) condition in the for loop. But there is as well in python (we just don't have to write it). There it is more like "for all the things in this: do that". Thank you for the links!


#4

@2freet,

If you are on the lookout
for the Javascript specifics....
there is a stackoverflow.com site which is used by millions of programmers....
with google search
== discussions / opinions ==
javascript for loop site:stackoverflow.com

The python "for all the things in this: do that"
is equivalent to the Javascript for-in loop.
This for-in loop is reserved to traverse through an object
( a Javascript object is Pythons dictionary )

An object has one or more properties, separated by a comma_,

A property has a property-key and it's associated VALUE

The associated VALUE can be anything from:

•Number
•String
•Boolean
•Symbol
•Object  ◦Function/'Method'
         â—¦Array
         â—¦Date
         â—¦RegExp
         â—¦Math
•Null
•Undefined

Above you have an overview of all data-types.

===== Datatype Array =========
What you must understand,
is that if you ( as an example )
create a array
for the Javascript-Interpreter
you have created an object of the prototype array.
and if you do a google search
javascript array site:developer.mozilla.org
and choose
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
on the left hand side you will see
what information you can gather about the array

PropertiesArray.prototype

Array.prototype.length

Methods ( pre-defined functions for Array operations )

Array.isArray()
Array.prototype.concat()
Array.prototype.every()
Array.prototype.filter()
Array.prototype.forEach()
Array.prototype.indexOf()
Array.prototype.join()
Array.prototype.lastIndexOf()
Array.prototype.map()
Array.prototype.pop()
Array.prototype.push()
Array.prototype.reduce()
Array.prototype.reduceRight()
Array.prototype.reverse()
Array.prototype.shift()
Array.prototype.slice()
Array.prototype.some()
Array.prototype.sort()
Array.prototype.splice()
Array.prototype.toLocaleString()
Array.prototype.toString()
Array.prototype.unshift()

========= looping over an array ===

The most common way to loop over all elements of an Array
is using the generic FOR-loop
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

for ([initialization]; [condition]; [final-expression]) {
      statement
}

You would
start at index zero,
continue as long as the index is =less than= the array.length,
and at each iteration increment / decrement the index-pointer

var myArray =["a",2,{anObjectKey: "anObjectValue"}];
for ( var index = 0, len = myArray.length; index < len ; index= index + 1 ) {
     // get access to the =element-Value= using the [bracket-notation]
     console.log( index + "==> "+myArray[index] );
}

== Do NOT use FOR-IN loop on an Array ==

You could look at an Array as it being an Object like

 myArray = {
      0: "a",
      1: 2,
      2: { anObjectKey: "anObjectValue"}
 }

and with the FOR-IN loop

for (var key in myArray) {
     //getting access to the =associate property-Value= 
      //using the =key= in a [bracket-notation]
      console.log( key + "==>"+myArray[key] );
}

but do a google search
== discussions / opinions ==
javascript why is it a bad idea to use for-in on an Array site:stackoverflow.com
for instance
= http://stackoverflow.com/questions/5269757/why-is-javascripts-for-in-loop-not-recommended-for-arrays
[quote]
An array is an object,
and array elements are simply properties with the numeric index converted to string.
For example, arr[123] refers to a property "123" in the array object arr.

The for ... in construct
works on all objects, not just arrays,
and that is cause for confusion.

When somebody for ... in an array,
most often the programmer intends to iterate just all the items, even most likely in order.

The semantics is so similar to array iteration in other programming languages that it is very easy to get confused.
In JavaScript, this construct does not iterate array elements in order.
It iterates all

  • the array's properties
  • (including inherited prototype functions, any properties added to it, any other non-element properties added to it etc.),
  • and not in order at all.
  • It will even find the property "length".

This is most likely not what the programmer means, if he/she comes from another programming language.

That's why you shouldn't do it.
You shouldn't use a language construct
that looks like it does obvious things, but actually does things that are completely different.
It creates bugs that are very obscure and very difficult to find.
[end quote Stephen Chung]