Question: This is my first trial with OOP so please be gentle.. 

First I created a function called Connection and I created objects out of it. There are currently 2 objects hardcoded, but there can by multiple objects dynamically created later on. How do I get the number of created objects (in this sample the result should be "2")? Or is there any smarter way to loop through all created objects?

```
function Connection(origin, target, departuredate, arrivaldate) {
     this.origin = origin;
     this.target = target;
     this.departuredate = departuredate;
     this.arrivaldate = arrivaldate;
}

var fare1 = new Connection("FRA", "BKK", "25.06.2018", "10.07.2018");
var fare2 = new Connection("FRA", "LAX", "22.07.2018", "30.07.2018");

```

Answer: 
In javascript functions are objects also and can have properties. Based on comments by georg and to avoid a global variable, why not just give `Connection` an array property and then use it however you want?

```js
function Connection(origin, target, departuredate, arrivaldate) {
     this.origin = origin;
     this.target = target;
     this.departuredate = departuredate;
     this.arrivaldate = arrivaldate;
     Connection.instances.push(this);
}

Connection.instances = [];

var fare1 = new Connection("FRA", "BKK", "25.06.2018", "10.07.2018");
var fare2 = new Connection("FRA", "LAX", "22.07.2018", "30.07.2018");

console.log(Connection.instances);
console.log('Number of connections:', Connection.instances.length);
```

**Edit:**

Keep in mind that this is a naive approach. It does not remove an instance of a `Connection` ever from the instances property. Furthermore it will keep references to those instance around forever, probably not what you want to do. I leave all of that as an exercise for the reader as, technically, the question did not ask for or about any of it.
You could use a global var in the constructor.

```
var connectionNb = 0;

function Connection(origin, target, departuredate, arrivaldate) {
     this.origin = origin;
     this.target = target;
     this.departuredate = departuredate;
     this.arrivaldate = arrivaldate;
     ++connectionNB;
}

var fare1 = new Connection("FRA", "BKK", "25.06.2018", "10.07.2018");
var fare2 = new Connection("FRA", "LAX", "22.07.2018", "30.07.2018");

```