Чтобы разделить строку по символам в JavaScript, вы можете использовать различные методы. Вот несколько распространенных подходов:
-
Разделение строки:
const str = "Hello World"; const characters = str.split(""); // Split into an array of characters console.log(characters); // Output: ["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"] -
Использование оператора расширения:
const str = "Hello World"; const characters = [...str]; // Convert string to an array of characters console.log(characters); // Output: ["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"] -
Итерация по строке:
const str = "Hello World"; const characters = []; for (let i = 0; i < str.length; i++) { characters.push(str.charAt(i)); // Add each character to the array } console.log(characters); // Output: ["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"]