2

For some reason I cannot assign anything to a global array without using .push().

(function() {
    var globalEmail = [];

    var testClear = function() {
        var arr = [1, 2, 3];

        if (globalEmail.length > 1) {
            globalEmail = [];
        }
        else {
            globalEmail = arr;
        }
    };

    window.globalEmail = globalEmail;
    window.testClear = testClear;
})();

If I call testClear() and then globalEmail in the console, globalEmail remains unchanged.

6
  • 3
    Works fine for me... pasted your code in the console, called testClear();, then window.globalEmail - result: (3) [1, 2, 3] globalEmail is also populated. Commented Jan 5, 2018 at 21:44
  • 2
    You are loosing your references when you do globalEmail = []; or globalEmail = arr;, so window.globalEmail is different than the new globalEmail array. You need to modify the array in place (with splice and concat, for your example). Commented Jan 5, 2018 at 21:46
  • @tymeJV: try executing this code inside an IIFE, and then use globalEmail and testClear to reproduce the issue. Commented Jan 5, 2018 at 21:48
  • the code works fine! If the code has to be executed in a IIFE, this should be mentioned in the question Commented Jan 5, 2018 at 21:49
  • @kLabz Thank you, this solution works fine. If you want to add it as an answer I will accept it. :) Commented Jan 5, 2018 at 21:53

3 Answers 3

2

For empty an array, you could set the lenght to zero and for assigning, you could use Array#push with spread syntax ...

var globalEmail = [],
    testClear = function() {
        var arr = [1, 2, 3];

        if (globalEmail.length > 1) {
            globalEmail.length = 0;
        } else {
            globalEmail.push(...arr);
        }
    };

testClear();
console.log(globalEmail);

testClear();
console.log(globalEmail);

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

2 Comments

assuming you use a transpiler for the that ;-)
@MarouenMhiri, it's ES6 standard (as the question is tagged).
1

As said in comments, you are loosing references here because you assign globalEmail to new arrays, so window.globalEmail is different than the new globalEmail array.

You need to modify the array in place (with splice and concat, for your example).

Comments

0

You want to define the globalEmail variable as global, not local.

(function() {
    window.globalEmail = []; // this goes here

    var testClear = function() {
        var arr = [1, 2, 3];

        if (window.globalEmail.length > 1) {
            window.globalEmail = [];
        }
        else {
            window.globalEmail = arr;
        }
    };

    //window.globalEmail = globalEmail; <-- not here
    window.testClear = testClear;
})();

The window. is not absolutely required, but the var globalEmail; within a function defines a local variable, which is not what you wanted in the first place.

Although, if the point of testClear() is to clear any array, then it is not properly implemented. Instead it should take a parameter and you work on that parameter.

function testClear(a)
{
    var arr = [1, 2, 3];

    if(a.length > 1) {
        a.splice(0);
    }
    else {
        a.splice(0);
        a.push.apply(a, arr);
    }
}

testClear(window.globalEmail);

Then the testClear() function makes more sense, I think.

Note that there are drawbacks with a.push.apply() which breaks on large arrays. See here How to extend an existing JavaScript array with another array, without creating a new array? for details.

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.