JavaScript String split() Method
You are Here:
JavaScript String split() Method
The split()
method splits a string into an array of substrings, and returns the new array.
Note: This method does not alter the original string.
Split by Words
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var str = "Hello there my old friend";
var result = str.split(" ");
document.write(result);
</script>
</body>
</html>
Syntax
str.split(separator, limit)
Parameter Values
Value | Type | Explanation |
---|---|---|
separator | optional | Specifies the string at which each split should occur. If omitted, the array returned contains one element consisting of the entire string. |
limit | optional | Specifies a limit on the number of splits to be found. limit must be an integer. |
Return Value
Value | Explanation |
---|---|
[array] | Returns an Array of strings, containing the splitted values. |
Split by Nothing
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var str = "Hello there my old friend";
var result = str.split();
document.write(result);
</script>
</body>
</html>
Split by Characters
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var str = "Hello there my old friend";
var result = str.split("");
document.write(result);
</script>
</body>
</html>
Split With Limits
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var str = "Hello there my old friend";
var result = str.split(" ", 4);
document.write(result);
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.