Javascript function logic just doesn't make sense to me

Javascript function logic just doesn't make sense to me

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];


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?

No, = is assignment. It doesn’t mean equals, it’s not maths. Zero is not made equal to the number. The number is assigned to a variable called largest. “If the value of largest is less than the current value, then assign the current value to largest”. If largest is 0, and the current value is 1, then make largest be 1 for the next iteration of the loop and so on