Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3439480
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:18:56+00:00 2026-05-18T08:18:56+00:00

I have multiple global arrays that a single method performs operations on. The method

  • 0

I have multiple global arrays that a single method performs operations on. The method will only need to operate on one array at a time. I would like to accomplish this by passing a parameter to the method and then let the method decide which array it needs to modify based on that parameter. For example,

var globalarray1;
var globalarray2;

Operate(globalarray1);

function Operate(globalarray){
   globalarray.push("test");
}

Of course, the code above only changes the value of the array local to the scope of the method. I know I can do something like this:

var globalarray1;
var globalarray2;

Operate(1);

function Operate(flag){
   if (flag == 1){
      globalarray1.push("test1");
   }
   else if (flag == 2){
      globalarray2.push("test2")
   }
}

However, it just doesn’t feel right. How can I change the value of the globals using parameters in a single method without using a bunch of conditional statements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:18:56+00:00Added an answer on May 18, 2026 at 8:18 am

    Your first approach is correct. This statement, however, is not:

    Of course, the code above only changes the value of the array local to the scope of the method.

    Array objects are passed by reference called by sharing (i.e. the reference is passed by value, not the value itself). When you pass the array to the method, it can (and in your case does) actually modified the global variable. This would not be the case if you passed in an immutable or primitive value such as a number or a string. In those cases, the value is in fact local to the scope of the method.

    The fact that your variables are global have nothing to do with it. Take this code, for example:

    function Hello(){
       var localArray = [];
       Operate(localArray);
    
       // now, localArray has been modified by Operate
    }
    
    Hello();
    

    Above, localArray is not a global variable, but it can still be affected by Operate() if you pass the array in directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a global object MyFruits that can have multiple properties. At a minimum,
I have a single xml parsing function that I'm trying to call multiple times
I have multiple threads (C# application running on IIS) running that all need to
I have multiple WCF services that share some data contracts and need to generate
I have multiple ajax requests with javascript code as response, and I need to
I have multiple UpdatePanels on a webpage, one inside UserControl (Purple ColorBox as show
I have multiple websites run on single database. My question related table Structure is
I have multiple arrays with string values and I want to compare them and
Using Wordpress I have created a multiple select box so that users can select
I have a local application that runs on multiple screens which I am in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.