2

I'm trying to discover how to send some private variables to the global scope. I've created a brief demo:

function test() {
    let private1 = 1, private2 = 2;

    return private1;
}

test(); // returns the first variable value, but not if I have multiple variables
console.log(private1); // returns obvious error - how to make private variable global?

Obviously logging any variables will return an error as they only exist in the local scope relative to the test function. So my question is, how can these variables be made public?

I've never had to use variables this way, nor could I find any other posts that explains this, so is this generally bad practice? I'm curious to know. Any explanation or guidance of how best to treat variables would be very helpful, thank you.

0

8 Answers 8

7

A variable declared inside a function is only available inside that said function, unless you declare it outside the function.

In this example, you can't use private1 outside the function.

function test() {
  let private1 = 1;
  return private1;
}
test();
console.log(private1);

You can't make it global otherwise from outside the scope of the said function.

In this example, you can use private1 outside the function.

let private1;

function test() {
  private1 = 1;
  return private1;
}
test();
console.log(private1);

0
3

The are 2 ways to add a variable to the global (and only the global scope) from inside a function (without returning the variables in an object or array). Though, by adding a variable to the global scope, you make it available to every script running on that page.

1. Make sure that strict mode is not activated and "forget" to declare the variables

// Dont do "use strict";
function test() {
    private1 = 1;
    private2 = 2;
    return private1;
}

When strict mode is not used, then whenever JavaScript sees a variable that has not been declared then it automatically declares it on the global scope. Though, this is very bad practice and should be avoided.

2. Add the variables directly to the global object

function test() {
    window.private1 = 1;
    this.private2 = 2;
    return private1;
}

The window object can be obtained using window or this. But this will only return the window if it is not called from inside an object or a binded/applied function.

Return and Destructuring (ES6)

A nice feature of ES6 (meaning this cant be used on older browsers like IE) is destructuring which allows you to do this:

function test() {
    return { private1: 1, private2: 2 };
}
var { private1: private1, private2: private2 } = test();

Or

function test() {
    return [1, 2];
}
var { 0: private1, 1: private2 } = test();
1

You can either create global variables and assign value to them

let global1,global2;
function test() {
    let private1 = 1, private2 = 2;
    global1 = private1;
    global2 = private2;

    return private1;
}

test(); 
console.log(global1); 

or you can use returned value

function test() {
    let private1 = 1, private2 = 2;

    return private1;
}

var result = test(); 
console.log(result);
1

The simplest way to make private1 global would be to do something like this

let global1

function test() {
    let private1 = 1, private2 = 2;

    return private1;
}

global1 = test()

But, as the other answer says, if you wanted this, it would be better to declare the variable outside the function scope.

1

scope of let is fixed, you cant access it outside the function. On solution to your problem is binding variable to window.

function test() {
    private1 = 1; // or you can use window.private1 = 1
    private2 = 2;
    return private1;
}


test(); 
console.log(private1);

If you dont declare your variable with var let or const it automatically gets binded to window scope, But this not considered as a good practice. You can also use window.variableName for this purpose.

1

Why first private and then public? If you need some private space, you could take an IIFE and a destructuring assignment of the result object to global variables.

var { private1: global1, private2: global2 } = function () {
        let private1 = 'foo',
            private2 = 'bar';
            
        return { private1, private2 };
    }();

console.log(global1, global2);

1

Just don't add any declaration at the Begining.Javascript treat this as a global variable.

function test(){
   private1 = 1;
   private2 = 2
}

test();

After the call of the function test the private1 and private2 set as a global variable.

If you put a console below test like this

console.log(private1,private2);
//1 2 is the answer.

It will printed in the console and you can able to call it like this too.

window.private1;
window.private2;

That's it.

1

If you're on a browser, you can assign them to window.

function test() {
  window.private1 = 1, window.private2 = 2;
}

test()

console.log(private1, private2)

Or you can export them by returning them:

function test() {
    let private1 = 1, private2 = 2;

    return {
      private1,
      private2
    };
}

console.log(test())

So is this generally bad practice?

Yes, making them global is bad practice. Exporting them if they are really needed outside the function itself is ok though.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.