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

JavaScript Strings can have properties, like length.

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

Methods of String

JavaScript Methods are build in functions. All methods are called by method name and then parenthesis. Parameters are passed inside parenthesis if needed.

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

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