1

I created this function

function calc (a, b, variableName){
  variableName = a * b;
}

The point is multiple a and b and create global variable

calc(2, 5, first);
calc(3, 5, second);

First function call should be first = 10; second call should be second = 15;

But it not works, how can I get global variable and define its name in function call?

P. Spack
  • 31
  • 4
  • Are you running this code in a browser of node.js? – Bsalex Dec 30 '17 at 02:32
  • 1
    Possible duplicate of [Pass Variables by Reference in Javascript](https://stackoverflow.com/questions/7744611/pass-variables-by-reference-in-javascript) – abagshaw Dec 30 '17 at 02:33
  • 1
    you can't pass variables like that in JS (variables to write into). Why don't you do something like `first = calc(2, 5); second = calc(3, 5);` – Thomas Dec 30 '17 at 03:32

4 Answers4

2

Have the function return the value and then assign the result of the function call to a variable.

function calc (a, b){ 
  return  a * b; 
}


first = calc(2, 5);
second = calc(3, 5);
NineBerry
  • 26,306
  • 3
  • 62
  • 93
2

JavaScript is pass by value. but global variables declared using var (not let or const) are represented on the global object (window in browsers), and the reverse (i.e. defining properties on the global object are available in the global scope) is possible as well.

So, just pass the name of the variable you want to alter as a string—instead of the value—then alter the corresponding property on the global object.

Keep in mind that polluting the global scope is generally not a good idea unless absolutely necessary for the application.

const alter = (prop, val) => Object.assign(window, { [prop]: val });

// Define a global variable
var test = 1;

// Alter the global variable
alter('test', 2);
console.log(test);

// Create a new global variable
alter('hello', 'world');
console.log(hello);

This concept applied to your specific example:

function calc(a, b, variableName) {
  window[variableName] = a * b;
}

calc(2, 5, 'first');
calc(3, 5, 'second');

console.log(first, second);
  • Although this is the literal answer to the user's question it's like answering `What's the best way to jump off a roof` functions that don't return anything and modify shared state is usually not good unless it's a state machine like class or IIFE and the function needs to process state based on previous calls. In that case it's still best to stick it in a class or IIFE. – HMR Dec 30 '17 at 05:12
  • @HMR the user asked a question, I provided the answer then told them why they should never do that. Ultimately this is a question and answer site, not a tutoring site. –  Dec 30 '17 at 16:15
  • Fair enough, thank you for your reply. I do see the warning about polluting global scope although I would worry seeing a function that doesn't return anything and isn't used as a (state machine?? (not sure what to call it)). – HMR Dec 30 '17 at 16:59
  • @HMR sure, but ultimately this is just a bad idea in general. I could go into detail of the multitude of reasons why this is a bad idea, but that isn't really pertinent to how to go about doing it, which may be reasonable in certain situations. Just as there may be certain situations where it would be reasonable to want to know the best way to jump off a roof, even though doing so is generally a bad idea. –  Dec 30 '17 at 17:55
2

Really not recommended... but if you are doing this inside a Web browser, you can attach them to the window object and then they will be available directly by name. Note the variable names must be passed as strings:

function calc (a, b, variableName){
  window[variableName] = a * b;
}

calc(2, 5, 'first');
calc(3, 5, 'second');

console.log(first);
console.log(second);
skyline3000
  • 7,639
  • 2
  • 24
  • 33
  • The `not recommended` got my upvote. Although this is the literal answer to the user's question it's like answering `What's the best way to jump off a roof` functions that don't return anything and modify shared state is usually not good unless it's a state machine like class or IIFE and the function needs to process state based on previous calls. In that case it's still best to stick it in a class or IIFE. – HMR Dec 30 '17 at 05:22
0

There are ways to do this. The first is exactly what you are describing but isn't a very good idea.

▶ node
> global.a = 5
5
> a
5

The global object in node makes all of it's children in the global scope.

Same thing as Tiny Giant described but for backend.

Slava Knyazev
  • 5,377
  • 1
  • 22
  • 43