String methods in javascript

Learn JavaScript string methods that will help you to manipulate string values. there are many built-in methods to work with string in javascript.

javascript string functions

Here we learn how to work with string in javascript, string length, substring, slice, trim, find, concat, indexOf, replace, Upper and Lower Case etc.

built-in string methods in JavaScript

JavaScript substring method example

Learn how to use JavaScript substring method and string length example.

<script>            
var _message = "I am learning javascript in WebTrainingRoom";            
var _resultString = _message.substring(4, 25);
</script>

4 is our start point
25 is our end point
now if you print the value of _resultString, you will see "learning javascript"

To find the length of any string, you need to use .length property, here is an example

<script>
var _message = "I am learning javascript string";
var _strLength=_message.length;
//_strLength will print the value string length
</script>

Note: while writing the above code, have you noticed that we used = equal sign? that’s not equal, that’s the way assigning the value to a variable. to compare two variable if equal, in JavaScript we use ==, learn all javaScript operators

Javascript Slice methdo example

Slice is almost same as Substring, but here you can specify negative number too

<script>
var _message = "I am learning javascript in WebTrainingRoom";            
var _resultString = _message.slice(4, -25);
</script>

Javascript trim function example

JavaScript trim() method used for removing the extra space in front and back

<script>
var _message = "    javascript trim function example      ";
var _resultString = _message.trim();
</script>

JavaScript String indexOf Method

JavaScript indexOf() method used for finding the position of a string within a string

<script>
var _message = "I am learning javascript indexof method";
var _resultPosition = _message.indexOf("learning");
</script>

in above example you see can see the position of word "learning" within that sentence
You also can sepcify the start index in case there are multiple location of string you are looking for.

var _message = "I am learning javascript indexof method, javascript is fun";
var _resultPosition = _message.indexOf("javascript",20);

Now in above example there are two "javascript" word , and this will tell you the index of second location.

Javascript string Upper and Lower case Example

Javascript has method to change case Upper or Lower Case

<script>
var _sampleString = "JavaScript String Methods";
               
var _uppercaseStringt = _sampleString.toUpperCase();
// result will be "JAVASCRIPT STRING METHODS"
               
var _lowercaseStringt = _sampleString.toLowerCase();
// result will be "javascript string methods"
</script>

Javascript string replace method Example

Javascript replace method will replace the one string with another string

<script>
var _message = "I am learning javascript in WebTrainingRoom";
var _resultString = _message.replace("javascript","JSON")
Now _resultString will print  "I am learning JSON in WebTrainingRoom";
</script>
 
Advanced JavaScript
Javascript String Methods
javascript Interview Questions Answers
Javascript String function examples
JavaScript Examples | JavaScript Online Course