{"id":335,"date":"2023-11-11T06:13:23","date_gmt":"2023-11-11T06:13:23","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=335"},"modified":"2023-11-11T18:20:16","modified_gmt":"2023-11-11T18:20:16","slug":"string-to-array-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/string-to-array-javascript\/","title":{"rendered":"Convert String to Array in JavaScript (4 Easy Methods)"},"content":{"rendered":"\n<p>In the world of JavaScript programming, there often comes a time when we need to convert string to array. Whether it&#8217;s breaking down a sentence into individual words or splitting a string of characters, you need to know how this as a developer. In this article, we will discuss various techniques on how to convert a string to an array in JavaScript.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What are Arrays in JavaScript?<\/strong><\/h3>\n\n\n\n<p><strong>Arrays are linear data structures that store homogenous data, i.e. same type of data.<\/strong> An array can contain all integers, all characters, or all strings as the elements. An array can not be a combination of integers and characters, or integers or strings. <\/p>\n\n\n\n<p>Let us look at how to declare and use arrays in Javascript:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedbca\"><code>\/\/declaring an integers array\nlet marks = &#91;98, 75, 62, 82, 97, 53];\n\n\/\/declaring a characters array\nlet grades=&#91;'A', 'B', 'A', '0'];\n\n\/\/declaring a strings array\nlet favtutors=&#91;\"John\", \"Joshua\", \"Alex\", \"Kate\"];\n\n\/\/declaring a boolean array\nlet present=&#91;true, false, false, true];<\/code><\/pre>\n\n\n\n<p>In the code above, we have made integers array marks containing all integers, a characters array grade consisting of all elements as characters, a strings array &#8216;favtutors&#8217; consisting of all strings, and a boolean present array consisting of all boolean values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What are Strings in JavaScript?<\/strong><\/h3>\n\n\n\n<p><strong>Strings in JavaScript represent a sequence of multiple characters.<\/strong> \u2018C\u2019 represents a single character and \u201cCat\u201d represents a string. In JavaScript, we can declare a string in two ways. Let us look into each method.<\/p>\n\n\n\n<p>We can declare a string in JavaScript by making a string literal using double quotes. The syntax and example for the same is given below.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a string literal\nlet a=&quot;FavTutor&quot;\n\n\/\/printing the string\nconsole.log(a);<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>FavTutor<\/code><\/pre>\n\n\n\n<p>We can declare a string in JavaScript by creating a String object. An example of the same is given below.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a string using String Object\nlet str=new String(&quot;FavTutor&quot;)\n\n\/\/printing the string\nconsole.log(str);<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; String: \u2018FavTutor\u2019 ]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4 Methods to Convert String to Array<\/strong><\/h2>\n\n\n\n<p>In this section, we will discuss various techniques on how to convert a string to an array in JavaScript.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Using the split() Method<\/strong><\/h3>\n\n\n\n<p>It is the most commonly used approach to convert string to array in JavaScript. The split() method allows you to split a string into an array of substrings based on a specified delimiter. A delimiter is a character (or more than one character) such as a comma, a space, etc that separates two text strings.<\/p>\n\n\n\n<p>The split() method is invoked on a string and takes a delimiter as an argument. It splits the string into an array of substrings at each occurrence of the delimiter. Let us take a look at the example below.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a string literal\nlet str = &quot;Hello FavTutor&quot;;\n\n\/\/using the split function\nlet arr = str.split(&quot;,&quot;);\n\n\/\/Printing the Array\nconsole.log(arr);<\/pre><\/div>\n\n\n\n<p>In the above example, we split the string str at every occurrence of the comma (&#8220;,&#8221;) and store the resulting substrings in the arr array.&nbsp;<\/p>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; \u2018Hello\u2019, \u2018FavTutor\u2019 ]<\/code><\/pre>\n\n\n\n<p>Let us take a look at another example.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a string literal\nlet str = &quot;Hello FavTutor&quot;;\n\n\/\/using the split function\nlet arr = str.split(&quot;,&quot;);\n\n\/\/Printing the Array\nconsole.log(arr);<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; \u2018Hello FavTutor\u2019 ]<\/code><\/pre>\n\n\n\n<p>As the output shows, the array contains only one element as there is no comma delimiter, hence the string components are not separated.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Using the Array.from() Method<\/strong><\/h3>\n\n\n\n<p><strong>One of the easiest methods to convert a string to an array in JavaScript is by using the Array.from() method. <\/strong>This method converts an iterable object or a string into an array. When applied to a string, it creates an array of individual characters. <\/p>\n\n\n\n<p>Let us look at an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a string literal\nlet str = &quot;FavTutor&quot;;\n\n\/\/using the Array.from() function\nlet arr = Array.from(str);\n\n\/\/Printing the Array\nconsole.log(arr);<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; \u2018F\u2019, \u2018a\u2019, \u2018v\u2019. \u2018T\u2019, \u2018u\u2019, \u2018t\u2019, \u2018o\u2019, \u2018r\u2019 ]<\/code><\/pre>\n\n\n\n<p>In this example, we convert the string to an array using the Array.from() method. We have obtained the desired output.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Using the Spread Operator<\/strong><\/h3>\n\n\n\n<p>Another method to convert string to array is by using the spread operator (&#8230;) in JavaScript. It expands the elements of an iterable object or a string into individual values. When applied to a string, it creates an array of individual characters. <\/p>\n\n\n\n<p>Here is example to understand:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a string literal\nlet str = &quot;FavTutor&quot;;\n\n\/\/using the spread operator\nlet arr = [...str];\n\n\/\/Printing the Array\nconsole.log(arr);<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; \u2018F\u2019, \u2018a\u2019, \u2018v\u2019, \u2018T\u2019, \u2018u\u2019, \u2018t\u2019, \u2018o\u2019, \u2018r\u2019 ]<\/code><\/pre>\n\n\n\n<p>In this example, we convert the string to an array using the spread operator (&#8230;). We have obtained the desired output.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4) Using the Object.assign() Operator<\/strong><\/h3>\n\n\n\n<p>The Object.assign() method can also be utilized to convert a string to an array in JavaScript. This method copies the properties of one or more objects to a target object. When used with an empty array as the target object, it converts the string into an array of individual characters. <\/p>\n\n\n\n<p>Let us take an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a string literal\nlet str = &quot;FavTutor&quot;;\n\n\/\/using the spread operator\nlet arr = Object.assign([],str)\n\n\/\/Printing the Array\nconsole.log(arr);<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; \u2018F\u2019, \u2018a\u2019, \u2018v\u2019, \u2018T\u2019, \u2018u\u2019, \u2018t\u2019, \u2018o\u2019, \u2018r\u2019 ]<\/code><\/pre>\n\n\n\n<p>In this example, we pass an empty array as the target object to the Object.assign() method along with the string str. We have obtained the desired output.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this blog, we discussed how to convert string to array in JavaScript using the split(), the Array.from() function, the spread operator, and the Object.assign() method.\u00a0You can check out this article to learn how to <a href=\"https:\/\/favtutor.com\/articles\/set-to-array-javascript\/\"><span style=\"text-decoration: underline;\">convert set to array in javascript.<\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the world of JavaScript programming, there often comes a time when we need to convert string to array. Whether it&#8217;s breaking down a sentence into individual words or splitting a string of characters, you need to know how this as a developer. In this article, we will discuss various techniques on how to convert [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":337,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[9],"tags":[11,13],"class_list":["post-335","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript","tag-javascript-array"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/335\/revisions"}],"predecessor-version":[{"id":379,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/335\/revisions\/379"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/337"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}