String Functions in JavaScript

JavaScriptWe have couple of functions to manipulate strings in JavaScript.  Mostly commonly used functions are given below with example.

toUpperCase():  This function will change the case of a string to upper.

toLowerCase():  This function will change the case of a string to lower.

trim():  This function will remove any extra white space at the start and end of a string.

replace():  This function will replace any value in a string.  It will take 2 parameters, old value and new value.  It will basically search for old value in a string and perform the replacement.  You can also pass regular expression as parameter instead of old value.

length:  This is not a function but a property which will return the length of a string.

<!DOCTYPE html>
<title>JavaScript Tutorial</title> 	
<script type="text/javascript">
	var myString = "    JavaScript Tutorials for Beginners   ";

	document.write(myString.toUpperCase() + "<br>");

	document.write(myString.toLowerCase() + "<br>");
	document.write(myString.trim() + "<br>");

	var result = myString.replace("Beginners", "Advanced");
	document.write(result+ "<br>");