The split()
method divides a String
into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method’s call.
Syntax
split()
split(separator)
split(separator, limit)
Return value
An Array
of strings, split at each point where the separator
occurs in the given string.
const str = 'Ercan OPAK'; const words = str.split(' '); console.log(words) // output: "Ercan, OPAK" console.log(words[0]); // output: "Ercan" console.log(words[1]); // output: "OPAK" const chars = str.split(''); console.log(chars) // output: "E, r, c, a, n, O, P, A, K" console.log(chars[2]); // expected output: "c" const strCopy = str.split(); console.log(strCopy); // output: Array ["Ercan OPAK"]
The join()
method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.
Syntax
join()
join(separator)
Return value
A string with all array elements joined. If arr.length
is 0
, the empty string is returned.
const elements = ['Ercan', 'OPAK', 'com']; console.log(elements.join()); // output: "Ercan, OPAK, com" console.log(elements.join('')); // output: "ErcanOPAKcom" console.log(elements.join('.')); // output: "Ercan.OPAK.com" console.log(elements.join('-')); // output: "Ercan-OPAK-com"