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

  • Home
  • SEARCH
  • 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 870965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:33:56+00:00 2026-05-15T10:33:56+00:00

What is the difference between these? var a = 13; this.b = 21; document.write(a);

  • 0

What is the difference between these?

var a = 13;  
this.b = 21;  
document.write(a);  
document.write(b);
  • 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-15T10:33:57+00:00Added an answer on May 15, 2026 at 10:33 am

    For global code (code that is not part of any function), they are almost equivalent, both at the end create a property on the global object.

    The difference is that a, which has been declared with the var statement, the Variable Instantiation process will use the global object as the Variable Object (1), and it will define that property as non-deleteable on it, e.g.:

    var a = 13;
    delete a; // false
    typeof a; // "number"
    

    Then, b since the this value in global code, points to the global object itself, will be also a global property, but this one can be deleted:

    this.b = 21;
    delete b; // true
    typeof b; // "undefined"
    

    Don’t try the first snippet in Firebug, since the Firebug’s console runs the code internally with eval, and in this execution context the variable instantiation process behaves differently, you can try it here.

    (1) The Variable Object (VO) is an object that is used by the variable instantiation process to define the identifiers of FunctionDeclarations, identifiers declared with the var statements, and identifiers of function formal parameters, in the different execution contexts, all those identifiers are bound as properties of the VO, the Scope chain is formed of a list of VO’s.

    For global code, the VO is the global object itself, that’s why a ends being a property of it. For function code, the VO (also known as the Activation Object for FunctionCode), is a new object is created behind the scenes when you invoke a function, and that is what creates a new lexical scope, in short I’ll talk about functions.

    Both a and this.b can be resolved simply as by a and b because the first object in the scope chain, is again the global object.

    Also, I think is work knowing that the Variable Instantiation process takes place before than the code execution, for example:

    alert(a); // undefined, it exists but has no value assigned to it yet
    alert(b); // ReferenceError is thrown
    
    var a = 13;
    this.b = 21;
    

    These differences may be trivial but I think is worth knowing them.

    Now, if the code snippet you posted is within a function, it is completely different.

    The a identifier, declared with the var statement in your example will be a local variable, available only to the lexical scope of the function (and any nested functions).

    Keep in mind that in JavaScript blocks don’t introduce a new scope, only functions do, and to declare a variable in that scope, you should always use var.

    The this.b identifier will become a property bound to the object that is referred by the this value, but… What is this???.

    The this value in JavaScript is implicitly set when you call a function, it is determined by how do you invoke it:

    1. When you use the new operator, the this value inside the function, will point to a newly created object, e.g.:

      function Test() {
        this.foo = "bar";
      }
      var obj = new Test(); // a new object with a `foo` property
      
    2. When you call a function that is member of an object, the this value inside that function will point to the base object, e.g.:

      var obj = {
        foo: function () {
          return this == obj;
        }
      };
      obj.foo(); // true
      
    3. When you invoke a function without any base object, the this value will refer to the global object:

      function test() {
        return this == window;
      }
      test(); // true
      
    4. The this value can be set explicitly, when you invoke a function using call or apply:

      function test() {
        alert(this);
      }
      test.call("hello world!"); // alerts "hello world!"
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any difference between these two: var test1 = function () { this.method1
Is there any difference between doing this: $(.topHorzNavLink).click(function() { var theHoverContainer = $(#hoverContainer); var
Is there a difference between the following selectors: var index = $(this).parent().index(); var index2
Is there any difference between these two LINQ statements: var query = from a
can someone please explain the difference between these 2 pieces of code: var temp
what are the difference between these two ways of creating a class: var apple
Is there a difference between this: var onClick = function() { var image =
What are the differences between these two codes in JavaScript? var obj = new
Are there any differences in functionality between these two JavaScript module patterns? var MODULE
Is there any difference in (asymptotic) performance between var a = Orders.OrderBy(order => order.Date).First()

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.