⚡ JavaScript

Métodos de cadenas — guía completa en JavaScript (Node.js): guía detallada

What is it and why? (a real-life analogy)

Imagine you received a long letter from a friend. It has a lot of text: a greeting, news, questions, a farewell. What can you do with it? You can:

  • Read it entirely or in parts (for example, only the first paragraph).
  • Find a specific word in it (for example, "meeting").
  • Replace one word with another (for example, "Monday" with "Tuesday").
  • Split the letter into sentences or words.
  • Check which word it starts and ends with.
  • Count how many letters, spaces, or exclamation marks are in it.

In programming, a string is exactly the same kind of "letter". It is a sequence of characters: letters, numbers, spaces, punctuation marks. And string methods are ready-made tools (like scissors, a magnifying glass, a ruler) that JavaScript gives us to work with this text. You don't need to reinvent the wheel: everything is already written for you.

💡 Important: Strings in JavaScript are a primitive data type. This means they are stored as simple values, not as complex objects. But when you call methods, JavaScript temporarily "wraps" the string in an object so you can use all these convenient functions.

Why is this needed? Imagine you are writing a program that checks a password: does it contain uppercase letters, numbers, and special characters. Or you are doing a site search and need to highlight the found words. Or you are processing data from a file: removing extra spaces, converting everything to lowercase. Without string methods, you would have to write dozens of lines of code for each such operation. With methods, it's one line.

Theory: how it works internally

Let's look "under the hood". When you write:

let text = "Hello, world!";
console.log(text.length); // 13

JavaScript counts each character. A space is also a character. An exclamation mark is a character. Each letter is a character. length is not a method, but a property. It simply stores a number — the length of the string.

But toUpperCase() is a method. When you call it:

let bigText = text.toUpperCase();

The following happens:

  1. JavaScript takes the original string "Hello, world!".
  2. It creates a new string where each letter is replaced with its uppercase version: "HELLO, WORLD!".
  3. It returns this new string.
  4. The original string text remains unchanged.
⚠️ Key point: Strings in JavaScript are immutable. This means you cannot change an existing string. You always create a new one. String methods never change the original; they return a new result.

Why is this important? Because if you write:

let name = "Ivan";
name.toUpperCase();
console.log(name); // "Ivan" — unchanged!

To save the result, you need to assign it to a variable:

let name = "Ivan";
let upperName = name.toUpperCase();
console.log(upperName); // "IVAN"

Now let's understand indices. Each character in a string has its own number (index), starting from zero:

let word = "Cat";
// Indices: 0: 'C', 1: 'a', 2: 't'

Many string methods use these indices. For example, charAt(1) will return the character at index 1 — which is 'a'. And slice(0, 2) will return a substring from index 0 to index 2 (not including it) — which is "Ca".

Methods can be called one after another in a chain, because each method returns a new string:

let result = "  Hello, World!  ".trim().toLowerCase().replace("world", "everyone");
// Result: "hello, everyone!"

Here, trim() removed the spaces, toLowerCase() made all letters lowercase, and replace() replaced the word. Each method works with the result of the previous one.

Syntax in JavaScript (Node.js): all forms and variants

In JavaScript (and Node.js), strings can be created in three ways:

// 1. Single quotes
let str1 = 'Hello';

// 2. Double quotes
let str2 = "Hello";

// 3. Backticks (template strings) — the most powerful
let name = "Anna";
let str3 = `Hello, ${name}!`; // "Hello, Anna!"

Template strings (backticks) allow you to insert variables directly into the text using ${} and write multi-line text without .

Now — a complete reference of methods. I have divided them into groups by purpose.

Table 1: Methods for changing case and removing spaces

Method What it does Example Result
toUpperCase() Converts all letters to uppercase "cat".toUpperCase() "CAT"
toLowerCase() Converts all letters to lowercase "CaT".toLowerCase() "cat"
trim() Removes spaces from the beginning and end " yes ".trim() "yes"