Free Web Development Online Tutorials, Learn to Code
Learn JavaScript string functions, built in string methods in javascript, string methods in javascript with examples

String methods in javascript

Learn JavaScript string methods that will help you to manipulate string values.

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 and string length example
var _message = "I am learning javascript in WebTrainingRoom";            
var _resultString = _message.substring(4, 25);

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

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

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 example

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

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

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

var _message = "    javascript trim function example      ";
var _resultString = _message.trim();

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

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

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

var _sampleString = "JavaScript String Methods";
var _uppercaseStringt = _sampleString.toUpperCase();
var _lowercaseStringt = _sampleString.toLowerCase();
// result will be "javascript string methods"
Javascript string replace method Example

Javascript replace method will replace the one string with another string

var _message = "I am learning javascript in WebTrainingRoom";
var _resultString = _message.replace("javascript","JSON")
Now _resultString will print  "I am learning JSON in WebTrainingRoom";
Javascript String Methods

javascript Interview Questions Answers

Javascript String function examples
JavaScript Examples | JavaScript Online Course