#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:

Common String Methods (with TypeScript Signatures):

  1. charAt(): Returns the character at the specified index.
    • Signature: charAt(index: number): string;
  2. charCodeAt(): Returns the Unicode value of the character at the specified index.
    • Signature: charCodeAt(index: number): number;
  3. concat(): Combines two or more strings and returns a new string.
    • Signature: concat(...strings: string[]): string;
  4. includes(): Checks if a string contains a specified substring.
    • Signature: includes(searchString: string, position?: number): boolean;
  5. indexOf(): Returns the index of the first occurrence of a specified substring, or -1 if not found.
    • Signature: indexOf(searchString: string, position?: number): number;
  6. lastIndexOf(): Returns the index of the last occurrence of a specified substring, or -1 if not found.
    • Signature: lastIndexOf(searchString: string, position?: number): number;
  7. slice(): Extracts a section of a string and returns it as a new string.
    • Signature: slice(start?: number, end?: number): string;
  8. substring(): Similar to slice(), but does not support negative indices.
    • Signature: substring(start: number, end?: number): string;
  9. substr(): Extracts a specified number of characters starting from a specified index.
    • Signature: substr(start: number, length?: number): string;
  10. toLowerCase(): Converts the string to lowercase.
    • Signature: toLowerCase(): string;
  11. toUpperCase(): Converts the string to uppercase.
    • Signature: toUpperCase(): string;
  12. trim(): Removes whitespace from both ends of a string.
    • Signature: trim(): string;
  13. replace(): Replaces a specified substring or pattern with another substring.
    • Signature: replace(searchValue: string | RegExp, replaceValue: string): string;
  14. split(): Splits a string into an array of substrings based on a specified separator.
    • Signature: split(separator: string | RegExp, limit?: number): string[];
  15. startsWith(): Checks if a string starts with a specified substring.
    • Signature: startsWith(searchString: string, position?: number): boolean;
  16. endsWith(): Checks if a string ends with a specified substring.
    • Signature: endsWith(searchString: string, position?: number): boolean;
  17. match(): Searches a string for a match against a regular expression and returns the matches.
    • Signature: match(regexp: string | RegExp): RegExpMatchArray | null;
  18. repeat(): Returns a new string with the original string repeated a specified number of times.
    • Signature: repeat(count: number): string;

Template Literals:

Linked Cards:

Tags: #JavaScript #Strings #Methods #TemplateLiterals #TypeScript #DataTypes

Connections:


Sources: