var sentence = "I learn JavaScript";
var words = sentence.split(" "); // space as separator
alert( "Found " + words.length + " words in that sentence!" ); // 3
var wordCount = document.getElementById("txt").value.split(" ").length;
Tip: pass anything you like, but enclose your argument inside single or double-quotes.
var numberOfLines = document.getElementById("txt").value.split("\n").length;
var str = "I do this AND I do that and then this And finally that.";
var steps = str.toLowerCase().split("and");
alert( steps.length ); // returns 4
// Without toLowerCase(), a 2-elements array would have been returned.
var str = "I do this AND I do that and then this And finally that.";
if( str.toLowerCase().indexOf("and") > -1 )
alert("Go ahead and split!");
var sentence = "I learn JavaScript";
var splitArr = sentence.split("carrot");
alert( "Returned array has " + splitArr.length + " elements." ); // 1
var sentence = "I learn JavaScript";
var splitArr = sentence.split(" ", 2); // maximum two elements
alert( "Array elements:\n" + splitArr.join("\n") ); // Show one per line
$sentence = 'I learn PHP';
$words = explode(' ', $sentence);
echo 'Found ' . count( $words ) . ' words in that sentence!';
Sister / opposite method: join() concatenates array elements with a string you supply.