I’ve been learning Javascript for a few months now, I understand most of the syntax, I understand what a function is but I just don’t understand how they work. Take this one for example:

```
var array = [1 , 2 , 3 , 6 , 12 , 13 , 17 , 3];
var largest = 0;
for( var i = 0; i < array.length; i++){
if(largest < array[i]) {
largest = array[i];
}}
console.log(largest);
```

This function somehow logs the highest number in the variable called “array”, besides all the array.length stuff in the “for” parameters which I do understand none of this code makes sense to me.

Based on what I understand of Javascript reading through this, the code in this function can be translated as: “If largest (zero in this case) is less than a number in the array, zero is then equal to that number.” That doesn’t make sense! How does that equation calculate the largest number in the array? How does the for loop know when to stop once it’s found the largest number? Wouldn’t this function just log zero every time? Isn’t largest less than every single number in the array?

I understand all the individual pieces but nothing makes sense when it’s all put together. Sorry if this is a long question but I’ve looked around and there don’t seem to be any resources actually detailing how function logic works. Anyone got any tips?