The Wayback Machine - https://web.archive.org/web/20240922041752/https://www.geeksforgeeks.org/how-to-get-character-array-from-string-in-javascript/
Open In App

How to Get Character Array from String in JavaScript ?

Last Updated : 13 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In JavaScript, you might often find yourself needing to break down a string into individual characters and turn it into an array. This can be useful for iterating over each character, performing string manipulations, or simply working with the data in a different format. Luckily, JavaScript offers several methods to convert a string into a character array. Let’s understand the different methods or approaches to get the character array from the string in the javascript.

Using String split() Method

The str.split() Method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument. 

Syntax:

str.split(separator, limit);

Example: In this example, we will get a character array from the string using the JavaScript split() method.

JavaScript
let str = "GeeksforGeeks";
let arr = str.split("");
console.log(arr);

Output
[
  'G', 'e', 'e', 'k',
  's', 'f', 'o', 'r',
  'G', 'e', 'e', 'k',
  's'
]

JavaScript Array.from() Method

The Array.from() method creates a new array instance from a given array. In case of string, every alphabet of the string is converted to an element of the new array instance, and in the case of integer values, a new array instance simply takes the elements of the given array. 

Syntax:

Array.from(str);

Example: In this example, we will get a character array from the string using the Array.from() function in JavaScript.

JavaScript
// Input array
let str = "GeeksforGeeks: A computer science portal";

// Getting char array
let arr = Array.from(str);
          
// Display output          
console.log(arr);

Output
[
  'G', 'e', 'e', 'k', 's', 'f', 'o',
  'r', 'G', 'e', 'e', 'k', 's', ':',
  ' ', 'A', ' ', 'c', 'o', 'm', 'p',
  'u', 't', 'e', 'r', ' ', 's', 'c',
  'i', 'e', 'n', 'c', 'e', ' ', 'p',
  'o', 'r', '...

JavaScript Spread Operator

The Spread operator allows an iterable to expand in place. In the case of a string, it example string into character and we capture all the characters of a string in an array.

Syntax:

let variableName = [ ...value ];

Example: In this example, we will get a character array from the string using the spread operator in JavaScript.

JavaScript
let str = "GeeksforGeeks";
console.log([...str]);

Output
[
  'G', 'e', 'e', 'k',
  's', 'f', 'o', 'r',
  'G', 'e', 'e', 'k',
  's'
]

Using for … of Loop

JavaScript for of statement iterates over the values of an iterable object (like Array, Map, Set, arguments object, â€Ķ,etc), executing statements for each value of the object.

Syntax:

for ( variable of iterableObjectName) {
   ...
}

Example: In this example, we are using for…of loop.

JavaScript
const s = '12345';
const a = [];
for (const s1 of s) {
   a.push(s1);
}
console.log(a);

Output
[ '1', '2', '3', '4', '5' ]

Previous Article
Next Article

Similar Reads

JavaScript Program to Get a Non-Repeating Character From the Given String
In JavaScript, we can find the non-repeating character from the input string by identifying the characters that occur only once in the string. There are several approaches in JavaScript to get a non-repeating character from the given string which are as follows: Table of Content Using indexOf and lastIndexOf methodsUsing Set and Array.from()Using M
3 min read
How to get the last character of a string in JavaScript ?
In JavaScript, working with strings often requires extracting specific characters, like the last one. Knowing how to access the last character of a string is essential in tasks such as formatting, validation, and manipulation, making it a fundamental skill in programming. There are several methods to solve this problem: Table of Content Using charA
4 min read
How to get the position of character in a string in PHP ?
In this article, we will get the position of the character in the given string in PHP. String is a set of characters. We will get the position of the character in a string by using strpos() function. Syntax: strpos(string, character, start_pos) Parameters: string (mandatory): This parameter refers to the original string in which we need to search t
2 min read
How to get the last character of a string in PHP ?
In this article, we will find the last character of a string in PHP. The last character can be found using the following methods. Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, in zero-indexing, the value of (length-1) is 5 whic
2 min read
How to Get Character of Specific Position using JavaScript ?
Get the Character of a Specific Position Using JavaScript We have different approaches, In this article we are going to learn how to Get the Character of a Specific Position using JavaScript Below are the methods to get the character at a specific position using JavaScript: Table of Content Method 1: Using String charAt() MethodMethod 2: Using Stri
4 min read
How to Search Character in List of Array Object Inside an Array Object in JavaScript ?
Searching for a character in a list of array objects inside an array object involves inspecting each array object, and then examining each array within for the presence of the specified character. The goal is to determine if the character exists within any of the nested arrays. There are several ways to search for characters in a list of array obje
4 min read
How to Get the Longest String in an Array using JavaScript?
Given an array, the task is to get the longest string from the array in JavaScript. Here are a few of the most used techniques discussed with the help of JavaScript. In this article, we will use JavaScript methods to find out the longest string in the array. All approaches are described below with examples. Table of Content Using the .sort() Method
5 min read
How to remove portion of a string after certain character in JavaScript ?
Given a URL and the task is to remove a portion of URL after a certain character using JavaScript. split() method: This method is used to split a string into an array of substrings, and returns the new array. Syntax:string.split(separator, limit)Parameters:separator: It is optional parameter. It specifies the character, or the regular expression, t
3 min read
Javascript Program for Queries for rotation and Kth character of the given string in constant time
Given a string str, the task is to perform the following type of queries on the given string: (1, K): Left rotate the string by K characters.(2, K): Print the Kth character of the string. Examples: Input: str = "abcdefgh", q[][] = {{1, 2}, {2, 2}, {1, 4}, {2, 7}} Output: d e Query 1: str = "cdefghab" Query 2: 2nd character is d Query 3: str = "ghab
2 min read
Find Word Character in a String with JavaScript RegExp
Regular expressions or RegExp can be used in JavaScript to search for patterns in texts. Searching for a certain character in a string, such as locating all occurrences of a word character, is a typical activity. In this article, we'll look at how to utilize RegExp in JavaScript to find word characters in a string. Terminologies: Regular expression
4 min read
Maximum Frequency Character in String in JavaScript
A string is a data structure in JavaScript that is used to store a set of characters. A string can contain a character, a word, or a sentence. A string also can be empty. It is represented using double quotes, single quotes, or template literals. Examples:Input: str = "abcdabac"Output: aExplanation: In the given string the number of occurrence of c
3 min read
Least Frequent Character in String in JavaScript
In JavaScript, finding the least frequent character in a string involves identifying the character that occurs the fewest times within the given string by iterating through the string, counting the occurrences of each character, and determining the one with the minimum frequency. This algorithmic task is essential for analyzing and manipulating str
2 min read
JavaScript Program to Remove Last Character from the String
In this article, we will learn how to remove the last character from the string in JavaScript. The string is used to represent the sequence of characters. Now, we will remove the last character from this string. Example: Input : Geeks for geeks Output : Geeks for geek Input : Geeksforgeeks Output : Geeksforgeek Below are the following approaches th
3 min read
How to Find the Index of Specific Character in a String in JavaScript ?
We will see how to find the index of a specific character in a string using JavaScript. A string is a sequence of characters, such as letters, numbers, or symbols, enclosed in single or double quotes. Here we are going to find the index of a specific character from our given string. Several approaches can be used to find the index of a specific cha
4 min read
JavaScript Program to Check if a String Contains any Special Character
This article will demonstrate the methods to write a JavaScript Program to check if a String contains any special characters. Any string that contains any character other than alphanumeric and space includes the special character. These special characters are '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', '=', '{', '}', '[', ']', '
5 min read
Remove all occurrences of a character in a string using JavaScript
Removing all occurrences of a character in a string means eliminating every instance of a particular character from the given string, resulting in a modified string without that character. Approaches to Remove all occurrences of a character in a string using JavaScriptTable of Content Approach 1: Using a JavaScript Regular ExpressionApproach 2: Usi
3 min read
JavaScript Program to Count the Occurrences of a Specific Character in a String
In this article, we will see how to count the frequency of a specific character in a string with JavaScript. Counting the frequency of a specific character in a string is a common task in JavaScript. Example: Input : S = “geeksforgeeks” and c = ‘e’Output : 4Explanation: ‘e’ appears four times in str.Input : S = “abccdefgaa” and c = ‘a’Output : 3Exp
3 min read
JavaScript Program to Find K’th Non-Repeating Character in String
The K'th non-repeating character in a string is found by iterating through the string length and counting how many times each character has appeared. When any character is found that appears only once and it is the K'th unique character encountered, it is returned as the result. This operation helps identify the K'th non-repeating character in the
7 min read
JavaScript Program to Test if Kth Character is Digit in String
Testing if the Kth character in a string is a digit in JavaScript involves checking the character at the specified index and determining if it is a numeric digit. Examples:Input : test_str = ‘geeks9geeks’, K = 5 Output : True Explanation : 5th idx element is 9, a digit, hence True.Input : test_str = ‘geeks9geeks’, K = 4 Output : False Explanation :
5 min read
JavaScript Program to Find i’th Index Character in a Binary String Obtained After n Iterations
Given a decimal number m, convert it into a binary string and apply n iterations. In each iteration, 0 becomes “01” and 1 becomes “10”. Find the (based on indexing) index character in the string after the nth iteration. Input: m = 5, n = 2, i = 3Output: 1Input: m = 3, n = 3, i = 6Output: 1Approach 1Convert a decimal number into its binary represent
6 min read
Remove a Character From String in JavaScript
We are given a string and the task is to remove a character from the given string. We have many methods to remove a character from a string that is described below. Table of Content Method 1: Using JavaScript replace() MethodMethod 2: Using JavaScript replace() Method with a Regular ExpressionMethod 3: Using JavaScript split() and join() MethodsMet
3 min read
How to Find the First and Last Character of a String in JavaScript ?
We are going to learn about finding the first and last character of a string in JavaScript. The first character of a string is its initial symbol, while the last character is the final symbol at the end of the string. Strings are sequences of characters. Here we have some common methods to find the first and last character of a string by using Java
5 min read
Delete first character of a string in JavaScript
Delete the first character of a string in JavaScript refers to removing the initial character from a given string and returning the rest of the string. Below are the methods used to delete the first character of a string in JavaScript: Table of Content Using slice() Method Using substr() MethodUsing the substring() Method Using slice() Method The s
2 min read
How to Get File Character Encoding in Node.js ?
Character encoding is essential when working with text files, as it determines how characters are represented in bytes. Different files may use different encodings, such as UTF-8, ASCII, or ISO-8859-1. Determining the character encoding of a file in Node.js can help ensure proper reading and processing of the file's content. Why Is Character Encodi
2 min read
How to get the elements of one array which are not present in another array using JavaScript?
The task is to get the elements of one array that are not present in another array. Here we are going to use JavaScript to achieve the goal. Here are a few techniques discussed. Approach Take the arrays in variables.Use the .filter() method on the first array and check if the elements of the first array are not present in the second array, Include
2 min read
JavaScript method to get the URL without query string
The task is to get the URL name of the page without using a query string with the help of JavaScript. replace() method: This method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value. Syntax: string.replace(searchVal, newvalue) Parameters: searchVal: This parameter is required. I
3 min read
How to use Regex to get the string between curly braces using JavaScript ?
The problem is to get the string between the curly braces. Here we are going to see different Regular Expressions to get the string. Approach 1: Selecting the string between the outer curly braces.The RegExp selects the all curly braces, removes them, and then gets the content. Example: This example illustrates the above approach. C/C++ Code <h1
2 min read
How to get tomorrow's date in a string format in JavaScript ?
In this article, we will see how to print tomorrow's date in string representation using JavaScript. To achieve this, we use the Date object and create an instance of it. After that by using the setDate() method, we increase one date to the present date. Now by using the getDate() method you will get tomorrow's date. Now to convert that date to the
2 min read
JavaScript Program to get Query String Values
Getting query string values in JavaScript refers to extracting parameters from a URL after the "?" symbol. It allows developers to capture user input or state information passed through URLs for dynamic web content and processing within a web application. Table of Content Using URLSearchParamsUsing Pure JavaScript Approach 1: Using URLSearchParamsI
2 min read
How to get a number of vowels in a string in JavaScript ?
The Approach of this article is to return the number of vowels in a string using Javascript. A vowel is also a letter that represents a sound produced in this way: The vowels in English are a, e, i, o, u. Example: Input:GeeksForGeeksOutput: 5 Input: Hello GeeksOutput: 4Examples of getting a number of vowels in a string in JavaScript1. Using for loo
4 min read