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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:56:53+00:00 2026-05-16T09:56:53+00:00

I’d like to know if standard JS provides a way of splitting a string

  • 0

I’d like to know if standard JS provides a way of splitting a string straight into a set of variables during their initial declaration. For example in Perl I would use:

my ($a, $b, $c) = split '-', $str;

In Firefox I can write

var [a, b, c] = str.split('-');

But this syntax is not part of the ECMAScript 5th edition and as such breaks in all other browsers. What I’m trying to do is avoid having to write:

var array = str.split('-');
var a = array[0];
var b = array[1];
var c = array[2];

Because for the code that I’m writing at the moment such a method would be a real pain, I’m creating 20 variables from 7 different splits and don’t want to have to use such a verbose method.

Does anyone know of an elegant way to do this?

  • 1 1 Answer
  • 1 View
  • 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-05-16T09:56:53+00:00Added an answer on May 16, 2026 at 9:56 am

    You can only do it slightly more elegantly by omitting the var keyword for each variable and separating the expressions by commas:

    var array = str.split('-'),
        a = array[0], b = array[1], c = array[2];
    

    ES6 standardises destructuring assignment, which allows you to do what Firefox has supported for quite a while now:

    var [a, b, c] = str.split('-');
    

    You can check browser support using Kangax’s compatibility table.

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

Sidebar

Related Questions

I want to know how can i format a number like 2000000 into standard
I would like to know if in C++ standard libraries there is any gaussian
I would like to know why designers of the URI standard chose to have
I would like to know is there any way to generate xml schema from
I would like to know if there are any xml coding standards. <?xml version=1.0?>
I'd like know how do to a method return a JSON array of List,
i would like know some reference. I know i can googling it. but prefer
Like you know, .NET Remoting has some limits, one of them is that server
Would like to know what a programmer should know to become a good at
I like to know why dont we call servlet constructor instead of init method

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.