1 minute to read - Apr 12, 2023

Find and explain bug in the code

Find the bug with this code: ''' for(var i=0, i<5,i++){ setTimeout(() => console.log(i), 1000) } '''

There is a syntax error in the for loop. The middle expression should be a comparison operator, such as `<`, `>`, `<=`, `>=`, `==`, or `!=`. However, the code has a comma instead. To fix the error, the for loop should be written as:

Copy code

`for(var i=0; i<5;i++){ setTimeout(() => console.log(i), 1000) }`

loading...