Javascript - What happens when you do not declare but only assign a variable?

You might have stumbled upon this or known it for some time. But it is completely valid to not declare variables in javascript and just assign them values (if you do not use strict mode). Below is an example where we assign a value to an undeclared variable inside a function:

function f(){
    undeclaredVar = 1;    


//same as console.log(window.undeclaredVar);

The console.log in the above logs the value 1. This is because undeclared variables become implicitly global and in a browser they are attached to the window object. There is usually little reason to do this, often you would want to declare your variables as global explicitly (if they are meant to be global).

I hope this makes sense :)