<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Victor Mugisha Shyaka</title>
    <description>The latest articles on DEV Community by Victor Mugisha Shyaka (@victor_mugisha).</description>
    <link>https://dev.to/victor_mugisha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2176679%2Fb2b90114-7f3f-4f97-b145-0cf64e1b660b.jpg</url>
      <title>DEV Community: Victor Mugisha Shyaka</title>
      <link>https://dev.to/victor_mugisha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/victor_mugisha"/>
    <language>en</language>
    <item>
      <title>🧠Mastering Debouncing in TypeScript: Enhance Your App's Performance</title>
      <dc:creator>Victor Mugisha Shyaka</dc:creator>
      <pubDate>Sun, 04 May 2025 16:00:16 +0000</pubDate>
      <link>https://dev.to/victor_mugisha/mastering-debouncing-in-typescript-enhance-your-apps-performance-1cbm</link>
      <guid>https://dev.to/victor_mugisha/mastering-debouncing-in-typescript-enhance-your-apps-performance-1cbm</guid>
      <description>&lt;p&gt;Ever noticed how some applications wait for you to pause typing before fetching or updating search results? That's debouncing in action—a technique that ensures that certain "expensive" operations aren't done too frequently, optimizing performance and resource utilization.&lt;/p&gt;

&lt;p&gt;Expensive operations are those that consume significant amount of computer resources, such as CPU time, memory, or disk I/O, making it slow and potentially impacting the overall performance of a program. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🔍 What is Debouncing?&lt;/strong&gt;&lt;br&gt;
Debouncing delays the execution of a function until a specified time has passed since the last time it was invoked. It's particularly useful for (but not limited to):&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Search Inputs:&lt;/strong&gt; Preventing API calls on every keystroke.&lt;br&gt;
&lt;strong&gt;2. Window Resizing:&lt;/strong&gt; Avoiding performance hits from rapid resize events.&lt;br&gt;
&lt;strong&gt;3. Scrolling Events:&lt;/strong&gt; Ensuring listener's callbacks aren't triggered multiple times every time a user scrolls a page.&lt;/p&gt;

&lt;p&gt;Here's how you can implement this powerful technique:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🛠️ TypeScript Implementation&lt;/strong&gt;&lt;br&gt;
The TypeScript debounce function below creates a reusable utility that wraps any function with a timing mechanism. In the example below, the &lt;code&gt;handleSearch&lt;/code&gt; function is invoked only after the user stops typing for 300 milliseconds, reducing unnecessary function calls and enhancing performance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function debounce&amp;lt;T extends (...args: any[]) =&amp;gt; void&amp;gt;(
  func: T,
  delay: number,
): (...args: Parameters&amp;lt;T&amp;gt;) =&amp;gt; void {
  let timeoutId: ReturnType&amp;lt;typeof setTimeout&amp;gt;
  return (...args: Parameters&amp;lt;T&amp;gt;): void =&amp;gt; {
    clearTimeout(timeoutId)
    timeoutId = setTimeout(() =&amp;gt; func(...args), delay)
  }
}

// Usage Example
const handleSearch = (query: string) =&amp;gt; {
  console.log(`Searching for: ${query}`)
}

const debouncedSearch = debounce(handleSearch, 300)

const searchInput = document.getElementById("search") as HTMLInputElement
searchInput.addEventListener("input", (event) =&amp;gt; {
  debouncedSearch((event.target as HTMLInputElement).value)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a recent project, implementing this pattern reduced our API calls by 85% during search interactions, significantly improving both server load and user experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;💬 Let's Collaborate!&lt;/strong&gt;&lt;br&gt;
Have you implemented debouncing in your projects? Share your specific use cases or how it improved your application's performance metrics in the comments below. Let's learn and grow together! 🚀&lt;/p&gt;

</description>
    </item>
    <item>
      <title>JavaScript Functions for Beginners: Quick Guide</title>
      <dc:creator>Victor Mugisha Shyaka</dc:creator>
      <pubDate>Wed, 16 Oct 2024 11:33:04 +0000</pubDate>
      <link>https://dev.to/victor_mugisha/javascript-functions-for-beginners-quick-guide-423f</link>
      <guid>https://dev.to/victor_mugisha/javascript-functions-for-beginners-quick-guide-423f</guid>
      <description>&lt;p&gt;Functions are one of the fundamental building blocks of JavaScript. They allow you to write reusable pieces of code that can be called multiple times throughout your program. Whether you're just starting out with JavaScript or looking to solidify your understanding, mastering functions is crucial for becoming a proficient developer.&lt;/p&gt;

&lt;p&gt;In this article, we'll explore the basics of JavaScript functions, including:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What functions are and why they're important&lt;/li&gt;
&lt;li&gt;How to declare and define functions&lt;/li&gt;
&lt;li&gt;Different ways to create functions in JavaScript&lt;/li&gt;
&lt;li&gt;Function parameters and arguments&lt;/li&gt;
&lt;li&gt;Return values and their significance&lt;/li&gt;
&lt;li&gt;The concept of function scope&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By the end of this guide, you'll have a solid foundation in working with JavaScript functions and be ready to use them in your own projects.&lt;/p&gt;

&lt;p&gt;Let's get started in exploring the world of JavaScript functions!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What Are Functions and Why Are They Important?
&lt;/h2&gt;

&lt;p&gt;Functions in JavaScript are reusable blocks of code designed to perform a specific task. Think of them as little machines that take input, process it, and produce an output. They are a fundamental concept in programming and serve several important purposes:&lt;/p&gt;

&lt;h3&gt;
  
  
  Reusability
&lt;/h3&gt;

&lt;p&gt;Functions allow you to write a piece of code once and use it multiple times throughout your program. This saves time and reduces the amount of code you need to write.&lt;/p&gt;

&lt;h3&gt;
  
  
  Abstraction
&lt;/h3&gt;

&lt;p&gt;Functions allow you to hide the details of how a task is performed. You can call a function without needing to know how it works internally. This makes your code more organized and easier to understand.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modularity
&lt;/h3&gt;

&lt;p&gt;Functions allow you to break down complex problems into smaller, more manageable pieces. This modular approach makes your code more flexible and easier to maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. How to declare and define functions
&lt;/h2&gt;

&lt;p&gt;There are sevelar ways of creating and defining functions in JavaScript, we have different types of functions which also determines how the function is created according to it's type.&lt;/p&gt;

&lt;p&gt;Before we dive in on how to define functions, there are few things all functions have in common that you should know first:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Function name:&lt;/strong&gt; Almost every function in JavaScript must have a name, otherwise, it wouldn't really be usable. Not all functions have names though, some special types of functions can be created and used without names (we will see this in other chapter).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optional parameters:&lt;/strong&gt; These are inputs that a function can receive and use them to produce output. A function can receive one or more parameters depending on what it does and the input it needs, but they are optional which means you can create a function that produce output without input (sounds interesting!)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function body:&lt;/strong&gt; Every function in JavaScript should have a body. This is a block of statement enclosed in curly braces &lt;code&gt;{}&lt;/code&gt; after the function parameters. It is what a function uses to process the input and produce the result (output).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;function&lt;/code&gt; keyword:&lt;/strong&gt; Again almost every function in JavaScript are defined using the &lt;code&gt;function&lt;/code&gt; keyword, but not all of them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;return&lt;/code&gt; statement:&lt;/strong&gt; The &lt;code&gt;return&lt;/code&gt; statement is used in a function to make the function produce the output, this is how the function produces the result of its calculation or the output of its operation(s). This is statement is also optional in JavaScript, when omitted, a function will return &lt;code&gt;undefined&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function signature:&lt;/strong&gt; This is nothing than the combination of a &lt;code&gt;function&lt;/code&gt; keyword, &lt;code&gt;functionName&lt;/code&gt; and the list of parameters. If you take all these three parts together, they form what we call &lt;strong&gt;function signature&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function call:&lt;/strong&gt; Some people call it function invokation, but I prefer function call. This is when you have defined your function, and then you want to use that function. To do this you write &lt;code&gt;functionName(optionalParameters)&lt;/code&gt; and this will give you the result from the function. For example, &lt;code&gt;printValue('hello')&lt;/code&gt;, this statement calls the function called &lt;code&gt;printValue&lt;/code&gt; and pass &lt;code&gt;'hello'&lt;/code&gt; as parameter to that function.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  a. Functions with &lt;code&gt;function&lt;/code&gt; keyword
&lt;/h3&gt;

&lt;p&gt;This is more popular and straightforward way of creating a function in JavaScript using the &lt;code&gt;function&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Syntax&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;functionName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// function body&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the basic syntax of creating a function. The &lt;code&gt;functionName&lt;/code&gt; can be anything you want just like a variable name, but it is a good practice to name the function after what it does. For example, if a function calcualates the sum of two numbers, you can name it &lt;code&gt;addTwoNumbers&lt;/code&gt; or &lt;code&gt;addNumbers&lt;/code&gt; so that whoever reads the name of the function, they immediately get a basic idea of what your function does.&lt;/p&gt;

&lt;p&gt;Let's see a simple example to help you understand this syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;number2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;number1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;number2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;addNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we defined a function called &lt;code&gt;addNumbers&lt;/code&gt; that accepts two inputs &lt;code&gt;number1&lt;/code&gt; and &lt;code&gt;number2&lt;/code&gt;, and return their sum. Then we called the function with &lt;code&gt;3&lt;/code&gt; and &lt;code&gt;4&lt;/code&gt; hence the function will give us &lt;code&gt;7&lt;/code&gt; in the console.&lt;/p&gt;

&lt;h3&gt;
  
  
  b. Arrow Functions
&lt;/h3&gt;

&lt;p&gt;Arrow functions, introduced in ES6, provide a more concise syntax for writing functions. They are especially useful for short, simple functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Arrow function syntax&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;functionName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// function body&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Example&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;multiplyNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;multiplyNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For simple, one-line functions, you can use an even more concise syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 16&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  c. Function Expressions
&lt;/h3&gt;

&lt;p&gt;Function expressions involve assigning a function to a variable. This can be done with either traditional function syntax or arrow functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Traditional function expression&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Arrow function expression&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;greetArrow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Hello, Alice!&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;greetArrow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Hello, Bob!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Function Parameters and Arguments
&lt;/h2&gt;

&lt;p&gt;Parameters are variables listed in the function definition, while arguments are the actual values passed to the function when it's called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;introduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`My name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and I'm &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;introduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Charlie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// My name is Charlie and I'm 30 years old.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Default Parameters
&lt;/h3&gt;

&lt;p&gt;ES6 introduced default parameters, allowing you to specify default values for parameters if no argument is provided.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greetUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Guest&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Welcome, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;greetUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Welcome, Guest!&lt;/span&gt;
&lt;span class="nf"&gt;greetUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;David&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Welcome, David!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Rest Parameters
&lt;/h3&gt;

&lt;p&gt;The rest parameter syntax allows a function to accept an indefinite number of arguments as an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Return Values
&lt;/h2&gt;

&lt;p&gt;Functions can return values using the &lt;code&gt;return&lt;/code&gt; statement. If no return statement is used, the function returns &lt;code&gt;undefined&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;calculateArea&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;area&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculateArea&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;area&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Function Scope
&lt;/h2&gt;

&lt;p&gt;Variables declared inside a function are only accessible within that function, creating a local scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;exampleScope&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;localVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm local&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;localVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// I'm local&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;exampleScope&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// console.log(localVar); // This would cause an error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. Higher-Order Functions
&lt;/h2&gt;

&lt;p&gt;Higher-order functions are functions that can take other functions as arguments or return functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;applyOperation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;applyOperation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 8&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;applyOperation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding functions is crucial for JavaScript development. They allow you to write reusable, modular, and efficient code. As you continue your JavaScript journey, you'll find that mastering functions opens up powerful programming paradigms and patterns.&lt;/p&gt;

&lt;p&gt;Practice creating and using functions in your projects to solidify your understanding.&lt;/p&gt;

&lt;p&gt;I know this wasn't all about functions, but I hope it gave you a good overview of what functions are and how they work in JavaScript.&lt;/p&gt;

&lt;p&gt;In my next articles, I will be breaking down some of the advanced concepts of functions such as &lt;code&gt;closures&lt;/code&gt;, &lt;code&gt;first-class functions&lt;/code&gt;, &lt;code&gt;higher-order functions&lt;/code&gt;, &lt;code&gt;currying&lt;/code&gt;, &lt;code&gt;partial application&lt;/code&gt;, &lt;code&gt;memoization&lt;/code&gt;, etc. and how they work behind the scenes. Don't miss them out!&lt;/p&gt;

&lt;p&gt;If you found this article helpful, please leave a comment and share it with your friends and colleagues. &lt;br&gt;
And if you wish me to write about something specific, please let me know in the comments below.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>functional</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Arrays In JavaScript: Everything You Need To Know</title>
      <dc:creator>Victor Mugisha Shyaka</dc:creator>
      <pubDate>Mon, 07 Oct 2024 06:47:26 +0000</pubDate>
      <link>https://dev.to/victor_mugisha/arrays-in-javascript-5cnl</link>
      <guid>https://dev.to/victor_mugisha/arrays-in-javascript-5cnl</guid>
      <description>&lt;h1&gt;
  
  
  Arrays in JavaScript
&lt;/h1&gt;

&lt;p&gt;Arrays in JavaScript can initially be confusing and challenging to grasp, especially when dealing with advanced concepts. I, too, struggled with understanding arrays in JavaScript at first. In this article, I aim to demystify arrays in JavaScript, breaking down everything you need to know so you can work with them confidently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Definition
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Is an Array
&lt;/h3&gt;

&lt;p&gt;An array is a data structure that stores a collection of elements, each accessible by an index. In many programming languages, arrays store elements in contiguous memory locations. In JavaScript, arrays are high-level, list-like objects used to store multiple values in a single variable. They are zero-indexed, meaning the first element is at index &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Arrays in JavaScript
&lt;/h3&gt;

&lt;p&gt;Since JavaScript is a dynamically typed language, arrays can hold elements of different types. An array can contain numbers, strings, booleans, objects, and even other arrays. This differs from statically typed languages like &lt;code&gt;Java&lt;/code&gt; or &lt;code&gt;C++&lt;/code&gt;, where arrays are typically homogeneous and must contain elements of the same data type.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use an array?
&lt;/h2&gt;

&lt;p&gt;Arrays help manage data efficiently by allowing you to store, access and manipulate values. They are used for anything that involves lists or collections, like managing items in an e-commerce cart or keeping track of user inputs.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create an array in JavaScript
&lt;/h2&gt;

&lt;p&gt;There are multiple ways to create arrays in JavaScript, I will go over few of them:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Using array literals &lt;code&gt;[]&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In JavaScript, you can simply create an array by assigning &lt;code&gt;[]&lt;/code&gt; to a variable or a constant&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Apple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Banana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Orange&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that elements of an array are separated by comma &lt;code&gt;,&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Using &lt;code&gt;Array&lt;/code&gt; constructor
&lt;/h3&gt;

&lt;p&gt;You can also create an array as an instance of a native JavaScript &lt;code&gt;Array&lt;/code&gt; constructor using the following this syntax:&lt;br&gt;
&lt;code&gt;const myArray = new Array()&lt;/code&gt;&lt;br&gt;
The &lt;code&gt;Array&lt;/code&gt; constructor takes one or more arguments (they have to be numbers), and it behaves differently according to the numbers of arguments you pass in!&lt;br&gt;
If you pass one argument: &lt;code&gt;const myArray = new Array(4)&lt;/code&gt;, this will create a new array with 4 empty elements!&lt;/p&gt;

&lt;p&gt;If you pass more that one arguments: &lt;code&gt;const myArray = new Array(1, 2, 3)&lt;/code&gt;, this creates an array of 3 numbers &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt; respectively and it is simiral to writing &lt;code&gt;const myArray = [1, 2, 3]&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;I know this looks confusing but trust me it is easy if you make some more exercises with them!&lt;/p&gt;
&lt;h3&gt;
  
  
  3. Using &lt;code&gt;Array.of&lt;/code&gt; method
&lt;/h3&gt;

&lt;p&gt;Arrays can also be created using this method &lt;code&gt;Array.of&lt;/code&gt; and it will just create an array containing all elements you pass to it as arguments. The difference betwee using &lt;code&gt;Array.of&lt;/code&gt; and &lt;code&gt;Array&lt;/code&gt; constructor lies in how they behave when they receive one argument!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This returns [1]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This returns [2, true, 'hello']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how &lt;code&gt;Array.of&lt;/code&gt; behaves when it receives one parameter, unlike &lt;code&gt;Array&lt;/code&gt; constructor which just creates an array of &lt;code&gt;n&lt;/code&gt; empty elements, the &lt;code&gt;Array.of&lt;/code&gt; just creates an array with single element which is the one you passed in!&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Using &lt;code&gt;Array.from&lt;/code&gt; method
&lt;/h3&gt;

&lt;p&gt;You can also use &lt;code&gt;Array.from(iterable)&lt;/code&gt; method, which receives one argument which also must be an iterable and creates an array out of it! For example, you can pass a set to this method and creates an array from the values of that set!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mySet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrayFromSet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mySet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Arrays manipulation in JavaScript
&lt;/h2&gt;

&lt;p&gt;We've seen what is an array and how you can create arrays in JavaScript, now the remaining problem you might be asking yourself is, how do I use and work with arrays?&lt;br&gt;
Well, that's a good question!&lt;/p&gt;
&lt;h3&gt;
  
  
  Traditional way of working with arrays in JavaScript
&lt;/h3&gt;

&lt;p&gt;Traditionally, we use loops to iterate over the elements stored in an array and use them!&lt;br&gt;
The following example shows how you can loop over an array of numbers and display the double of every number inside the array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using modern JavaScript array method syntax
&lt;/h3&gt;

&lt;p&gt;JavaScript comes with many array methods (higher-order functions) out of the box, they are accessible to all array instances through &lt;code&gt;Array.prototype&lt;/code&gt; object, we use those methods provided by JavaScript to manipulate and work with data stored in arrays! We can even create our own array methods (we will look at this on next chapter)&lt;/p&gt;

&lt;p&gt;They are higher-order methods, because they take other functions as arguments and use those functions to manipulate the data stored in arrays!&lt;/p&gt;

&lt;p&gt;Those array methods are also categorized into two categories:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Mutating:&lt;/strong&gt; These loop over the array and while applying the callback function, they also mutate the original array!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-mutating:&lt;/strong&gt; These iterate over the array and apply the callback function, but instead of mutating the original array, they return new array&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  1. &lt;code&gt;forEach()&lt;/code&gt; method
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;forEach()&lt;/code&gt; is an array higher-order method used to loop over elements of an array and apply a callback function on every element without either modifying the original array or creating a new array!&lt;/p&gt;

&lt;p&gt;The syntax of the callback functions which is usually anonymous function (usually the same in all other methods):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentElement&lt;/span&gt;&lt;span class="p"&gt;[,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;targetArray&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means that on every iteration, the array method has access to the current element in iteration (&lt;code&gt;currentElement&lt;/code&gt;), it's index in the array &lt;code&gt;index&lt;/code&gt; which is optional, and the reference to the target array in which the method is being executed over &lt;code&gt;targetArray&lt;/code&gt; which is also optional. But you can call those parameters however you want, you just need to pay attention on their positions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. &lt;code&gt;map()&lt;/code&gt; method
&lt;/h4&gt;

&lt;p&gt;Just like &lt;code&gt;forEach&lt;/code&gt; map is also another way of iterating over the elements of the array and apply the callback function on every element, but unlike &lt;code&gt;forEach&lt;/code&gt; map is non-mutating method and hence it creates and return the new array as a result of iteration and does not change the original array!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;squaredNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;squaredNumbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. &lt;code&gt;filter()&lt;/code&gt; method
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;filter()&lt;/code&gt; method is used when you want to filter the array by removing the elements you don't want, this is non-mutating method and it returns a new array!&lt;br&gt;
For every iteration, the callback inside the &lt;code&gt;filter()&lt;/code&gt; must return a boolean value indicating whether or not the current element is to be included in the new filtered array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;evenNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;evenNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [2, 4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  4. &lt;code&gt;reduce()&lt;/code&gt; method
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;reduce()&lt;/code&gt; method is a bit different from those above. Think of it as a way to combine all the elements in an array into a single value by applying a function to each element, one by one. It returns a single value from all elements of the array, you will use it when you want a single value from all elements of an array like sum, average, maximum or minimum just to name a few!&lt;/p&gt;

&lt;p&gt;The syntax is a also different&lt;br&gt;
&lt;code&gt;Array.reduce(function(accumulator, element[, index, targetArray]) [, initialValue])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This method takes two arguments, the first one is the callback function just like other methods, and the second one will be the initial value of the &lt;code&gt;accumutator&lt;/code&gt; variable. This second argument is optional, if it is not provided, the reduce will use the first element of an array as the initial value of &lt;code&gt;accumulator&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;accumulator&lt;/code&gt; holds the result returned by the callback function on each iteration and will be the final result returned from the &lt;code&gt;reduce&lt;/code&gt; once the iteration is over!&lt;/p&gt;

&lt;p&gt;Let's try to find a sum from an array of numbers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  4. &lt;code&gt;slice()&lt;/code&gt; method
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;slice()&lt;/code&gt; is another useful array method in JavaScript, which is used to extract a small portion of an array! &lt;code&gt;slice()&lt;/code&gt; creates a new array by copying a section of an existing array without modifying the original array.&lt;/p&gt;

&lt;p&gt;The syntax for the &lt;code&gt;slice()&lt;/code&gt; method is as the following:&lt;br&gt;
&lt;code&gt;Array.slice(startIndex, endIndex)&lt;/code&gt; &lt;code&gt;startIndex&lt;/code&gt; represents a starting point of extraction and it is inclusive and the &lt;code&gt;endIndex&lt;/code&gt; represents the ending element of extraction, it is optional and exclusive. When it is not provided, the &lt;code&gt;slice&lt;/code&gt; methods copies an array from &lt;code&gt;startIndex&lt;/code&gt; to the end of an array!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;apple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;banana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orange&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mango&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;slicedFruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ['banana', 'orange']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  5. &lt;code&gt;splice()&lt;/code&gt; method
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;splice()&lt;/code&gt; is an array method in JavaScript, which is used for adding, removing and replacing elements in array. &lt;code&gt;splice()&lt;/code&gt; changes the content of an array by adding, removing or replacing elements in array.&lt;/p&gt;

&lt;p&gt;The syntax for the &lt;code&gt;splice()&lt;/code&gt; method is as the following:&lt;br&gt;
&lt;code&gt;Array.splice(index, elementsToRemove, newElement1, newElement2, ..., newElementn)&lt;/code&gt;&lt;br&gt;
This might look confusing but, let me try to explain:&lt;br&gt;
&lt;code&gt;index&lt;/code&gt; means the starting index in the array in which the removing of element is supposed to begin and it is inclusive.&lt;br&gt;
&lt;code&gt;elementsToRemove&lt;/code&gt; represents the number of elements from &lt;code&gt;index&lt;/code&gt; in which you want to remove from array, if you only want to add new elements, you can provide &lt;code&gt;0&lt;/code&gt; in this position.&lt;br&gt;
&lt;code&gt;newElement1&lt;/code&gt;, &lt;code&gt;newElement2&lt;/code&gt; etc. These can be any number of elements you want to add in your array and they will replace &lt;code&gt;elementsToRemove&lt;/code&gt; elements you specified in your array!&lt;/p&gt;

&lt;p&gt;Much of talking, let's see an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;apple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;banana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orange&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mango&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="c1"&gt;// If we want to replace 'banana' with 'pineapple'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;splicedFruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pineapple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will return ['apple', 'pineapple', 'orange', 'mango']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;fruits.splice(1, 1, "pineapple")&lt;/code&gt; this means from index of &lt;code&gt;1&lt;/code&gt;, remove &lt;code&gt;1&lt;/code&gt; elements and add &lt;code&gt;'pineapple'&lt;/code&gt;&lt;br&gt;
If we only wanted to add &lt;code&gt;pineapple&lt;/code&gt; in array not replacing other value with it, we could have written this as&lt;br&gt;
&lt;code&gt;fruits.splice(1, 0, "pineapple")&lt;/code&gt; this would add &lt;code&gt;pineapple&lt;/code&gt; after element at index&lt;code&gt;1&lt;/code&gt; and will not remove any other element from this array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Arrays are a fundamental and versatile feature in JavaScript, offering an essential structure to store and manipulate collections of data. From simple array creation using literals to more advanced methods like &lt;code&gt;Array.of()&lt;/code&gt; and &lt;code&gt;Array.from()&lt;/code&gt;, JavaScript arrays provide a range of ways to handle different types of data. By mastering array manipulation through loops or modern methods like &lt;code&gt;forEach()&lt;/code&gt;, &lt;code&gt;map()&lt;/code&gt;, &lt;code&gt;filter()&lt;/code&gt;, &lt;code&gt;reduce()&lt;/code&gt;, &lt;code&gt;slice()&lt;/code&gt;, and &lt;code&gt;splice()&lt;/code&gt;, you can efficiently perform complex operations. Understanding these array features is key to becoming proficient in JavaScript, allowing you to write cleaner, more concise, and powerful code.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
