In JavaScript, a string is a primitive data type that is used for textual data. JavaScript string must be enclosed in single quotes, double quotes, or backticks. The followings are string literals in JavaScript.
Example: String literals
Hello World
Hello Soopro
Hello Pathshala
Example: String variables
let str1 = "This is a double quoted string.";
let str2 = 'This is a single quoted string.';
let str3 = `This is a template string.`;
The template string (using backticks) is used when you want to include the value of a variable or expressions into a string. Use ${variable or expression} inside backticks as shown below.
Example: Template String
let amount = 1000, rate = 0.05, duration = 3;
let result = `Total Amount Payble: ${amount*(1 + rate*duration)}`;
The template string can be spanned in multiple lines which is not allowed with a single or double quoted string, as shown below.
JavaScript string can be treated like a character array. You can access a character in a string using square brackets [index] or using the str.at(pos) method.
Example: String as array
let str = 'Hello World';
let ch1 = str[0] // H
let ch2 = str[1] // e
let ch3 = str.at(2) // l
let ch4 = str.at(3) // l
str[4] = "P"; //error
You can include single quotes in double-quoted string or include double quotes in a single quoted string. However, you cannot include a single quotes in single quoted string and double quotes in double-quoted string.
Example: Quotes in string
let str1 = "This is 'simple' string";
let str2 = 'This is "simple" string';
let str3 = `This is 'simple' and "easy" string`;
JavaScript string can be concatenated using the + operator or string.concat() method.
Example: String concatenation
let str1='Hello';
let str2="world ";
let str3= str1+str2;
let str4=str1.concat(str2);
JavaScript allows you to create a string object using the new keyword, as shown below.
Example: string object
let str1=new String();
str1='Hello World';
let str2=new String('Hello world');
String objects and string literals are different. The typeof() method will return the type of a variable. The following distinguished string and string objects.
Example:string object
let str1=new String('Hello world');
let str2="Hello world";
typeof(str1);//"object"
typeof(str2);//"string"
Two strings can be compared using <, >, ==, === operator, and string.localeCompare(string) method.
The mathematical operators < and > compare two strings and return a boolean (true or false) based on the order of the characters in the string.
The == operator compares the content of strings and === compares the reference equality of strings. The localeCompare() method compares two strings in the current locale. It returns 0 if strings are equal, else returns 1.
Property | Description |
---|---|
length | Returns the length of the string |
Method | Description |
---|---|
charAt(Position) | Returns the character at the specified position (in Number). |
charCodeAt(position) | Returns a number indicating the Unicode value of the character at the given position (in Number). |
concat([string,,]) | Joins specified string literal values (specify multiple strings separated by comma) and returns a new string. |
indexOf(SearchString, Position) | Returns the index of first occurrence of specified String starting from specified number index. Returns -1 if not found. |
lastIndexOf(SearchString, Position) | Returns the last occurrence index of specified SearchString, starting from specified position. Returns -1 if not found. |
localeCompare(string,position) | Compares two strings in the current locale. |
match(RegExp) | Search a string for a match using specified regular expression. Returns a matching array. |
replace(searchValue, replaceValue) | Search specified string value and replace with specified replace Value string and return new string. Regular expression can also be used as searchValue. |
search(RegExp) | Search for a match based on specified regular expression. |
slice(startNumber, endNumber) | Extracts a section of a string based on specified starting and ending index and returns a new string. |
split(separatorString, limitNumber) | Splits a String into an array of strings by separating the string into substrings based on specified separator. Regular expression can also be used as separator. |
substr(start, length) | Returns the characters in a string from specified starting position through the specified number of characters (length). |
substring(start, end) | Returns the characters in a string between start and end indexes. |
toLocaleLowerCase() | Converts a string to lower case according to current locale. |
toLocaleUpperCase() | Converts a sting to upper case according to current locale. |
toLowerCase() | Returns lower case string value. |
toString() | Returns the value of String object. |
toUpperCase() | Returns upper case string value. |
valueOf() | Returns the primitive value of the specified string object. |