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

  • Home
  • SEARCH
  • 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 8388333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:27:08+00:00 2026-06-09T18:27:08+00:00

I declare a variable, outside of a function like so: var vitalsValuesChecked = [];

  • 0

I declare a variable, outside of a function like so:

var vitalsValuesChecked = [];

then inside of a function I do:

vitalsValuesChecked.push('foobar');

In a later function I need to loop through the array for the items pushed, and constantly am not getting the result I expect. So inside that same function, I added console.log(vitalsValuesChecked); which returns [].

EDIT Code sample below;
EDIT 2 Fixed code below

var vitalsValuesChecked = [];

$(document).delegate("#hv-byresult-btn", "click", function () {
    var vitalsTypeList = ['bp', 'ht', 'wt', 'pulse', 'resp', 'temp'];
    vitalsValuesChecked = [];
    for (var i = 0;i < vitalsTypeList.length;i++) {
        if (document.getElementById(vitalsTypeList[i]).checked == true) {
            vitalsValuesChecked.push(vitalsTypeList[i]);
            console.log(vitalsTypeList[i] + " is checked. Adding to global array");
        }
    }
    $('#vitals-measures-content').empty();
    navigate("#vitals-measures");
    for (var i = 0;i < vitalsValuesChecked.length;i++) {
        console.log("vitalsValuesChecked at index " + i + " is " + vitalsValuesChecked[i]);
     }
        readRec('clinicalObservation', null, sortVitalsByResult);

    });
function foobar() {
    console.log(vitalsValuesChecked); //return []
    for (var i=0;i < vitalsValuesChecked.length;i++) {
        var valueSelected = vitalsValuesChecked[i];
        console.log("Value of vitalsValuesChecked at index " + i + " is " + vitalsValuesChecked[i]);
    }
}
  • 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-06-09T18:27:09+00:00Added an answer on June 9, 2026 at 6:27 pm

    You have defined vitalsValuesChecked twice which is a problem. One is global and one is local to the delegate() callback. The local definition overrides the global definition so when you thought you were setting values into the global variable, you were not – you were just changing the local variable which has a limited lifetime and thus your data was not available later in the global variable.

    You should remove the

    var vitalsValuesChecked = [];
    

    inside the delegate handler so all modifications occur on the single global variable.

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

Sidebar

Related Questions

Outside of a for I declare a variable using var list; . I use
In Javascript, if I declare variables with var in javascript outside of a function
Possible Duplicate: Declaring a global variable inside a function Is it possible to declare
So when I declare a variable outside the scope of any function, it becomes
Is there any difference between a variable declared as static outside any function between
i declare a global variable and use and modify its value in the function.
When I declare a enum variable like this: enum paint_colors { RED, GREEN, BLUE,
I need to declare 3d array variable but can't. int[][][] ary = new int[5][2][];
I'm trying to declare a global variable from within a class like so: class
Normally, I've seen prototype functions declared outside the class definition, like this: function Container(param)

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.