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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:18:59+00:00 2026-06-18T07:18:59+00:00

Is it possible to declare two variables in the initialization part of a for

  • 0

Is it possible to declare two variables in the initialization part of a for loop? I want to call a function on each character of a string.

for(var i = 0, c = aString.charAt(i); i < aString.length; i++){//problem here: not itterating
    alert("c: "+c)
    func1[typeOfChar(c)]++
}

The problem is the string isn’t being itterated in the sense c is always the first letter of the string.
The alert was just for trouble shooting purposes, by the way.

I’m curious, how come c doesn’t need the var keyword when being declared?

UPDATE: got it working. I wasn’t going to ask but I notice edits are still being made, I’m used to not using the semi-colons as they are optional. How can a for loop be written without them? I don’t add them because I see it as the less the simpler, or do they improve readability?

  • 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-18T07:19:00+00:00Added an answer on June 18, 2026 at 7:19 am

    You’d like c to change at every iteration, not to declare it at the start of the loop, try

    var i,c;
    for(i = 0,c=aString.charAt(0); i < aString.length; ++i, c = aString.charAt(i)){
        alert("c: "+c)
        func1[typeOfChar(c)]++
    }
    

    For what it’s worth I don’t think it makes very readable code, I would put it in the first line.

    Here is some information on the comma operator you’re using.

    Also note that javascript has no block scoping for for loops, so you’re actually declaring i and c at the top of the current scope (this is usually the top of the current function, or the top of the global scope).

    Here is a fiddle: http://jsfiddle.net/maWua/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two questions. I know it is possible to declare class objects in
Possible Duplicate: Cannot declare Public static final String s = new String(“123”) inside an
Possible Duplicate: Global Variables in Cocoa/Objective-C? problem with declare a global variable in objective
Is it possible to dynamically declare arrays or variables after compilation? I ask because
Possible Duplicate: Properties and Instance Variables in Objective-C 2.0 I'm confused by these two
Possible Duplicate: Is it possible to declare an array as constant Is it possible
Is it possible to declare more than one variable using a with statement in
is it possible to declare more than 1 block in a module? I tried
In Java, is it possible to declare a field/variable whose type is multiple interfaces?
Is it possible to forward-declare a nested class, then use it as the type

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.