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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:55:52+00:00 2026-06-17T05:55:52+00:00

In a JavaScript environment can I declare a variable before a function to make

  • 0

In a JavaScript environment can I declare a variable before a function to make the variable reachable on a global level.
For instance:

var a;
function something(){
  a = Math.random()
}

Will this make “a” a global variable?

or is using…

var a = function(){
  var b = Math.random();
  return b;
}
document.write(a())

Really the only way to do it?

Is there a way to make “b” global other than calling the function “a()”?

  • 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-17T05:55:53+00:00Added an answer on June 17, 2026 at 5:55 am

    There are basically 3 ways to declare a global variable:

    1. Declaring it in the global scope, outside of any function scope.
    2. Explicitly assigning it as a property of the window object: window.a = 'foo'.
    3. Not declaring it at all (not recommended). If you omit the var keyword when you first use the variable, it’ll be declared globally no matter where in your code that happens.

    Note #1: When in strict mode, you’ll get an error if you don’t declare your variable (as in #3 above).

    Note #2: Using the window object to assign a global variable (as in #2 above) works fine in a browser environment, but might not work in other implementations (such as nodejs), since the global scope there is not a window object. If you’re using a different environment and want to explicitly assign your global variables, you’ll have to be aware of what the global object is called.

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

Sidebar

Related Questions

How can I create an environment in client-side javascript where any code related to
javascript code: function getCheckbox(name,id){ var check = 0; var deger = option-checkbox-+id; var dom
JavaScript/JQuery var arr=[]; $('.element').each(function(i) { arr.push({id:i,value:$(this).attr('data-value')}); }); $.get('/json.php?arr='+arr,function(result) { alert(result); }); PHP <?php $j
JavaScript: $('.addTable').live('click', function () { var appendTxt = <table id='tab1'> <tr><td><input type='text' name='input1' /></td><td><input
Is there a decent browser-based javascript self-editor? It is obvious one can make a
how can i call a VB function - deleteevent() in usercontrol.ascx.vb from a javascript
I am new to JavaScript environment.I have read that <script> </script> tag can be
When I run javascript script file in windows command line environment, and there is
Javascript is pretty shaky for me, and I can't seem to find the answer
javascript with> var customer=document.getElementById('custList').value; and that works... Why does it work BUT... var customer=(form1.custList.value);

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.