> ## 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 If Statement

The `if` statement is a fundamental control flow structure in JavaScript that executes a specific statement or block of code only when its associated condition evaluates to a truthy value. It forces the JavaScript engine to evaluate an expression in a boolean context, dictating the execution path of the program based on that evaluation.

## Syntax

```javascript theme={"dark"}
if (condition) {
  // Statement(s) executed if condition is truthy
} else if (alternativeCondition) {
  // Statement(s) executed if alternativeCondition is truthy
} else {
  // Statement(s) executed if all preceding conditions are falsy
}
```

## Evaluation Mechanics

**Implicit Boolean Coercion**
JavaScript does not require the `condition` expression to be a strict boolean type (`true` or `false`). The engine applies the internal `ToBoolean` abstract operation to the evaluated expression.

**Falsy Values**
If the condition resolves to any of the following strictly defined falsy values, the `if` block is bypassed:

* `false`
* `0` and `-0`
* `0n` (BigInt zero)
* `""`, `''`, ` `` ` (empty strings)
* `null`
* `undefined`
* `NaN` (Not a Number)
* `document.all` (a legacy web-specific host object explicitly defined as falsy by the ECMAScript specification via the `[[IsHTMLDDA]]` internal slot)

**Truthy Values**
Any value that is not in the falsy list is considered truthy. This includes structures that developers sometimes mistakenly assume are falsy, such as empty objects (`{}`), empty arrays (`[]`), and strings containing only whitespace (`" "`).

## Execution Behavior

**Branching and Short-Circuiting**
In an `if...else if...else` chain, the JavaScript engine evaluates conditions sequentially from top to bottom. The moment a condition evaluates to a truthy value, its corresponding block is executed. All subsequent `else if` or `else` blocks are entirely skipped; their conditions are not evaluated.

**Block vs. Single Statement**
The curly braces `{}` define a block statement. If the execution body consists of only a single statement, the braces are syntactically optional:

```javascript theme={"dark"}
if (true) console.log("executed");
```

However, omitting braces is generally discouraged in modern JavaScript style guides, as it can lead to structural ambiguity and errors during refactoring.

## Lexical Scoping

The block statement `{}` associated with an `if` clause creates a new lexical scope.

* Variables declared with `let` and `const` inside the block are strictly bound to that block and cannot be accessed from the outer scope.
* Variables declared with the legacy `var` keyword ignore block scope entirely; they are hoisted and bound to the enclosing function or global scope.

```javascript theme={"dark"}
if (true) {
  const blockScoped = "isolated";
  var functionScoped = "accessible";
}

// console.log(blockScoped); // ReferenceError
// console.log(functionScoped); // "accessible"
```

<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>
