Javascript Strings

strings are common text type data. It is collection of any character i.e. lower case, upper case, special characters, or numbers.


 var x="Tech Altum";    // string; 
 var y='Tech Altum';    // string; 
 var z="9";             // string
 var a='9';             // string


Properties of string

PropertyUseExample
lengthreturn no of characters var x="tech altum";
x.length=10;

Methods of String

MethoduseExample
indexOf("t")return first index of t substring.var x="tech altum";
x.indexOf("t")=0;
lastIndexOf("t")return last first index of t substring.var x="tech altum";
x.lastIndexOf("t")=7;
concat("x")concat with "." string .var x="tech altum";
x.concat(".")="tech altum.";
charAt(0)return first character of string.var x="tech altum";
x.charAt(0)="t";
charAt(1)return second character of string.var x="tech altum";
x.charAt(1)="e";
charAt(x.length-1)return last character of variable x.var x="tech altum";
x.charAt(x.length-1)="m";
charAt(x.length)return ""var x="tech altum";
x.charAt(x.length)="";
charCodeAt(0)return character encoding (ASCII value) of substring at 0 index.var x="tech altum";
x.charCodeAt(0)=116;
charCodeAt(1)return character encoding (ASCII value) of substring at 1 index .var x="tech altum";
x.charCodeAt(1)=101;
toUpperCase()convert lower to upper case .var x="tech altum";
x.toUpperCase()="TECH ALTUM";
toLowerCase()convert upper to lower case .var x="Tech Altum";
x.toLowerCase()="tech altum";
substr(1)return substring after 0 index .var x="tech altum";
x.substr(1)="ech altum";
substr(2)return substring after 1 index .var x="tech altum";
x.substr(2)="ch altum";
substr(2,2)return substring between 2 and next 2.var x="tech altum";
x.substr(2,2)="ch";
substring(1)return substring after 0 index.var x="tech altum";
x.substring(1)="ech Altum";
substring(2,4)return substring between 2 and 4 index.var x="tech altum";
x.substring(2,4)="ch";
trim()trim method is used to remove extra whitespace before and after string.var x=" tech altum ";
x.trim()="tech altum";
search("tech")return position on substring "tech", i.e 0.var x="tech altum";
x.search("tech")=0;
search("Tech")return position on substring "Tech", i.e -1.var x="tech altum";
x.search("Tech")=-1;

"+" operator and "concat()" method, both concat strings in javascript.