Array to String in JavaScript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Initially, JavaScript was used only to make web interfaces, but nowadays, JavaScript is one of the most popular languages used in multiple arenas. Array and String are two of JavaScript's most widely used data containers.

The array in JavaScript holds multiple elements in a single variable. The string is used to hold the series of characters. We often encounter a situation where we need to convert an array to a string in JavaScript. There is a method in JavaScript known as toString(), which is used for this purpose.

Converting an Array to String in Javascript (Using toString() Method)

In order to convert an array into a string in Javascript, we simply apply the toString() method on the given array, and we get a stringified version of our array. Internally javascript first converts each element into string and then concretes them to return the final string.

Let's look into some widely used conversions.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Converting an Array of Numbers into a String in Javascript

The code for converting the array of numbers to string will be as follows :

Output :

As you can notice in the output that the square brackets are replaced by quotation marks. This indicates that the given array is converted into a string.

Converting an Array of Strings into Strings in Javascript

The code for converting the array of strings to strings will be as follows :

Output :

Converting Mix Arrays (both numbers and string) into Strings in Javascript

The code to convert array to string javascript is as follows :

Output :

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Converting String Back to the Array In Javascript

There is no direct method present to do so. But if we take a closer look at the given scenario, then we will notice that the string contains all the elements of the array, and if we simply combine all the given elements into a single array, then we can get our array back.

Do you know any method in javascript that can be used in this case?

There is a method in javascript known as split() which splits a string into an array of substrings. The parameter that we pass in the split function is used to split the string. Thus if we pass ',' as a parameter to our split method, then we will get our required array.

Now let's look the code for above scenario :

Output :

As you can see integer values are also converted into the string. This is how the split method works, it cannot detect the previous type of string.

Working with Nested Arrays

There might be a situation when we have an array inside an array; such arrays are known as nested arrays. How to convert such an array into a string? toString() flattens the array. Thus all the elements of the array, including the nested array elements, get combined into a single array. And that's how we get the new array having all the elements separated by a comma.

Let's look into the code for this:

Output :

In the above example, the toString method flattens the array, and all the elements of the array, including the nested array elements, get combined into a single array.

Elevate your coding skills with our JavaScript course. Enroll today and become a proficient JavaScript developer!

Conclusion

  • We can convert an array of numbers and an array of strings into strings using toString().
  • We can also convert the nested array into string using toString().
  • And in order to convert the string back to the array, we will use the split() method.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more