Q:

How to extrapolate variables to make them become globals?

Let's say I have to find out the window width, the code will be:
$(window).width();

and if I want it to become a global variable i just have to declare its name:
var windowWidth = $(window).width();

The result will be the window's width.
But I need that variable to change when things happens, for example when I resize the window:
$(window).resize(function(){
    var windowWidth = $(window).width();
});

How can I extrapolate this variable in order to override the one before?
I can override the variable before putting the function i need in the .resize(function(); but in this way my code confusionary and I need just a variable to use it in other functions outside the resize function, for example a .click(function)
$(document).ready(function(){
    var w = $(window).width();
    $(window).resize(function(){
        var w = $(window).width();

        //click function goes here.
    });
});

how if I want that the function above will become a whole new variable? without putting a new function in it?

A:

the problem with your code is that you have var twice. every time you use var you create a new variable. so just leave out the second one like this:
$(document).ready(function(){
    var w = $(window).width();
    $(window).resize(function(){
        w = $(window).width();

        //click function goes here.
    });
});

