Skip to content

Add additional examples about const, let, var and block scopes#26

Open
JuhQ wants to merge 2 commits into
ilkkamtk:mainfrom
JuhQ:patch-15
Open

Add additional examples about const, let, var and block scopes#26
JuhQ wants to merge 2 commits into
ilkkamtk:mainfrom
JuhQ:patch-15

Conversation

@JuhQ
Copy link
Copy Markdown
Contributor

@JuhQ JuhQ commented Nov 25, 2025

This pull request adds a new section to the vuorovaikutteiset_ohjelmat.md documentation that explains JavaScript variable scope, specifically the differences between let, const, and var. The new content uses clear analogies and examples to help readers understand block scope versus function scope.

Additions to documentation about variable scope:

  • Added a detailed explanation of block scope for let and const, including examples and analogies to clarify that variables declared with these keywords are only accessible within their enclosing block.
  • Added a section describing how var is function-scoped rather than block-scoped, with illustrative code samples showing its behavior inside and outside of functions.

Summary by Sourcery

Update JavaScript documentation for interactive programs with clearer examples and introduce an explanation of variable scoping.

Documentation:

  • Normalize formatting and indentation of JavaScript code examples to improve readability in the interactive programs guide.
  • Add a new documentation section explaining JavaScript block scope for let/const and function scope for var with illustrative examples.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Nov 25, 2025

Reviewer's Guide

This PR updates the JavaScript basics documentation by normalizing indentation in code samples and adding a new section that explains JavaScript variable scope, contrasting block-scoped let/const with function-scoped var using simple examples and explanations.

State diagram for variable visibility in block and function scopes

stateDiagram-v2
  [*] --> Global_scope

  state Global_scope {
    [*] --> No_variables
    No_variables --> Has_var_x: Declare_var_x_in_if_block
    Has_var_x --> Global_access_x_ok: Access_x_outside_if_block

    Has_var_x --> Call_test_function: Call_test
    Call_test_function --> Call_test_function: Reenter_test_if_called_again
    Call_test_function --> Global_access_y_error: Access_y_outside_function

    No_variables --> Enter_block_let_const: Enter_if_block_and_declare_let_const
    Enter_block_let_const --> Block_scope
  }

  state Block_scope {
    [*] --> Let_const_visible
    Let_const_visible --> Exit_block: Reach_closing_brace
    Exit_block --> [*]
  }

  Global_access_x_ok --> [*]
  Global_access_y_error --> [*]
  Enter_block_let_const --> Has_let_a_const_b
  Has_let_a_const_b --> Exit_block

  state Has_let_a_const_b {
    [*] --> Inside_block_a_b_defined
    Inside_block_a_b_defined --> [*]
  }
Loading

File-Level Changes

Change Details Files
Normalize JavaScript code examples by removing unnecessary leading indentation inside fenced code blocks so they copy/paste cleanly and render consistently.
  • Adjusted console.log and alert example snippets to start at column 0 inside ```javascript fences.
  • Updated variable declaration, string concatenation, prompt, arithmetic, Math, and strict-mode examples to use consistent left-aligned formatting.
  • Ensured multi-line examples keep logical structure while improving readability and consistency across the document.
vuorovaikutteiset_ohjelmat.md
Add a new documentation section introducing block scope for let/const and function scope for var with illustrative examples.
  • Introduced a subsection explaining block scope, describing blocks as code inside braces and showing that let/const variables declared in a block are not accessible outside it.
  • Added examples demonstrating that let and const are confined to their enclosing block, leading to reference errors when accessed outside.
  • Added a subsection for var explaining function scope, including examples where var escapes if/for blocks but remains confined within functions.
  • Clarified the conceptual difference between block scope and function scope using plain-language analogies and comments on expected runtime behavior.
vuorovaikutteiset_ohjelmat.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • The new section uses js code fences while the rest of the file uses javascript; consider standardizing the language tag for consistency in syntax highlighting.
  • You removed the leading indentation from many existing code blocks; please double-check that this matches the formatting conventions used in the rest of the documentation so code samples render consistently.
  • The use of emojis and symbols (✅, ❌) in the new block-scope section may not match the tone of the rest of the document; consider replacing them with plain-text descriptions if a more neutral style is preferred.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new section uses ```js code fences while the rest of the file uses ```javascript; consider standardizing the language tag for consistency in syntax highlighting.
- You removed the leading indentation from many existing code blocks; please double-check that this matches the formatting conventions used in the rest of the documentation so code samples render consistently.
- The use of emojis and symbols (✅, ❌) in the new block-scope section may not match the tone of the rest of the document; consider replacing them with plain-text descriptions if a more neutral style is preferred.

## Individual Comments

### Comment 1
<location> `vuorovaikutteiset_ohjelmat.md:289` </location>
<code_context>

 ```javascript
-     const name = prompt('Type your name.');
-     console.log('Noice to meet you, ' + name);
+const name = prompt('Type your name.');
+console.log('Noice to meet you, ' + name);
</code_context>

<issue_to_address>
**issue (typo):** Possible typo in greeting text: "Noice" vs "Nice".

The string "Noice to meet you" is likely a misspelling of "Nice to meet you". If it’s not intentional, please update it to improve clarity.

```suggestion
console.log('Nice to meet you, ' + name);
```
</issue_to_address>

### Comment 2
<location> `vuorovaikutteiset_ohjelmat.md:203` </location>
<code_context>

 The type of the variable can be tested with `typeOf`-operation:
 ```javascript
-        const name = 'Ahmed';
</code_context>

<issue_to_address>
**issue (typo):** Incorrect casing in `typeof` operator name.

Consider updating the sentence to: “The type of the variable can be tested with the `typeof` operator:” so the operator name matches JavaScript’s actual syntax.

```suggestion
The type of the variable can be tested with the `typeof` operator:
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

const name = prompt('Type your name.');
console.log('Noice to meet you, ' + name);
const name = prompt('Type your name.');
console.log('Noice to meet you, ' + name);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (typo): Possible typo in greeting text: "Noice" vs "Nice".

The string "Noice to meet you" is likely a misspelling of "Nice to meet you". If it’s not intentional, please update it to improve clarity.

Suggested change
console.log('Noice to meet you, ' + name);
console.log('Nice to meet you, ' + name);

Comment thread vuorovaikutteiset_ohjelmat.md Outdated
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant