Strict equality operator

Why the tutorial uses the strict equality operator in this part ( In this case the last iteration is i === 4) in the text below? I think it should of been I = 4, because that of the tutorial returns true/false statement and I don’t see where the context suggests that.

var arr = [10, 9, 8, 7, 6];
for (var i = 0; i < arr.length; i++) {

Remember that arrays have zero-based indexing, which means the last index of the array is length - 1 . Our condition for this loop is i < arr.length , which stops the loop when i is equal to length . In this case the last iteration is i === 4 i.e. when i becomes equal to arr.length - 1 and outputs 6 to the console. Then i increases to 5 , and the loop terminates because i < arr.length is false .

Challenge: Iterate Through an Array with a For Loop

Link to the challenge:

i=4 doesn’t check for equality, it performs an assignment. So if we want to say “when the iterator is equal to four” we would conventionally say "when i===4". Even though we’re seeing it in English test instead of in code, we typically still use symbols and terms consistent with code rather than how you might see them expressed in non-technical contexts.


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.