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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:49:34+00:00 2026-06-12T01:49:34+00:00

In the following Java snippet, the scope of i is limited to the inside

  • 0

In the following Java snippet, the scope of i is limited to the inside of the for loop. That’s why it causes an error. However, in the similar JS snippet, the i is apparently accessible outside of the loop. How is that possible?

Java:

for(int i=0;i<10;i++) {
    ...
}
System.out.println(i);

Output: “i is not defined”

JS:

for(var i=0;i<10;i++) { 
    ...
}
console.log(i);

Output: 10

I didn’t expect to see output from the JS. I want to know how JS differs from Java. How does JavaScript variable scope work?

  • 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-06-12T01:49:35+00:00Added an answer on June 12, 2026 at 1:49 am

    In Javascript “local” variables have function scope, not block scope.

    All local variable declarations are “hoisted” to the top of the current scope, so your code is equivalent to:

    var i;
    for (i = 0; i < 10; ++i) {
    }
    console.log(i);
    

    Note that while the declaration is hoisted, any assignment is not. e.g. this code

    function test() {
        console.log(i);  // undefined
        var i = 1;       // declaration and assignment
        console.log(i);  // 1
    }
    

    is equivalent to:

    function test() {
        var i;           // declaration hoisted
        console.log(i);  // undefined
        i = 1;           // assignment still happens here
        console.log(i);  // 1
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following snippet from RuntimeUtil.java from jlibs guarantees GC that garbage collection is done.
Consider the following code snippet in Java. I know that the statement temp[index] =
The following code snippet causes error....the logcat has been attached...I am tring to pass
I was surprised to see that the following Java code snippet compiled and ran:
I'm trying to convert following java snippet to Xtend, but i couldn't able to
I have the following snippet of java code: File directoryToMoveTo = new File(file.getParent()+_TEMP); boolean
I need the java code snippet for the below logic: Following is a string
Let's look at the following code snippet in Java. package division; import java.math.BigDecimal; final
The following Java code is throwing a compiler error: if ( checkGameTitle(currGame) ) ArrayList<String>
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]

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.