0

I need to save data in a global variable, so that I can use it from different function of application.

Here is the code I tried.

var userDetails=[];

function getDetails(){

    var firstname=document.getElementById('firstname').value;
    var lastname=document.getElementById('lastname').value;
    var email=document.getElementById('email').value;
    var password=document.getElementById('password').value;

    var item = {}
    item ["firstname"] = firstname;
    item ["lastname"] = lastname;
    item ["email"] = email;
    item ["password"] = password;

    userDetails.push(item);
}

The problem is when I am trying to access it from a different function, then I am getting userDetails as undefined.

I am accessing the userDetails from another function after executing getDetails().

UPDATE I am trying to access userDetails from the below function. NOTE: I am calling getDetails on SignUp button, and for the login I am calling this.

function login(){

var userName=document.getElementById('loginUserName').value;

var password=document.getElementById('loginPassword').value;

console.log(userDetails);  // here i need userDetails to check if this user has signed up and credential are correct.

}
  • var userDetails=[]; replace this with userDetails=[];. Just remove the var – Jitendra Khatri Dec 22 '15 at 13:49
  • 3
    @JitendraKhatri no, really, please, don't. Global variables are bad. There's almost always a way to encapsulate all state within a closure, or if using multiple files by using the module pattern. – Alnitak Dec 22 '15 at 13:51
  • You should read up on this thread, Rajesh: http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript and @Alnitak, somebody learning javascript might not realize that. You really should include at least a little bit of an explanation or a link. Saying something is bad isn't going to dissuade anybody. – Marie Dec 22 '15 at 13:53
  • i think your script with the variable definition is loaded too late – messerbill Dec 22 '15 at 13:55
  • @ToniW that comment wasn't at the OP, it was sent to someone who calls himself a "software engineer". In any event, I've added my own answer now. – Alnitak Dec 22 '15 at 14:00
  • @Alnitak Oh, Sorry. I missed the username reference. Regardless, The user asked how to do something and Jit told them. Granted, I think Jit should have mentioned that it isn't good practice. – Marie Dec 22 '15 at 14:03
  • That code should work. The problem must be with the code you haven't shown us. You need to provide a [test case](http://sscce.org/) that actually demonstrates the problem. – Quentin Dec 22 '15 at 15:24
  • @Quentin I have updated the question with problem statement – Rajesh Kumar Dec 23 '15 at 06:31
  • @RajeshKumar — http://jsbin.com/notokufiqe/1/edit?html,js,console — It's an empty array, not undefined. You still haven't provided a test case that actually demonstrates your problem. – Quentin Dec 23 '15 at 09:40

1 Answers1

0

All JS variables should as far as possible be encapsulated within a closure. If there's only one script block there's no need for globals at all.

However, things are different if you need to share state across multiple source files (since closures are a lexical construct, that can't cross script file boundaries).

A common way to minimize leakage of variables into the globals is to create a single namespace variable that then contains all of the other state variables that you wish to maintain, e.g.:

var MYMODULE = MYMODULE || {};  // use or create the namespace

(function(module) {
    module.foo = 'bar';  // accessible outside as MYMODULE.foo

    // any other variables declared with 'var' are private to this scope
    var foo = 'bar';      // private

})(MYMODULE);   // 
Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • I'd appreciate an explanation for the downvote - an answer I gave explaining this module pattern (albeit for different reasons) a few years ago has hundreds of upvotes. – Alnitak Dec 22 '15 at 16:32