<?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: Muhammad Jareer</title>
    <description>The latest articles on DEV Community by Muhammad Jareer (@muhammadjareer).</description>
    <link>https://dev.to/muhammadjareer</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%2F2435362%2F59852372-2677-416b-8a2e-876dab16bbac.png</url>
      <title>DEV Community: Muhammad Jareer</title>
      <link>https://dev.to/muhammadjareer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/muhammadjareer"/>
    <language>en</language>
    <item>
      <title>JavaScript Variables: Understanding var, const, and let</title>
      <dc:creator>Muhammad Jareer</dc:creator>
      <pubDate>Mon, 07 Apr 2025 11:12:00 +0000</pubDate>
      <link>https://dev.to/muhammadjareer/javascript-variables-understanding-var-const-and-let-53cp</link>
      <guid>https://dev.to/muhammadjareer/javascript-variables-understanding-var-const-and-let-53cp</guid>
      <description>&lt;p&gt;In JavaScript, &lt;strong&gt;variables&lt;/strong&gt; are used to store and manage data. They act as containers for values that can be accessed and manipulated throughout your program.  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Three Ways to Declare Variables&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;JavaScript provides three ways to declare variables:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;var&lt;/code&gt;&lt;/strong&gt; → The &lt;strong&gt;older&lt;/strong&gt; way, has function-scoped behavior.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;const&lt;/code&gt;&lt;/strong&gt; → &lt;strong&gt;Block-scoped&lt;/strong&gt;, cannot be reassigned after declaration.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;let&lt;/code&gt;&lt;/strong&gt; → &lt;strong&gt;Block-scoped&lt;/strong&gt;, allows reassignment but prevents redeclaration.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding their differences is crucial for writing &lt;strong&gt;efficient&lt;/strong&gt; and &lt;strong&gt;bug-free&lt;/strong&gt; JavaScript code. Let's dive in!  &lt;/p&gt;




&lt;h2&gt;
  
  
  🔴 &lt;strong&gt;Using &lt;code&gt;var&lt;/code&gt; (Function-Scoped)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;var&lt;/code&gt; keyword was the original way to declare variables in JavaScript. However, it has some quirks due to &lt;strong&gt;function scoping&lt;/strong&gt; and &lt;strong&gt;hoisting&lt;/strong&gt;.  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example: Function Scope of &lt;code&gt;var&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;showNumber&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;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ✅ Accessible inside the function&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;showNumber&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;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ✅ Also accessible outside&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Output:&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="mi"&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Key Notes:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;var&lt;/code&gt; is &lt;strong&gt;function-scoped&lt;/strong&gt;, meaning it is accessible throughout the function.&lt;/li&gt;
&lt;li&gt;Variables declared with &lt;code&gt;var&lt;/code&gt; &lt;strong&gt;can be redeclared&lt;/strong&gt;, leading to potential bugs.&lt;/li&gt;
&lt;li&gt;It gets &lt;strong&gt;hoisted&lt;/strong&gt; to the top of its scope, sometimes causing unexpected behavior.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Use &lt;code&gt;var&lt;/code&gt; only if you must &lt;strong&gt;support older&lt;/strong&gt; JavaScript versions.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🔵 &lt;strong&gt;Using &lt;code&gt;const&lt;/code&gt; (Immutable Variables)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The const keyword, introduced in &lt;strong&gt;ES6&lt;/strong&gt;, is block-scoped and ensures that the variable &lt;strong&gt;cannot be reassigned&lt;/strong&gt; after initialization.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example: Constant Variables with &lt;code&gt;const&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&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;PI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.1416&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;PI&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ✅ Works fine&lt;/span&gt;

&lt;span class="nx"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.14&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ❌ TypeError: Assignment to constant variable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Output:&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mf"&gt;3.1416&lt;/span&gt;
&lt;span class="nx"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Assignment&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;constant&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Key Notes:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;It ensures &lt;strong&gt;data integrity&lt;/strong&gt; by preventing accidental modifications.&lt;/li&gt;
&lt;li&gt;It makes your code &lt;strong&gt;more predictable&lt;/strong&gt; and easier to debug.&lt;/li&gt;
&lt;li&gt;Helps in writing &lt;strong&gt;safer&lt;/strong&gt; and &lt;strong&gt;more readable&lt;/strong&gt; JavaScript.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Use &lt;code&gt;const&lt;/code&gt; &lt;strong&gt;by default&lt;/strong&gt;, unless you know the value will change.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🟢 &lt;strong&gt;Using &lt;code&gt;let&lt;/code&gt; (Block-Scoped &amp;amp; Reassignable)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;let&lt;/code&gt; keyword is an improved version of &lt;code&gt;var&lt;/code&gt;, allowing block-scoped variables that can be &lt;strong&gt;reassigned&lt;/strong&gt; but &lt;strong&gt;not redeclared&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example: Block Scope of &lt;code&gt;let&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&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;testLet&lt;/span&gt;&lt;span class="p"&gt;()&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;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ✅ Accessible within the function&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;testLet&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;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ❌ ReferenceError: number is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Output:&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="nx"&gt;ReferenceError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Key Notes:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;It prevents &lt;strong&gt;variable redeclaration&lt;/strong&gt;, reducing accidental modifications.&lt;/li&gt;
&lt;li&gt;It has &lt;strong&gt;block scope&lt;/strong&gt;, making it more predictable than &lt;code&gt;var&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It does not get &lt;strong&gt;hoisted&lt;/strong&gt; in an unpredictable way like &lt;code&gt;var&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Use &lt;code&gt;let&lt;/code&gt; when you need a variable that can be reassigned but should remain within a limited scope.&lt;/em&gt;&lt;/p&gt;




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

&lt;ul&gt;
&lt;li&gt;Use const for values that shouldn’t change.&lt;/li&gt;
&lt;li&gt;Use let for reassignable values.&lt;/li&gt;
&lt;li&gt;Avoid var unless working with older JavaScript code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To write better code in &lt;strong&gt;JavaScript&lt;/strong&gt;, we need to &lt;strong&gt;understand&lt;/strong&gt; these differences and &lt;strong&gt;MASTER&lt;/strong&gt; them.&lt;/p&gt;




&lt;p&gt;That's it for today!&lt;/p&gt;

&lt;p&gt;Happy Coding! 🤩&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Slice vs Splice in JavaScript: Key Differences Explained</title>
      <dc:creator>Muhammad Jareer</dc:creator>
      <pubDate>Wed, 26 Mar 2025 18:15:45 +0000</pubDate>
      <link>https://dev.to/muhammadjareer/slice-vs-splice-in-javascript-key-differences-explained-45pa</link>
      <guid>https://dev.to/muhammadjareer/slice-vs-splice-in-javascript-key-differences-explained-45pa</guid>
      <description>&lt;p&gt;When working with arrays in JavaScript, two commonly used methods are &lt;code&gt;slice()&lt;/code&gt; and &lt;code&gt;splice()&lt;/code&gt;. While they might seem similar at first glance, they serve very different purposes. In this article, we’ll break down their differences, usage, and when to use each one.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔹 What is &lt;code&gt;slice()&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;slice()&lt;/code&gt; method is used to extract a portion of an array &lt;strong&gt;without modifying the original array&lt;/strong&gt;. It returns a new array containing the selected elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Syntax&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;array&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="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;start&lt;/code&gt;: The index where extraction begins.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;end&lt;/code&gt; (optional): The index where extraction stops (not included in the result).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;let&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;cherry&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;date&lt;/span&gt;&lt;span class="dl"&gt;"&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;citrus&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="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;citrus&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ["banana", "cherry"]&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;fruits&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ["apple", "banana", "cherry", "date"] (unchanged)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  📌 &lt;strong&gt;Key Takeaways:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Returns a &lt;strong&gt;new array&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Does not&lt;/strong&gt; modify the original array.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;end&lt;/code&gt; index is &lt;strong&gt;not included&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔹 What is &lt;code&gt;splice()&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;The splice() method is used to &lt;strong&gt;modify the original array&lt;/strong&gt; by adding or removing elements at a specified position.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Syntax&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;array&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="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deleteCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item2&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;ul&gt;
&lt;li&gt;
&lt;code&gt;start&lt;/code&gt;: The index where the modification begins.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;deleteCount&lt;/code&gt;: The number of elements to remove.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;item1, item2, ...&lt;/code&gt;: Elements to be added (optional).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example: Removing Elements&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;colors&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;red&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;blue&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;green&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;yellow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;colors&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;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;colors&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ["red", "yellow"]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, it removes 2 elements starting from index 1 (&lt;code&gt;"blue"&lt;/code&gt; and &lt;code&gt;"green"&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example: Adding Elements&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;let&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;5&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;splice&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;0&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;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="c1"&gt;// [1, 2, 3, 4, 5]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we insert &lt;code&gt;4&lt;/code&gt; at index &lt;code&gt;3&lt;/code&gt; without removing anything.&lt;/p&gt;

&lt;h4&gt;
  
  
  📌 &lt;strong&gt;Key Takeaways:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Modifies&lt;/strong&gt; the original array.&lt;/li&gt;
&lt;li&gt;Can &lt;strong&gt;remove&lt;/strong&gt; elements.&lt;/li&gt;
&lt;li&gt;Can &lt;strong&gt;add&lt;/strong&gt; elements.&lt;/li&gt;
&lt;/ul&gt;




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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;slice()&lt;/code&gt; returns a new array without
modifying the original.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;splice()&lt;/code&gt; modifies the original array by
adding/removing elements.&lt;/li&gt;
&lt;li&gt;Choose &lt;code&gt;slice()&lt;/code&gt; when you need a portion of
an array without altering it.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;splice()&lt;/code&gt; when you need to change
the array in place.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding these differences will help you write cleaner, more efficient JavaScript code!&lt;/p&gt;

&lt;p&gt;Happy Coding! 🤩&lt;/p&gt;

&lt;p&gt;If you have any questions or suggestions, feel free to connect with me on &lt;a href="https://www.linkedin.com/in/muhammadjareer/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or email me at &lt;a href="mailto:m.ahmar2496@gmail.com"&gt;m.ahmar2496@gmail.com&lt;/a&gt;. You can also check out my &lt;a href="https://jareer-portfolio.vercel.app/" rel="noopener noreferrer"&gt;Portfolio&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>slicevssplice</category>
    </item>
  </channel>
</rss>
