What is the reason for offset.top not work without var of element

This one work

var $test = $('#test');
var $testoffset = $test.offset().top;

but with without var for element #test


Not work? What the logic behind this?

Without the var keyword, this would create a global variable, which can cause many problems.


Still a question with when use .scroll function with jquery that default what is inside is script that is global? I try with .each function and there only works var inside it what used in .each function when outside its not work. So basicly functions in jQuery all global or local like .each?