0

It is possible to global a variable inside a function, if not, how can I access a var in function b from function a.

var money;
var moneyInput = document.getElementById('input');

function check(){
if (moneyInput.value == "") {
    money = 100;
}                   

function hit() {
    document.write(money) // will this money var be 100 or undefined?
}
Yangshun Tay
  • 49,270
  • 33
  • 114
  • 141
Steven Tran
  • 104
  • 1
  • 8
  • post the example code – prasanth Jun 03 '17 at 17:05
  • 3
    *"It is possible to global a variable inside a function"* Yes. *"how can I access a var in function b from function a"* You usually pass the value as argument to the function. Having to use global variables could be an indicator of bad design. Can you provide more information/context about what you are actually trying to do? – Felix Kling Jun 03 '17 at 17:12
  • 1
    Also please use the search because this is a possible duplicate of [How to declare a global variable in JavaScript?](https://stackoverflow.com/q/3352020/218196) – Felix Kling Jun 03 '17 at 17:13
  • 1
    Possible duplicate of [How to declare a global variable in JavaScript?](https://stackoverflow.com/questions/3352020/how-to-declare-a-global-variable-in-javascript) – Assafi Cohen-Arazi Jun 03 '17 at 17:15

7 Answers7

2

Just declare that variable outside function and then use it inside of that function.

P.S Please, post some code that you tried already.

let a = 5;
let b = 0; // Declare global variable

function printAB(_b) {
  b = _b; // Set new value through this function
  console.log(a, b);
}

function printBC() {
  let c = 7;
  console.log(b, c); // Get b variable
}

printBC(); // 0, 7 // b = 0
printAB(2); // 5, 2
printBC(); // 2, 7 // b = 2

Edit

With code you provided, answer is simple. Variable money can be equal 100 or be undefined, that depends if function hit will be called before function check.

Community
  • 1
  • 1
Oen44
  • 3,156
  • 1
  • 22
  • 31
1

If you assign a value to an undeclared variable in a function it will be created as a global variable.

user2182349
  • 9,569
  • 3
  • 29
  • 41
1

You should declare the variable outside of the function scope:

var myGlobalVar = "foo";

function a(){
   var myLocalVar = 'bar';
   alert(myGlobalVar); //shows foo
   alert(myLocalVar); //shows bar
}

function b(){
   var myLocalVar = 'baz';
   alert(myGlobalVar); //shows foo too
   alert(myLocalVar); //shows baz
}
Raphael Martin
  • 67
  • 2
  • 10
1

Assign a variable without using var, let, or const

var foo = 'bar';

function fn() {
  console.log(foo);
  bar = 'xyz';
}

fn()

console.log(bar)

Repl: https://repl.it/languages/javascript

Baruch
  • 2,381
  • 1
  • 17
  • 29
1

You can assign a value to the global object ( window in browsers):

function a(){
  window.test="test";
}
function b(){
 console.log(test);
}
a(),b();
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
1

Declaring the variable on the outer scope and using it inside should solve your problem. However if you insist on creating a variable on the global scope you can do the following;

window.VAR_NAME = 'foo';

By doing so you are actually creating a global variable VAR_NAME and it now has the value foo.

Tukan
  • 2,253
  • 15
  • 17
1

/* 1 */

var a = 'sample text';

function one() {
  a = 'modified text';
}

function two() {
  console.log(a);
}

one();
two();

/* 2 */

function one(callback) {
  var a = 'sample text callback';
  callback(a);
}

function two() {
  one(function(a) {
    console.log(a);
  });
}

two();

/* 3 */

var one = new Promise(function(resolve, reject) {
  var a = 'sample text promise';
  resolve(a);
});

function two() {
  one.then(function(a) {
    console.log(a);
  });
}

two();

/* 4 */

var myGlobals = {};

function one() {
  myGlobals.a = 'sample text';
}

function two() {
  console.log(myGlobals.a);
}

one();
two();