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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:56:19+00:00 2026-06-16T22:56:19+00:00

I read a lot of posts and ask/answer about javascript anonymous self-executing functions, but

  • 0

I read a lot of posts and ask/answer about javascript anonymous self-executing functions, but I’m afraid I’m still missing the point. Why does this code show myvar value? Shouldn’t the construct (function(){ code })() keep all variables not visible from outside?

(function(){
    myvar = 5;
})();

alert(myvar);

so what’s the difference betwen above code and

function myfunction(){
    myvar = 5;
};
myfunction();

alert(myvar);

?

  • 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-16T22:56:21+00:00Added an answer on June 16, 2026 at 10:56 pm

    Variables are scoped at the function level in javascript. What this means is if you declare a variable inside a loop or an if statement, it will be available to the entire function.

    In your case, your myVar is available to the whole window as somebody pointed out already.

    If you need to explicitly cage your variable inside a function scope, create an anonymous function and then execute it straight away and all the variables inside will be scoped to the function

    (function(){
        var myvar = 5;
    })();
    
    
    alert(typeof(myVar));
    

    results “undefined”

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

Sidebar

Related Questions

Ok, I have read a lot of posts and resources about this but I
Hey, There's a lot of posts on here about read barcodes but I want
I read a lot of posts about the problem I have, but haven't been
I read the Sun documentation and a lot of posts on Stack Overflow, but
I've read a lot of the posts here giving profiling advice but I need
I've read a lot of posts about inserting a DataTable into a SQL table,
I've read a lot of posts about using threads, subprocesses, etc.. A lot of
I have already read a lot of posts around this subject but I am
I have read quite a lot of posts here and elsewhere, but I can't
I've read a lot of similar posts but after two days, I thought I

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.