0

I'm trying to pass variables across functions,
something like

function one() {
  var x = 1
}

function two() {
  var y = x + 1;
}

alert(y);

Is there a way to do it?

edit:Thanks everyone for being so helpful, but maybe I should have been more specific with my question.

2
  • 1
    Please show us how you intend to call the functions. Without calling them, no assignment is done and no value is ever created. Commented Dec 19, 2018 at 22:54
  • Also please explain why you want/need to do this? Having function accept arguments and return values is a much better design. Commented Dec 19, 2018 at 23:52

5 Answers 5

3

You current way has x and y in the scope of the function, which means the other function doesnt know it exists. Also, its good practice to name functions according to what they do. 3 straightforward ways to do this.

  1. Global
  2. Params
  3. Inline

  1. Set two variables outside of the functions scope that any function can reach.

var x, y;

function assignOne() {
   x = 1;
}

function addOne() {
  y = x + 1;
}

assignOne();
addOne();
console.log(y);

  1. Pass in a parameter to to the function and return values.

function one() {
   return 1;
}

function addOneTo(x) {
  return x + 1;
}

const y = addOneTo(one());
console.log(y);

  1. Perform functions inline

var x = null;

function one() {
   x = 1;
}

function two() {
  return x + 1;
}

one();
const y = two();
console.log(y);

Sign up to request clarification or add additional context in comments.

Comments

1

You will need to hoist the scope, by extracting the variable declaration to outside of the functions. That is to say, define x and y outside of the functions. Note that you can still update their values from within the functions. However, don't forget that you'll actually need to invoke both functions as well!

This can be seen in the following:

var x, y;

function one() {
   x = 1;
}

function two() {
  y = x + 1;
}

one();
two();
console.log(y);

Comments

1

If you really want to get variable declared in one method, return it

  function one(){
        var x = 1;
        return x;
    }
    
    function two() {
      var x = one();
      var y = x + 1;
      return y;
    }
alert(two());

Comments

1

Seems like you want to have shared state between both functions instead of passing arguments. So, an object oriented pattern seems to be appropriate.

class Thing {
  constructor() {
    this.x = 1;
  }
  one() {
    return this.x;
  }
  two() {
    return this.x + 1;
  }
}

const t = new Thing();
console.log(t.one());
console.log(t.two());

Comments

1

If you want to share variables between functions but don't want to declare them in global scope, you can use a closure like this:

(function() {
  var x, y;

  function one() {
    var x = 1
  }

  function two() {
    var y = x + 1;
  }
  one();
  two();
  alert(y);
})();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.