faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 7 people (71%) answered Yes
Recently 5 of 7 people (71%) answered Yes

Entry

Is it possible to convert a local variable to a Global variable?

Apr 5th, 2008 20:31
ha mo, Mark Szlazak, Jean-Bernard Valentaten, Guy Perkins,


Only using a workaround I'm afraid.
Simply declare a global variable and inside the function you assign it 
the value of the local variable.
HTH
An addendum.
If you want to use the same variable name locally and globally, say 
local variable "x" within the scope of some function and global 
variable "x", then make a global assignment to the "x" property of 
the "window" object: window.x = x;
<html>
<head>
<script>
x = 20;
function local() {
	var x = 10;
	return x;
}
alert ('Local x: '+local()+'\nGlobal x: '+x);
function localToGlobal() {
	var x = 10;
	window.x = x;
        return x;
}
alert ('Local x: '+localToGlobal()+'\nGlobal x: '+x);
</script>
</head>
<body>
</body>
</html>
Mark
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com