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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:29:36+00:00 2026-06-12T19:29:36+00:00

While learning JavaScript, I did not get why the output when we print the

  • 0

While learning JavaScript, I did not get why the output when we print the array returned of the Sting.split() method (with regular expression as an argument) is as explained below.

var colorString = "red,blue,green,yellow";
var colors = colorString.split(/[^\,]+/);
document.write(colors); //this print 7 times comma: ,,,,,,,

However when I print individual element of the array colors, it prints an empty string, three commas and an empty string:

 document.write(colors[0]);  //empty string
 document.write(colors[1]);  //,
 document.write(colors[2]);  //,
 document.write(colors[3]);  //,
 document.write(colors[4]);  //empty string
 document.write(colors[5]);  //undefined
 document.write(colors[6]);  //undefined

Then, why printing the array directly gives seven commas.

Though I think its correct to have three commas in the second output, I did not get why there is a starting (at index 0) and ending empty string (at index 4).

Please explain I am screwed up here.

  • 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-12T19:29:38+00:00Added an answer on June 12, 2026 at 7:29 pm

    /[^\,]+/ splits on one or more characters that are not a comma. Thus, JavaScript will split your string on red, blue etc. The resulting leftovers, then, are the empty string at the beginning (the substring from index 0 to 0), the commas, and the empty string at the end. If you go out of bounds of the array you get undefined (as with any array).

    red,blue,green,yellow
    xxx xxxx xxxxx xxxxxx   <-- x is what is being eaten during split, because it's the delimiter
    

    You just want .split(","), which splits on commas, so that the commas are eaten and you are left with the colors.

    Now, when you do document.write(someArray), the array is converted into a string so that it can be displayed. This effectively means someArray.join() is called, which by default puts commas in between. So you get commas joined by commas, resulting in even more commas.

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

Sidebar

Related Questions

So, I am learning Javascript while playing white Google Calendar APIs and I just
I started learning JavaScript a while ago. It's a fairly easy programming language considering
i've started learning about javascript closures, and while experimenting, i realised that the following
I've been reading up on the Prototype source code while learning Javascript. I've been
(I am Javascript Beginner) While I am learning Javascript global variable's life time, it
I'm learning JavaScript and while browsing through the jQuery library I see : (colon)
I have a C/C++ programming background, and while casually learning Javascript through the @3C
While watching Learning to Love JavaScript, I got hung up on why this 2nd
This may be very convoluted and twisted idea. Got it while learning some JavaScript.
This is really for informational and learning purposes while learning more about JavaScript and

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.