Strings are a fundamental data type in JavaScript, representing sequences of characters. Understanding how to manipulate strings effectively is essential for any JavaScript developer. In this article, we’ll explore various string operations in JavaScript, accompanied by examples presented in a convenient table format for easy reference.
1. Concatenation (+
):
Concatenation is the process of combining two or more strings into a single string.
const firstName = 'John';
const lastName = 'Doe';
const fullName = firstName + ' ' + lastName;
console.log(fullName); // Output: 'John Doe'
Operation | Description | Example |
---|---|---|
Concatenation | Combines two or more strings into one. | 'Hello' + 'World' returns 'HelloWorld' . |
2. String Length (length
):
The length
property returns the number of characters in a string.
javascriptconst message = 'Hello, World!';
c
onst length = message.length;console.log(length); // Output: 13
Operation | Description | Example |
---|---|---|
String Length | Returns the number of characters in a string. | 'Hello'.length returns 5 . |
3. Accessing Characters (Indexing):
You can access individual characters in a string using bracket notation with zero-based indexing.
const message = 'Hello, World!';
const firstCharacter = message[0];
console.log(firstCharacter); // Output: 'H'
Operation | Description | Example |
---|---|---|
Accessing Characters | Accesses the character at a specified index. | 'Hello'[0] returns 'H' . |
4. Substrings (substring()
):
The substring()
method extracts a portion of a string and returns it as a new string.
const message = 'Hello, World!';
const substring = message.substring(7, 12);
console.log(substring); // Output: 'World'
Operation | Description | Example |
---|---|---|
Substrings | Extracts a portion of a string. | 'Hello'.substring(2, 4) returns 'll' . |
5. String Case Methods (toUpperCase()
, toLowerCase()
):
These methods change the case of characters in a string.
const message = 'Hello, World!';
const upperCaseMessage = message.toUpperCase();
const lowerCaseMessage = message.toLowerCase(); console.log(upperCaseMessage); // Output: 'HELLO, WORLD!' console.log(lowerCaseMessage); // Output: 'hello, world!'
Operation | Description | Example |
---|---|---|
toUpperCase() | Converts all characters to uppercase. | 'hello'.toUpperCase() returns 'HELLO' . |
toLowerCase() | Converts all characters to lowercase. | 'HELLO'.toLowerCase() returns 'hello' . |
6. String Searching (indexOf()
, lastIndexOf()
, includes()
):
These methods search for substrings within a string.
const message = 'Hello, World!';
const indexOfWorld = message.indexOf('World');
const lastIndexOfl = message.lastIndexOf('l');
const includesHello = message.includes('Hello');
console.log(indexOfWorld); // Output: 7
console.log(lastIndexOfl); // Output: 10
console.log(includesHello); // Output: true
Operation | Description | Example |
---|---|---|
indexOf() | Returns the index of the first occurrence of a substring. | 'Hello'.indexOf('l') returns 2 . |
lastIndexOf() | Returns the index of the last occurrence of a substring. | 'Hello'.lastIndexOf('l') returns 3 . |
includes() | Checks if a string contains a specified substring. | 'Hello'.includes('H') returns true . |
Here’s an expanded table including various string operations in JavaScript:
Operation | Description | Example |
---|---|---|
Concatenation | Combines two or more strings into one. | 'Hello' + 'World' returns 'HelloWorld' . |
String Length | Returns the number of characters in a string. | 'Hello'.length returns 5 . |
Accessing Characters | Accesses the character at a specified index. | 'Hello'[0] returns 'H' . |
Substrings | Extracts a portion of a string. | 'Hello'.substring(2, 4) returns 'll' . |
Slice | Extracts a section of a string and returns it as a new string. | 'Hello'.slice(1, 3) returns 'el' . |
String Case Methods | Changes the case of characters in a string. | 'hello'.toUpperCase() returns 'HELLO' . |
'HELLO'.toLowerCase() returns 'hello' . | ||
String Searching | Searches for substrings within a string. | 'Hello'.indexOf('l') returns 2 . |
'Hello'.lastIndexOf('l') returns 3 . | ||
'Hello'.includes('H') returns true . | ||
Split | Splits a string into an array of substrings. | 'Hello,World'.split(',') returns ['Hello', 'World'] . |
Trim | Removes whitespace from both ends of a string. | ' Hello '.trim() returns 'Hello' . |
Replace | Replaces occurrences of a specified substring with another substring. | 'Hello, World'.replace('World', 'Universe') returns 'Hello, Universe' . |
Repeat | Returns a new string consisting of a specified number of copies of the original string. | 'abc'.repeat(3) returns 'abcabcabc' . |
Includes | Determines whether one string may be found within another string. | 'Hello'.includes('H') returns true . |
StartsWith | Determines whether a string begins with the characters of a specified string. | 'Hello'.startsWith('He') returns true . |
EndsWith | Determines whether a string ends with the characters of a specified string. | 'Hello'.endsWith('lo') returns true . |