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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:10:43+00:00 2026-05-28T06:10:43+00:00

HTML: <script type=text/javascript> var x = overriden; </script> <script src=myjs.js></script> myjs.js: $(document).ready(function(){ var x

  • 0

HTML:

<script type="text/javascript">
  var x = "overriden";
</script>
<script src="myjs.js"></script>

myjs.js:

$(document).ready(function(){
  var x = x || "default val";
  alert(x); // this alerts "default val" and not "overriden"
});

For some reason, x is ending up as "default val" and not "overriden", even tho initially I’m setting it to "overriden" before I even include the script reference to myjs.js.

Any idea as to why this is happening? I’m trying to enable the hosting page to set an override for a variable that’s used in an included js file, otherwise use the default val.

  • 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-28T06:10:43+00:00Added an answer on May 28, 2026 at 6:10 am

    What you have after variable declaration hoisting is applied:

    var x;
    x = 5;
    
    $(document).ready(function(){
        var x;
        x = x || "default";
    });
    

    It looks at the closest x and sees it’s value is undefined which is a falsy value, so x gets set to "default".


    You would be fine if they were in the same scope, because the declarations are always hoisted above assignments so:

    var x = 5;
    
    var x = x || "default";
    

    Is actually just

    var x;
    
    x = 5;
    x = x || "default";
    

    This was suggested which is completely pointless:

    $(document).ready(function(){
        x = x || "default";
    });
    

    It will throw a ReferenceError if x is not defined.


    So either do the check in the same scope or do something like:

    $(document).ready(function(){
        var x = window.x || "default";
    });
    

    Invalid property reads don’t cause a ReferenceError but just return undefined instead.

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

Sidebar

Related Questions

My html: <script type=text/javascript> $(function() { $(#bt1).click(function() { var f = $(#form1); var formData
<html> <head></head> <body> <script type = text/javascript> var x = 5; var y =
<!DOCTYPE html> <html lang=en> <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8> <script src=http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js type=text/javascript></script> <script type=text/javascript>
look at this code, <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8 /> <script> function change() {
New Google Analytics code looks like one below: <script type=text/javascript> var _gaq = _gaq
The JS script: function ShowUploadingAnimation() { $('#info_msg').html(<div style=\padding-top: 15px; height: 50px;\ align=\center\><img id=\uploading-img\ /></div>);
Using javascript, how to open a text-file or html-file from a web-page and load
I use this code, but javascript is not loading. var head = document.getElementsByTagName('head')[0]; var
I have made a js function for including another Javascript in my html. I
I have this function which is imported directly in the HTML function include(filename){ var

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.