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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:11:09+00:00 2026-05-28T13:11:09+00:00

So I hear that variable in js that initialized without a var will be

  • 0

So I hear that variable in js that initialized without a “var” will be global. so:

$(document).ready(function(){
      function foo(){
         //since i'm not using "var", will this function become global?
      }

      var bar = function(){
         //is this the better way to declare the function?
      }
})

If it is global, why I can’t access it in the console. If it’s not global, and it’s scope is in the function, will omitting “var” cost some performances? Thanks.

  • 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-28T13:11:09+00:00Added an answer on May 28, 2026 at 1:11 pm

    Only variables declared without var become global, this does not apply to functions.

    You can, however, declare foo like so:

    foo = function() {}
    

    and it should be global.

    Omitting var is generally not recommended for these reasons (off the top of head):

    • Variable resolution starts at the most local and goes toward looking in the global namespace, making it slower. Much slower in certain browsers.
    • You tend to eventually have naming conflicts by polluting the global namespace. One of the worst offenders would be, say, for(i = 0; i < arr.length; i++) (note the lack of var)

    You might want to declare functions using var due to a language feature called hoisting

    BTW, if you do choose to declare functions with var, I recommend you do so this way:

    var foo = function foo() {}
    

    because it gives the function a “name” instead of being treated as an anonymous function, which will help with debugging. Most people do not do this and declare using function, I believe.

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

Sidebar

Related Questions

It's rare that I hear someone using Inversion of Control (Ioc) principle with .Net.
I'm writing a function that I need to use either a TABLE variable for
I hear that tr1::result_of gets used frequently inside of Boost... I'm wondering if there
i hear that NTFS alternate data streams can be used to hide running executabes.
I always hear that programmers try to pick the right tool for the job.
You always hear that functional code is inherently easier to parallelize than non-functional code,
I always hear that Java being open-source is a big benefit, but I fail
We all hear that math at least helps a little bit with programming. My
We have a Prepared Statement in Java and we hear that it is different
I created an iPhone application for the App Store. I hear that some things

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.