Arrays & Strings in JavaScript: Conversion & String Methods

Instructor: Vignesh Sivabalan
JavaScript arrays can be converted to strings and vice versa. In this lesson, you will learn the basic array and string conversions, length property of strings and the substring() method.

JavaScript Strings

JavaScript arrays store a collection of characters, for example ''Jack Davidson.'' A string can be any kind of text that is enclosed in single or double quotes.


var studentName1 = ''Asha'';
var studentName2 = ''Mike'';

Here, var is a keyword representing variables like numbers, text, etc. The variable names are studentName1 and studentName2 and the values of the variables are texts, ''Asha'' and ''Mike.''

Conversion of Arrays to Strings in JavaScript

The toString() JavaScript method converts an array to a string and returns the desired result. The various array elements are represented in the returned string, separated by commas.




var flowers = [''Rose'', ''Lily'', ''Camellia'', ''Sunflower''];
var x = flowers.toString();


Rose, Lily, Camellia, Sunflower

Conversion of Strings to Arrays Using the split() Method

The JavaScript method, split(), is used for splitting a string into a new array of substrings and returns the new array. The resultant array is based on the separator (delimiter) you specify.

The split() JavaScript method never changes the original string.


string.split(separator, limit);

Parameters Explanation
separator This parameter denotes the regular expression or the characters that are used for splitting strings. It is optional and if omitted, an entire string gets returned.
limit This is an integer value that represents split numbers. It is also optional.

By varying the arguments in the split() method, the resultant array can be varied. Let us consider few example for better understanding.

Example 1:

The ''separator'' parameter is omitted in the split() method. When the separator parameter is omitted, the entire string is returned in the resultant array.

var string1 = ''Where are you?'';
var result = string1.split();

Output :

[Where are you?]

Example 2:

The separator parameter is an empty string, '' ''. When an empty string '' '' is used as a separator parameter, it splits each character of the string using the separator '',''.

var string2 = ''How are you?'';
var result = string2.split('' '');


[H,o,w, ,a,r,e, ,y,o,u,?]

Example 3:

The second parameter of split method, limit is used. It sets a particular limit on the maximum number of elements that can be returned. It does not display the characters or strings after the given limit.

var greeting = ''I am fine thank you!'';
var result = greeting.split('' '', 3);



Example 4:

The separator is '',''. It splits the strings into array elements on the comma space.

var fruits = ''Banana'', ''Mango'', ''Apple'';
var result = fruits.split('','');


["Banana", "Mango", "Apple"]

Example 5:

Any letter or a symbol can also be used as a separator. Let us consider an example where the alphabet ''o'' is used as a separator. Whenever the alphabet ''o'' is encountered, the string is split into a new array element.

var samplestring = ''HelloWorld'';
var result = samplestring.split(''o'');


["Hell", "W","rld"];

length Property in JavaScript Strings

The length property returns the total number of string characters i.e., the length of a string.




var string1 = ''Hi John!'';
var x = string1.length;




The length property for a JavaScript string returns the number of characters in a string. Even the space and special characters are counted.

length Property in JavaScript Arrays

It returns the array length or the total number of array elements.



To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account