{"id":136262,"date":"2024-01-30T17:15:52","date_gmt":"2024-01-30T17:15:52","guid":{"rendered":"https:\/\/www.sushilkumar.ind.in\/blog\/?p=136262"},"modified":"2024-01-30T17:19:57","modified_gmt":"2024-01-30T17:19:57","slug":"how-many-ways-have-to-initialise-a-javascript-array","status":"publish","type":"post","link":"https:\/\/www.sushilkumar.ind.in\/blog\/javascript\/how-many-ways-have-to-initialise-a-javascript-array\/","title":{"rendered":"How many ways have to initialise a JavaScript Array?"},"content":{"rendered":"\n<p><br>Sure, there are several ways to initialize a JavaScript array. Here are some common methods:<\/p>\n\n\n\n<p>1. **Using an array literal**: The simplest way to create an array is by using square brackets `[]`:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   const arr1 = &#91;]; \/\/ Empty array\n   const arr2 = &#91;1, 2, 3]; \/\/ Array with elements<\/code><\/pre>\n\n\n\n<p>2. **Using the `Array()` constructor**: You can use the `Array()` constructor to create an array with a specific length or with initial elements:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    const arr3 = new Array(); \/\/ Empty array\n    const arr4 = new Array(3); \/\/ Array with length 3, but no elements\n    const arr5 = new Array('a', 'b', 'c'); \/\/ Array with initial elements\n<\/code><\/pre>\n\n\n\n<p>3. **Using `Array.from()`:** You can create an array from an array-like or iterable object, or from an iterable function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    const arr6 = Array.from('hello'); \/\/ Creates an array &#91;'h', 'e', 'l', 'l', 'o']\n    const arr7 = Array.from({ length: 5 }, (_, index) => index); \/\/ Creates an array &#91;0, 1, 2, 3, 4]<\/code><\/pre>\n\n\n\n<p>4. **Using spread syntax (`&#8230;`)**: You can use spread syntax to create a new array by concatenating existing arrays or adding new elements:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    const arr8 = &#91;...arr2]; \/\/ Creates a shallow copy of arr2\n    const arr9 = &#91;...arr2, 4, 5, 6]; \/\/ Creates a new array by concatenating arr2 with additional elements<\/code><\/pre>\n\n\n\n<p>5. **Using `fill()`:** You can create an array with a specified value repeated for a certain length:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    const arr10 = new Array(5).fill(0); \/\/ Creates an array &#91;0, 0, 0, 0, 0]<\/code><\/pre>\n\n\n\n<p>6. **Using `Array.of()`:** You can create an array from a list of arguments:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> const arr11 = Array.of(1, 2, 3); \/\/ Creates an array &#91;1, 2, 3]<\/code><\/pre>\n\n\n\n<p>These are some of the most common ways to initialize arrays in JavaScript, each with its own specific use case and advantages.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sure, there are several ways to initialize a JavaScript array. Here are some common methods: 1. **Using an array literal**: The simplest way to create an array is by using square brackets `[]`: const arr1 = &#91;]; \/\/ Empty array const arr2 = &#91;1, 2, 3]; \/\/ Array with elements 2. **Using the `Array()` constructor**: &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[323],"tags":[],"class_list":["post-136262","post","type-post","status-publish","format-standard","","category-javascript"],"jetpack_publicize_connections":[],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p99pkJ-zrM","_links":{"self":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136262"}],"collection":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/comments?post=136262"}],"version-history":[{"count":2,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136262\/revisions"}],"predecessor-version":[{"id":136264,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136262\/revisions\/136264"}],"wp:attachment":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/media?parent=136262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/categories?post=136262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/tags?post=136262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}