In JavaScript, a String
is a sequence of characters used to represent text. Strings are immutable, meaning once they are created, they cannot be changed. However, you can create new strings based on operations performed on existing ones.
Creating Strings
Strings can be created using single quotes ('
), double quotes ("
), or backticks (`
) for template literals.
let singleQuoteString = 'Hello, world!'; let doubleQuoteString = "Hello, world!"; let templateLiteralString = `Hello, world!`;
String Methods
JavaScript provides a variety of methods to work with strings. Below are some commonly used methods with examples:
- charAt(index):
Returns the character at the specified index.
let str = 'Hello'; console.log(str.charAt(1)); // 'e'
- charCodeAt(index):
Returns the Unicode of the character at the specified index.
let str = 'Hello'; console.log(str.charCodeAt(1)); // 101
- concat(str1, str2, …):
Concatenates the string arguments to the calling string and returns a new string.
let str1 = 'Hello'; let str2 = 'World'; console.log(str1.concat(' ', str2)); // 'Hello World'
- includes(searchString, position):
Determines whether one string may be found within another string.
let str = 'Hello, world!'; console.log(str.includes('world')); // true console.log(str.includes('World')); // false
- endsWith(searchString, length):
Determines whether a string ends with the characters of another string.
let str = 'Hello, world!'; console.log(str.endsWith('world!')); // true console.log(str.endsWith('hello')); // false
- indexOf(searchValue, fromIndex):
Returns the index of the first occurrence of the specified value.
let str = 'Hello, world!'; console.log(str.indexOf('world')); // 7 console.log(str.indexOf('hello')); // -1
- lastIndexOf(searchValue, fromIndex):
Returns the index of the last occurrence of the specified value.
let str = 'Hello, world!'; console.log(str.lastIndexOf('o')); // 8
- match(regexp):
Retrieves the result of matching a string against a regular expression.
let str = 'Hello, world!'; console.log(str.match(/o/g)); // ['o', 'o']
- replace(searchValue, newValue):
Replaces occurrences of a specified value with another value.
let str = 'Hello, world!'; console.log(str.replace('world', 'everyone')); // 'Hello, everyone!'
- search(regexp):
Executes a search for a match between a regular expression and this String object.let str = 'Hello, world!'; console.log(str.search(/world/)); // 7
- slice(start, end):
Extracts a section of a string and returns it as a new string.let str = 'Hello, world!'; console.log(str.slice(0, 5)); // 'Hello'
- split(separator, limit):
Splits a String object into an array of strings by separating the string into substrings.let str = 'Hello, world!'; console.log(str.split(', ')); // ['Hello', 'world!']
- startsWith(searchString, position):
Determines whether a string begins with the characters of another string.let str = 'Hello, world!'; console.log(str.startsWith('Hello')); // true console.log(str.startsWith('world', 7)); // true
- substring(start, end):
Returns the part of the string between the start and end indexes, or to the end of the string.let str = 'Hello, world!'; console.log(str.substring(0, 5)); // 'Hello'
- toLowerCase():
Returns the calling string value converted to lowercase.let str = 'Hello, World!'; console.log(str.toLowerCase()); // 'hello, world!'
- toUpperCase():
Returns the calling string value converted to uppercase.let str = 'Hello, World!'; console.log(str.toUpperCase()); // 'HELLO, WORLD!'
- trim():
Removes whitespace from both ends of a string.let str = ' Hello, world! '; console.log(str.trim()); // 'Hello, world!'
- trimStart() / trimEnd():
Removes whitespace only from the beginning or end of a string.let str = ' Hello, world! '; console.log(str.trimStart()); // 'Hello, world! ' console.log(str.trimEnd()); // ' Hello, world!'
- valueOf():
Returns the primitive value of a String object.javascript let str = new String('Hello, world!'); console.log(str.valueOf()); // 'Hello, world!'
Example Usage
let str = 'Hello, JavaScript World!'; console.log(str.charAt(0)); // 'H' console.log(str.includes('Java')); // true console.log(str.indexOf('World')); // 18 console.log(str.slice(7, 18)); // 'JavaScript' console.log(str.toUpperCase()); // 'HELLO, JAVASCRIPT WORLD!'
These methods provide a robust set of tools for manipulating and interacting with strings in JavaScript.