Weird for loop behaviour

Problem description:
Replit doesnt throw error if the variable i is not declared in for loop .

Expected behavior:
It should throw error saying variable i is not declared/ not defined.

Actual behavior:
It doesnt throw error which is quite incorrect.

Steps to reproduce:
Please write a for loop in js without declaring the i variable .ie
for(i=0;i< array.length;i++)

Bug appears at this link:


Welcome to the forums @pal05249!

You’ve defined it here!


in js you need a variable declaration keyword such as let the issue is that it runs even though it should not without that keyword


I’m aware about this javascript feature. But I think it’s intentional, I mean, its in Javascript itself, not only Replit.
(It works in my console, without a let statement)


wow thank you so much I didn’t realize :smiley:


Yep it’s definitely a feature of JavaScript, not Replit (it’s your browser running the code, not Replit), as @Raadsel stated.

I believe the reason this works is because in the browser, the global scope is actually the window object. So when you don’t use a keyword to define it, you actually might technically be doing window.i=0. And properties of the window object don’t need to be accessed via window, for example console.log is actually a property of the window when JavaScript is run in your browser.

1 Like
"use strict";

Put this at the top of the file to prevent mistakes which may occur due to this implicit global


Youcan use strict mode in a particular function too, if it’s desirable. I would recommend reading about strict mode here first, before you write it on the top of every page!!