how to pass variable from one function into another in javascript

To pass a variable from one function to another in JavaScript, you can simply pass it as an argument to the function that needs it.

index.tsx
function firstFunction() {
  var variable1 = "hello";
  secondFunction(variable1);
}

function secondFunction(arg1) {
  console.log(arg1); // outputs "hello"
}

firstFunction();
178 chars
11 lines

In the example above, variable1 is passed as an argument to secondFunction by calling secondFunction(variable1). arg1 in secondFunction becomes a local variable that holds the same value as variable1. You can then use arg1 within secondFunction just like any other variable.

gistlibby LogSnag