> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript var Declaration

The `var` statement declares a function-scoped, module-scoped, or globally-scoped variable, optionally initializing it to a value. It operates independently of block constructs and is processed during the creation phase of the JavaScript execution context.

## Syntax

```javascript theme={"dark"}
var identifier;
var identifier = expression;
var identifier1 = expression1, identifier2 = expression2;
```

## Scope Semantics

Variables declared with `var` are bound to the nearest enclosing function execution context. If declared outside of any function, their binding depends on the execution environment: in traditional scripts, they are bound to the global execution context, whereas in ES6 modules or Node.js CommonJS modules, they are bound to the module execution context.

* **Function Scope:** A `var` declared within a function is inaccessible outside of that function's lexical environment.
* **Module Scope:** A top-level `var` declared within an ES module or CommonJS module is scoped strictly to that module's environment record, not the global environment.
* **Lack of Block Scope:** Unlike `let` and `const`, `var` ignores block boundaries (such as those created by `if`, `for`, or `while` statements). A `var` declared inside a block is scoped to the containing function, module, or global environment.

```javascript theme={"dark"}
function scopeExample() {
    if (true) {
        var x = 10; 
    }
    console.log(x); // Outputs: 10 (x is scoped to scopeExample, not the block)
}
```

## Hoisting and Initialization

During the compilation phase, JavaScript engines hoist `var` declarations to the top of their enclosing function, module, or global scope.

* **Declaration Hoisting:** The identifier is registered in the environment record before any code is executed.
* **Default Initialization:** Hoisted `var` variables are automatically initialized with the primitive value `undefined`. The actual assignment expression remains in its original location and is evaluated during the execution phase.

```javascript theme={"dark"}
console.log(a); // Outputs: undefined (ReferenceError is not thrown)
var a = 5;
console.log(a); // Outputs: 5
```

## Redeclaration

The JavaScript engine permits multiple `var` declarations with the same identifier within the same scope. Subsequent declarations are treated as re-assignments if they include an initializer; otherwise, the duplicate declaration is ignored. Strict mode (`"use strict"`) does not prevent `var` redeclaration.

```javascript theme={"dark"}
var b = 1;
var b = 2; // Valid syntax, overwrites previous value
var b;     // Ignored, b remains 2
```

## Global Object Binding and Modules

When executed in a traditional global script scope, a top-level `var` declaration creates a property on the global environment object (e.g., `window` in browsers, `global` in Node.js). This property is created with its `[[Configurable]]` attribute set to `false`, meaning it cannot be deleted via the `delete` operator.

Crucially, this behavior does *not* apply to ES modules or Node.js CommonJS modules. Top-level `var` declarations inside a module do not create properties on the global object.

```javascript theme={"dark"}
// In a traditional browser script:
var c = 100;
console.log(window.c); // Outputs: 100

// In an ES module (<script type="module">) or Node.js module:
var d = 200;
console.log(globalThis.d); // Outputs: undefined
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor JavaScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
