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 8592521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:46:11+00:00 2026-06-11T23:46:11+00:00

I have the following bit of code: (function(){ … var n = n ||

  • 0

I have the following bit of code:

(function(){
...
var n = n || {};
...
})(); 

This does what I want, setting n equal to {} since this is the first time it’s encountered and will be undefined. Unfortunately, since this inside a function, n is limited in scope to the function and I can’t use it in other scripts.

I wanted to just change the line to n = n || {}, but I got an error: ReferenceError: n is not defined

Changing it to just n = {} worked as expected; however, this is not what I want. I don’t get why n‘s undefinedness causes an error when I don’t use var and works as expected (being a falsey value used to get the right side of the OR statement) when I do. Based on my understanding of the var keyword, I would expect it to be both or neither.

Why does the var matter, what is going on here?

  • 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-11T23:46:12+00:00Added an answer on June 11, 2026 at 11:46 pm

    So it sounds like you’re having trouble creating a global variable.

    Good.

    Creating global variables in JavaScript is easier than it should be.

    Your problem is that n is not defined (duh). You can’t reference a variable that’s not yet defined. But you’re probably thinking, “How is that different from the version using var???” And that’s a good question.

    JavaScript does something called “hoisting” of variable definitions. If you define a variable within a function, that definition gets implicitly relocated to the top of the function. So while you wrote:

    var n = n || {};
    

    What really happened was closer to:

    var n;
    n = n || {};
    

    However, it’s not really that simple, because if you tried to write that code, you’d wind up with n always being set to {}. But the general essence is there. The variable declaration happens before the assignment.

    If you remove the var, it will cause a reference error because there’s no declaration to hoist anymore. So one “proper” (and I use that term loosely, because it’s never “proper” to create a global variable) way to do what you want is to take your var and place it outside your function wrapper. Like so:

    var n = n || {};
    (function () {
        //do stuff.
    }()); 
    

    Unfortunately, you can’t do it like this:

    var n; 
    (function () {
        n = n || {};
    }());
    

    That has the same “problem” as my example above. If n is defined elsewhere, it will be set to undefined by the var n; and then set to {} in the function. By doing the whole declaration and assignment outside the function, you get what you’re looking for. Which I assume (based on the title of your question) is actually namespacing and not some arbitrary global variable. That would be naughty! 😉

    UPDATE:

    Oh, and by the way, a better way to do this might be by explicitly referencing the global object:

    (function (exports) {
        exports.n = exports.n || {};
    }(this));
    

    This will probably play better with things like node.js and anything that might wrap your code (like a $(function() { })).

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

Sidebar

Related Questions

I have the following bit of code: protected function onEnterFrame(e:Event):void { var diff:Number; //
I have the following bit of code, simply: $(function() { $('a.add-photos-link').live('click', function(e) { $(this).colorbox({
I have problems with the following bit of javascript/jquery code: this.droppable = function(){ $('.imageWindow
All, Say I have the following bit of code: select: function(start, end, allDay) {
I have the following code for example $(a.foo).bind(function (e){ var t; if ( $(e.target).is(a)
Im my application I have the following code, the first bit runs when my
All, I have the following bit of code: function addPoints() { newpoints[0] = new
Using the following bit of code: function Node(){ .... function foo(request){ for (var name
My problem is a little bit complex. I have the following code: $(document).ready(function() {
Possible Duplicate: What does this mean? (function (x,y)){…}){a,b); in JavaScript I have the following

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.