8 chapters | 36 lessons
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.''
var flowers = [''Rose'', ''Lily'', ''Camellia'', ''Sunflower''];
var x = flowers.toString();
Rose, Lily, Camellia, Sunflower
|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.
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();
[Where are you?]
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,?]
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);
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"]
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'');
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;
It returns the array length or the total number of array elements.
var flowers = [''Rose'', '`'Lily'', ''Camellia'', ''Sunflower''];
var x = flowers.length;
|start||Mandatory Parameter. It specifies the position from where to begin the string extraction. The first character is always at index 0.|
|end||Optional Parameter. It specifies the position where to stop the string extraction. If left unmentioned, the extraction ends at the end of the string.|
var greeting = ''Good Morning'';
var result1 = greeting.substring(3); //begins extraction from 3rd position
var result2 = greeting.substring(2,6); //begins extraction from second position and ends at sixth position but doesn't include it
In the string ''Good Morning'' the position value starts from 0, i.e. G is at position 0, o at position 1 and so on. Even the space between ''Good'' and ''Morning'' is treated as an individual character.
To unlock this lesson you must be a Study.com Member.
Create your account
Already a member? Log InBack
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
Not sure what college you want to attend yet? Study.com 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.
8 chapters | 36 lessons