#atom
Content:
Strings in JavaScript are sequences of characters used to represent text. They are primitive data types but have object-like behavior due to JavaScript's automatic boxing (temporary conversion to a String
object when methods are called). Strings are immutable, meaning once created, their values cannot be changed.
Key Characteristics:
- Strings can be created using single quotes (
'
), double quotes ("
), or backticks (`
). - Backticks enable template literals, which support multi-line strings and string interpolation.
- Strings are zero-indexed, meaning the first character is at index
0
. - Strings have a
length
property that returns the number of characters in the string.
Common String Methods (with TypeScript Signatures):
charAt()
: Returns the character at the specified index.- Signature:
charAt(index: number): string;
- Signature:
charCodeAt()
: Returns the Unicode value of the character at the specified index.- Signature:
charCodeAt(index: number): number;
- Signature:
concat()
: Combines two or more strings and returns a new string.- Signature:
concat(...strings: string[]): string;
- Signature:
includes()
: Checks if a string contains a specified substring.- Signature:
includes(searchString: string, position?: number): boolean;
- Signature:
indexOf()
: Returns the index of the first occurrence of a specified substring, or-1
if not found.- Signature:
indexOf(searchString: string, position?: number): number;
- Signature:
lastIndexOf()
: Returns the index of the last occurrence of a specified substring, or-1
if not found.- Signature:
lastIndexOf(searchString: string, position?: number): number;
- Signature:
slice()
: Extracts a section of a string and returns it as a new string.- Signature:
slice(start?: number, end?: number): string;
- Signature:
substring()
: Similar toslice()
, but does not support negative indices.- Signature:
substring(start: number, end?: number): string;
- Signature:
substr()
: Extracts a specified number of characters starting from a specified index.- Signature:
substr(start: number, length?: number): string;
- Signature:
toLowerCase()
: Converts the string to lowercase.- Signature:
toLowerCase(): string;
- Signature:
toUpperCase()
: Converts the string to uppercase.- Signature:
toUpperCase(): string;
- Signature:
trim()
: Removes whitespace from both ends of a string.- Signature:
trim(): string;
- Signature:
replace()
: Replaces a specified substring or pattern with another substring.- Signature:
replace(searchValue: string | RegExp, replaceValue: string): string;
- Signature:
split()
: Splits a string into an array of substrings based on a specified separator.- Signature:
split(separator: string | RegExp, limit?: number): string[];
- Signature:
startsWith()
: Checks if a string starts with a specified substring.- Signature:
startsWith(searchString: string, position?: number): boolean;
- Signature:
endsWith()
: Checks if a string ends with a specified substring.- Signature:
endsWith(searchString: string, position?: number): boolean;
- Signature:
match()
: Searches a string for a match against a regular expression and returns the matches.- Signature:
match(regexp: string | RegExp): RegExpMatchArray | null;
- Signature:
repeat()
: Returns a new string with the original string repeated a specified number of times.- Signature:
repeat(count: number): string;
- Signature:
Template Literals:
- Template literals (using backticks) allow embedded expressions and multi-line strings.
- Example:
const name = "Alice"; const greeting = `Hello, ${name}! Welcome to JavaScript.`; console.log(greeting);
Linked Cards:
- Data Types in JavaScript: Strings are a primitive data type in JavaScript.
- Functions in JavaScript: String methods are functions that operate on strings.
- Arrays in JavaScript: Strings can be converted to arrays using
split()
. - Regular Expressions in JavaScript: Used with methods like
match()
andreplace()
. - Prototypes in JavaScript: Strings inherit methods from
String.prototype
. - Syntactic Sugar: Template literals provide syntactic sugar for string manipulation.
Tags: #JavaScript #Strings #Methods #TemplateLiterals #TypeScript #DataTypes
Connections:
Sources: