{"id":661,"date":"2024-01-03T18:49:00","date_gmt":"2024-01-03T18:49:00","guid":{"rendered":"https:\/\/www.cmsgalaxy.com\/blog\/?p=661"},"modified":"2024-02-05T10:17:13","modified_gmt":"2024-02-05T10:17:13","slug":"javascript-array-methods","status":"publish","type":"post","link":"https:\/\/www.cmsgalaxy.com\/blog\/javascript-array-methods\/","title":{"rendered":"JavaScript Array Methods"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Array Methods:<\/h3>\n\n\n\n<p>JavaScript arrays have many built-in methods for performing common tasks, such as <code>push()<\/code>, <code>pop()<\/code>, <code>shift()<\/code>, <code>unshift()<\/code>, <code>splice()<\/code>, <code>concat()<\/code>, <code>slice()<\/code>, <code>indexOf()<\/code>, <code>includes()<\/code>, <code>filter()<\/code>, <code>map()<\/code>, and many more. These methods make it easy to manipulate and work with arrays efficiently in JavaScript.<\/p>\n\n\n\n<p>JavaScript provides a variety of built-in methods that can be used to manipulate arrays efficiently. Here&#8217;s an overview of some commonly used array methods:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>push()<\/strong>: Adds one or more elements to the end of an array and returns the new length of the array.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>let fruits = &#91;'apple', 'banana', 'orange'];\nfruits.push('kiwi');\nconsole.log(fruits); \/\/ Output: &#91;'apple', 'banana', 'orange', 'kiwi']<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>pop()<\/strong>: Removes the last element from an array and returns that element.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>let fruits = &#91;'apple', 'banana', 'orange'];\nlet lastFruit = fruits.pop();\nconsole.log(lastFruit); \/\/ Output: 'orange'\nconsole.log(fruits);    \/\/ Output: &#91;'apple', 'banana']<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>shift()<\/strong>: Removes the first element from an array and returns that element.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>let fruits = &#91;'apple', 'banana', 'orange'];\nlet firstFruit = fruits.shift();\nconsole.log(firstFruit); \/\/ Output: 'apple'\nconsole.log(fruits);     \/\/ Output: &#91;'banana', 'orange']<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li><strong>unshift()<\/strong>: Adds one or more elements to the beginning of an array and returns the new length of the array.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>let fruits = &#91;'banana', 'orange'];\nfruits.unshift('apple');\nconsole.log(fruits); \/\/ Output: &#91;'apple', 'banana', 'orange']<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\">\n<li><strong>concat()<\/strong>: Combines two or more arrays and returns a new array.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>let fruits = &#91;'apple', 'banana'];\nlet moreFruits = &#91;'orange', 'kiwi'];\nlet allFruits = fruits.concat(moreFruits);\nconsole.log(allFruits); \/\/ Output: &#91;'apple', 'banana', 'orange', 'kiwi']<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"6\">\n<li><strong>slice()<\/strong>: Returns a shallow copy of a portion of an array into a new array.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>let fruits = &#91;'apple', 'banana', 'orange', 'kiwi'];\nlet citrus = fruits.slice(1, 3);\nconsole.log(citrus); \/\/ Output: &#91;'banana', 'orange']<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"7\">\n<li><strong>splice()<\/strong>: Adds or removes elements from an array.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>let fruits = &#91;'apple', 'banana', 'orange', 'kiwi'];\nfruits.splice(2, 1, 'grape', 'pineapple');\nconsole.log(fruits); \/\/ Output: &#91;'apple', 'banana', 'grape', 'pineapple', 'kiwi']<\/code><\/pre>\n\n\n\n<p>Each method serves a specific purpose, making array manipulation tasks more convenient and efficient.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Array Methods: JavaScript arrays have many built-in methods for performing common tasks, such as push(), pop(), shift(), unshift(), splice(), concat(),<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-661","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/comments?post=661"}],"version-history":[{"count":1,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/661\/revisions"}],"predecessor-version":[{"id":662,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/661\/revisions\/662"}],"wp:attachment":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/media?parent=661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/categories?post=661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/tags?post=661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}