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?

3
  • Are you running this code in a browser of node.js? Commented Dec 30, 2017 at 2:32
  • 1
    Possible duplicate of Pass Variables by Reference in Javascript Commented Dec 30, 2017 at 2: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); Commented Dec 30, 2017 at 3:32

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

Comments

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);

1 Comment

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.
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);

4 Comments

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 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.
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 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.
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.

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.