In this tutorial, you will learn about JavaScript string with the help of examples.
A string is a collection of one or more characters that can be letters, numbers, or symbols. Strings are primitive data types in JavaScript that are immutable, which means they do not change.
Contents
What is String in JavaScript with an example?
A string can be any text enclosed in double or single quotation marks: carName1 = “Volvo XC60” carName2 = “Volvo XC60” String indexes start at zero: The first character is in place zero, the second in position one, and so on.
A string in JavaScript is a primitive data type that is used to work with text. As an example:
const name = 'John';
Create JavaScript Strings
Strings are created in JavaScript by enclosing them in quotes. Quotes can be used in three different ways.
- Single quotes: ‘DevelopersDome’
- Double quotes: “DevelopersDome”
- Backticks: `DevelopersDome`
For example:
//strings example const name = 'Sagar'; const name1 = "Sahil"; const result = `The names are ${name} and ${name1}`;
Single and double quotations are nearly identical and can be used interchangeably.
Backticks are commonly used to include variables or expressions in a string. This is accomplished by enclosing variables or expressions in $variable or expression, as seen above.
You can also write a quote within a quote. As an example:
const name = 'My name is "Peter".';
The quote, however, should not match the surrounding quotes. As an example:
const name = 'My name is 'Peter'.'; // error
Access String Characters
There are two ways to get at the characters in a string.
One approach is to consider strings as an array. As an example:
const a = 'hello'; console.log(a[1]); // "e"
Another way is to use the method charAt(). For example:
const a = 'hello'; console.log(a.charAt(1)); // "e"
JavaScript Strings are immutable
String specifications differ amongst programming languages, although most handle them as reference types. However, strings in JavaScript are not the same. They are unchangeable primitives. This means that the characters contained within them cannot be modified and that all operations on strings result in the creation of new strings.
Strings are immutable in JavaScript. That is, a string’s characters cannot be modified. As an example:
let a = 'hello'; a[0] = 'H'; console.log(a); // "hello"
You can, however, assign the variable name to a new string. As an example:
let a = 'hello'; a = 'Hello'; console.log(a); // "Hello"
JavaScript is Case-Sensitive
JavaScript is a case-sensitive programming language. This means that all language keywords, variables, function names, and other identifiers must be entered with consistent letter capitalization. For example, the while keyword must be typed “while,” not “While” or “WHILE.”
The case is important in JavaScript. That is, lowercase and uppercase letters are treated differently in JavaScript. As an example:
const a = 'a'; const b = 'A' console.log(a === b); // false
In JavaScript, the values a and A are treated differently.
JavaScript Multiline Strings
Strings that span many lines can be created in three ways: By making use of template literals. The + operator – the JavaScript concatenation operator – is used. Using the JavaScript backslash operator and escape character.
You can use either the + or the operator to use a multiline string. As an example:
// using the + operator const message1 = 'This is a long message ' + 'that spans across multiple lines' + 'in the code.' // using the \ operator const message2 = 'This is a long message \ that spans across multiple lines \ in the code.'
JavaScript String Length
You can use the built-in length property to determine the length of a string. As an example:
const a = 'hello'; console.log(a.length); // 5
JavaScript String Objects
The new keyword can also be used to construct strings. As an example:
const a = 'hello'; const b = new String('hello'); console.log(a); // "hello" console.log(b); // "hello" console.log(typeof a); // "string" console.log(typeof b); // "object"
Note: It is strongly advised to avoid using string objects. Using string objects slows the program down.
JavaScript String Methods
The Java String class includes many methods for performing string operations such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring(), and so on. The class java.lang. Serializable, Comparable, and CharSequence interfaces are implemented by the String class.
Here are the commonly used JavaScript String methods:
Method | Description |
---|---|
charAt(index) | returns the character at the specified index |
concat() | joins two or more strings |
replace() | replaces a string with another string |
split() | converts the string to an array of strings |
substr(start, length) | returns a part of a string |
substring(start,end) | returns a part of a string |
slice(start, end) | returns a part of a string |
toLowerCase() | returns the passed string in lowercase |
toUpperCase() | returns the passed string in the upper case |
trim() | removes whitespace from the strings |
includes() | searches for a string and returns a boolean value |
search() | searches for a string and returns a position of a match |
Example: JavaScript String Methods
const text1 = 'hello'; const text2 = 'world'; const text3 = ' JavaScript '; // concatenating two strings const result1 = text1.concat(' ', text2); console.log(result1); // "hello world" // converting the text to uppercase const result2 = text1.toUpperCase(); console.log(result2); // HELLO // removing whitespace from the string const result3 = text3.trim(); console.log(result3); // JavaScript // converting the string to an array const result4 = text1.split(); console.log(result4); // ["hello"] // slicing the string const result5= text1.slice(1, 3); console.log(result5); // "el"
JavaScript String() Function
String() is a function that converts various data types to strings. As an example:
const a = 225; // number const b = true; // boolean //converting to string const result1 = String(a); const result2 = String(b); console.log(result1); // "225" console.log(result2); // "true"
You may like:
continue and break Statements in JavaScript