<?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: Anwar</title>
    <description>The latest articles on DEV Community by Anwar (@anwarr_________).</description>
    <link>https://dev.to/anwarr_________</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%2F493531%2Fc6888e99-b030-494f-874e-951058a91619.jpg</url>
      <title>DEV Community: Anwar</title>
      <link>https://dev.to/anwarr_________</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anwarr_________"/>
    <language>en</language>
    <item>
      <title>primitives vs objects in JAVASCRIPT : Call by sharing</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Fri, 26 Mar 2021 05:18:23 +0000</pubDate>
      <link>https://dev.to/anwarr_________/primitives-vs-objects-in-javascript-function-arguments-2d0g</link>
      <guid>https://dev.to/anwarr_________/primitives-vs-objects-in-javascript-function-arguments-2d0g</guid>
      <description>&lt;p&gt;Previously in this series, we've discussed how primitives and objects behave when we try to mutate or copy them. &lt;br&gt;
Now in the final article in this series we're going to see how they behave when we pass them to functions.&lt;/p&gt;
&lt;h1&gt;
  
  
  Recap
&lt;/h1&gt;

&lt;p&gt;First article we talked about mutability, which is changing the properties of something without changing it as a whole and we said that objects are mutable while primitives aren't, here's a quick example to demo what I've just said&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4wo6d37442uqt4q5814r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4wo6d37442uqt4q5814r.png" alt="mutability example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Second article was all about copying, when you copy a primitive you copy its value but when you copy an object you copy its reference(its address in memory),here's another quick example: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnxg09szzwgtjjgba3kqo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnxg09szzwgtjjgba3kqo.png" alt="copying example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Moving on to talk about how both primitives and objects behave when we pass them to functions as arguments but first, let's talk about functions.&lt;/p&gt;
&lt;h1&gt;
  
  
  functions
&lt;/h1&gt;

&lt;p&gt;Starting with a really typical function, the function &lt;code&gt;add&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuq2na7jk6r0vrl7hlpdw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuq2na7jk6r0vrl7hlpdw.png" alt="function example"&gt;&lt;/a&gt;&lt;br&gt;
now let's walk through this code to see how javascript is run.&lt;br&gt;
Two main things that a javascript engine does are storing stuff and executing orders, let's see this in action.&lt;br&gt;
The javascript engine reads the code from top to bottom so in our example here the first thing it sees is this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(x , y){
  return x + y;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this is called a &lt;strong&gt;function declaration&lt;/strong&gt;, when the engine see &lt;br&gt;
a function declaration it saves all its code inside the &lt;strong&gt;global memory&lt;/strong&gt; and it doesn't execute any of the stuff inside the function because the function hasn't been called yet(remember we're talking about the first 3 lines) &lt;/p&gt;

&lt;p&gt;Moving on to line 4 and 5 in which variables &lt;code&gt;num_1&lt;/code&gt; and num_2 respectively are also stored in global memory&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2qsqtndlk3nug6cpf2nz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2qsqtndlk3nug6cpf2nz.png" alt="global memory"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Notes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in the picture above the letter &lt;em&gt;f&lt;/em&gt; is short for the function's code&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;num_1&lt;/code&gt; and &lt;code&gt;num_2&lt;/code&gt; are stored to demo that the engine also saves variables not functions only&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  execution context
&lt;/h3&gt;

&lt;p&gt;Right now we've done nothing but storing stuff in global memory, on reaching the line &lt;code&gt;add(2,6)&lt;/code&gt; the function &lt;code&gt;add&lt;/code&gt; starts to execute.&lt;br&gt;
when a function is called, an execution context is created and execution context is a place in which information about the function are stored.&lt;br&gt;
It stores stuff like the function's local variables, at what line the program is at the moment.&lt;br&gt;
so as you see in the following picture the function &lt;code&gt;add&lt;/code&gt; has two local variables &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; and they store the numbers we provided when we call the function&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F73u85q6ijaw019zkujns.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F73u85q6ijaw019zkujns.png" alt="execution context"&gt;&lt;/a&gt;&lt;br&gt;
currently we're inside the function body, which is the line &lt;code&gt;return x+y;&lt;/code&gt; the program calculates the value of &lt;code&gt;x + y&lt;/code&gt; which is &lt;code&gt;2 + 6&lt;/code&gt; in this case and that's equal to &lt;code&gt;8&lt;/code&gt;.&lt;br&gt;
Then the function returns this value(8) and once we return, the function's execution context goes away it vanishes and if we called the function again a new execution context will be created and that's a rule: &lt;strong&gt;for every function call a new execution context is created and once we hit a return statement that execution context disappears&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;things stored in global memory are available everywhere in our program. On the other hand, local variables are available only inside the function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And that's it for functions in this article, there's one more thing about functions behind the scenes which is the call stack&lt;br&gt;
if you want to learn about it, go watch &lt;a href="https://www.youtube.com/watch?v=W8AeMrVtFLY" rel="noopener noreferrer"&gt;this video&lt;/a&gt; or read &lt;a href="https://www.javascripttutorial.net/javascript-call-stack/" rel="noopener noreferrer"&gt;this article&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Primitives as functions arguments
&lt;/h1&gt;

&lt;p&gt;consider the following example&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxkcxxh1c7vj9pojaowdp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxkcxxh1c7vj9pojaowdp.png" alt="Primitives as functions arguments"&gt;&lt;/a&gt;&lt;br&gt;
the question is: will the values of &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;language&lt;/code&gt; change ? &lt;br&gt;
let's see what the console will tell us&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ktmqkahu5e8h52fex0e.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ktmqkahu5e8h52fex0e.jpg" alt="results"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, nothing has changed that's because when we pass a primitive to a function , we're passing a &lt;strong&gt;copy of its value&lt;/strong&gt; &lt;br&gt;
so &lt;code&gt;num&lt;/code&gt; and &lt;code&gt;str&lt;/code&gt; have the same values as &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;language&lt;/code&gt; but they're different variables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7m4ctcyeox4bi34ynez4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7m4ctcyeox4bi34ynez4.png" alt="memory visualization"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An important thing to note is that the values of &lt;code&gt;num&lt;/code&gt; and &lt;code&gt;str&lt;/code&gt; are changed and we can see that if we put a &lt;code&gt;console.log&lt;/code&gt; inside the function &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F72la7q7evbwpc2gucj6i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F72la7q7evbwpc2gucj6i.png" alt="example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  call by value
&lt;/h1&gt;

&lt;p&gt;when you pass a primitive to a function, its value is copied to the function argument hence the primitive and the argument have the same value but they're two different variables, that's what called &lt;strong&gt;call by value&lt;/strong&gt; .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ful1q8l9xeoduf0x15292.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ful1q8l9xeoduf0x15292.jpg" alt="call by value"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  objects as functions arguments
&lt;/h1&gt;

&lt;p&gt;remember that objects are stored by reference which is the memory address of the object so when you say &lt;code&gt;let obj = {};&lt;/code&gt; you storing the address of &lt;code&gt;{}&lt;/code&gt; inside &lt;code&gt;obj&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's see what happens when we pass an object to a function&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ichzq36yitq0x6kszmt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ichzq36yitq0x6kszmt.png" alt="objects as functions arguments"&gt;&lt;/a&gt;&lt;br&gt;
do you expect &lt;code&gt;obj&lt;/code&gt; to change or remain the same ? &lt;br&gt;
well, there's only one way to find out&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1d41zzqsxkmxf2vno8mx.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1d41zzqsxkmxf2vno8mx.jpg" alt="result"&gt;&lt;/a&gt;&lt;br&gt;
 As our console said, &lt;code&gt;obj&lt;/code&gt; does change.&lt;br&gt;
 But what if we tried the following: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsm92omsatt3nq5k2x2id.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsm92omsatt3nq5k2x2id.png" alt="reassigning objects using functions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case nothing will change and here's the output to demo that&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3px84egq874ahbhi5d6c.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3px84egq874ahbhi5d6c.jpg" alt="result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  why is that ?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;When you pass an object to a function, you are passing a copy of the reference and you can use that copy to access and modify the object but you can't change the reference using the function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx90ap10b5s8aw8rwy5wj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx90ap10b5s8aw8rwy5wj.png" alt="mutate objects with functions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F665fu631lui44wd9l8e5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F665fu631lui44wd9l8e5.png" alt="reassign objects with functions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  call by sharing
&lt;/h1&gt;

&lt;p&gt;if you pass an object to a function, you can &lt;strong&gt;mutate&lt;/strong&gt; the object inside the function but you can't *&lt;em&gt;reassign *&lt;/em&gt; it.&lt;br&gt;
That's called call by sharing, many people say that objects are called by reference that's not right because call by reference means passing the reference itself and hence you can both mutate and reassign the object using the function and that's not the case in javascript.&lt;br&gt;
We're passing a copy of the reference not the reference itself, &lt;br&gt;
it's like primitives when we pass a copy of the value not the value itself.&lt;/p&gt;

&lt;p&gt;Hope this makes sense for you, you can read about call by sharing using the following links: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://wafy.me/tech/2016/07/02/call-by-sharing.html" rel="noopener noreferrer"&gt;https://wafy.me/tech/2016/07/02/call-by-sharing.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's it for this article and the whole series, hope you guys have enjoyed the series !&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>primitives vs objects in JAVASCRIPT : copying</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Thu, 11 Mar 2021 17:27:30 +0000</pubDate>
      <link>https://dev.to/anwarr_________/primitives-vs-objects-in-javascript-copying-2o0i</link>
      <guid>https://dev.to/anwarr_________/primitives-vs-objects-in-javascript-copying-2o0i</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/anwarr_________/primitives-vs-objects-in-javascript-mutability-2poo"&gt;previous article&lt;/a&gt; we talked about mutability and saw how primitives can't be mutated while objects can. &lt;br&gt;
In this article we're going to see how primitives and objects behave when we try to copy them.&lt;/p&gt;
&lt;h1&gt;
  
  
  Copying primitives
&lt;/h1&gt;

&lt;p&gt;let's start with something simple, let's create a variable &lt;code&gt;x&lt;/code&gt; that has a value of two&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = 2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we wanted to represent this variable visually, it's like a box with some value inside of it so here's how it might look(P.S : I'm not a designer 🙃)&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm7m0qfs3lwkt0tqr2mfz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm7m0qfs3lwkt0tqr2mfz.png" alt="visual representation of a variable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Second thing I'd like to do is make a copy of &lt;code&gt;x&lt;/code&gt; and log it to the console&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;y = x;
console.log(y); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and here the result, it works just fine&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnjm64sfwl12fr6qyh9ud.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnjm64sfwl12fr6qyh9ud.png" alt="result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the question is, If we changed the value of &lt;code&gt;x&lt;/code&gt; would that affect &lt;code&gt;y&lt;/code&gt; ? let's see&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = 100;
console.log(x , y); // what's the output??
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The change in &lt;code&gt;x&lt;/code&gt; won't affect the value stored in &lt;code&gt;y&lt;/code&gt; at all, here's the output if you don't believe me&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7b85g51igjq2lto9xjb4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7b85g51igjq2lto9xjb4.png" alt="result"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Our first rule
&lt;/h2&gt;

&lt;p&gt;In the statement &lt;code&gt;let y = x;&lt;/code&gt; we've created a new box &lt;code&gt;y&lt;/code&gt; and &lt;strong&gt;&lt;em&gt;copied&lt;/em&gt;&lt;/strong&gt; the value of &lt;code&gt;x&lt;/code&gt; inside it.&lt;br&gt;
We've two independent boxes and that makes sense, if you have two copies of the same book cutting a page form one won't affect the other.&lt;br&gt;
so our first rule is &lt;strong&gt;Primitive and its copy are two different variables with the same value&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febpig7ueloccnrcpeyou.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febpig7ueloccnrcpeyou.png" alt="copying primitive"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  practice time
&lt;/h2&gt;

&lt;p&gt;Try to figure out the output of the following(&lt;em&gt;answered in the comments below, but try to figure it out yourself&lt;/em&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let str = "hello world";
let copy = str;
str.toUpperCase();
console.log(str , copy); // what's the output ?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Copying objects
&lt;/h1&gt;

&lt;p&gt;Objects are stored and copied differently, they're stored and copied by &lt;em&gt;reference&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What's a reference ?
&lt;/h2&gt;

&lt;p&gt;It's simply &lt;strong&gt;an address in memory&lt;/strong&gt;, when you initialize some variable (&lt;code&gt;obj&lt;/code&gt; for example) and assign it an object you're storing the &lt;strong&gt;address&lt;/strong&gt; of that object in memory and when you try to perform an operation on that object it's like telling Javascript to go to that address and perform some operation.&lt;/p&gt;

&lt;p&gt;I like to think about references like I'm looking for &lt;code&gt;{name : "anwar" , age:19}&lt;/code&gt; inside the Javascripts engine's memory and the variable &lt;code&gt;obj&lt;/code&gt; telling me "hey, the object you're looking for is right over there"&lt;/p&gt;

&lt;p&gt;enough with the theoretical talk and let's look at an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = {name : "anwar" , age:19};
obj.age++;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code the variable &lt;code&gt;obj&lt;/code&gt; is a &lt;strong&gt;reference(address in memory)&lt;/strong&gt; to &lt;code&gt;{name : "anwar" , age:19}&lt;/code&gt; this is how it might look&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbwepzc9ip4qi6f2pnzrg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbwepzc9ip4qi6f2pnzrg.png" alt="objects"&gt;&lt;/a&gt;&lt;br&gt;
now the line &lt;code&gt;obj.age++;&lt;/code&gt; remember &lt;code&gt;obj&lt;/code&gt; is the &lt;strong&gt;reference(again, an address in memory)&lt;/strong&gt; so this statement means : "go to the object at the following address and increment its &lt;code&gt;age&lt;/code&gt; property by one"&lt;/p&gt;
&lt;h2&gt;
  
  
  what happens when you copy an object ?
&lt;/h2&gt;

&lt;p&gt;previously I mentioned that objects are copied by &lt;strong&gt;REFERENCE&lt;/strong&gt; it means that when you copy an object you're copying the reference of the that object, the object itself isn't duplicated.&lt;/p&gt;

&lt;p&gt;for example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = {name : "anwar" , age: 19};
let copy = obj;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this is how it looks &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo7etjwklllsjzfxjcxf7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo7etjwklllsjzfxjcxf7.png" alt="copying objects"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;obj&lt;/code&gt; and &lt;code&gt;copy&lt;/code&gt; are two variable that store references to the same object, they both point to the same object&lt;/p&gt;
&lt;h2&gt;
  
  
  our second rule
&lt;/h2&gt;

&lt;p&gt;Now if we wanted to access or modify &lt;code&gt;{name : "anwar" , age: 19}&lt;/code&gt; we can do that using the variable &lt;code&gt;obj&lt;/code&gt; or &lt;code&gt;copy&lt;/code&gt; as they're referencing the same object.&lt;br&gt;
so our second rule &lt;strong&gt;an object and its copy are references to the exact same object&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;so if we &lt;em&gt;mutated&lt;/em&gt; &lt;code&gt;obj&lt;/code&gt; the same change will happen in &lt;code&gt;copy&lt;/code&gt;(and vice versa) because they reference the same object&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;remember, mutating is changing object properties without changing the object as a whole(changing the object as a whole is reassignment)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = {name : "anwar" , age: 19};
let copy = obj;
obj.age++;
console.log(copy); // {name : "anwar" , age: 20}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;what if we did the following, what is the output ?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = {name : "anwar" , age: 19};
let copy = obj;
obj = {};
console.log(obj , copy); //??
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the output will be &lt;code&gt;{}&lt;/code&gt; &lt;code&gt;{name : "anwar" , age: 19}&lt;/code&gt; because in the line &lt;code&gt;obj = {};&lt;/code&gt; we're not mutating &lt;code&gt;obj&lt;/code&gt; we're making it reference another object (reassigning it)&lt;/p&gt;

&lt;p&gt;So remember mutating one copy mutates all copies, reassigning one copy doesn't affect other copies.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk96dzj116tsi6vnrk85w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk96dzj116tsi6vnrk85w.png" alt="reassigning copies"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  comparing objects
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Third rule
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;objects are compared by reference, meaning that objects are equal only if they have the same reference&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1,2,3];
let arr_2 = [1,2,3];
console.log(arr === arr_2); // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Javascript compares the references of both objects(arrays are objects) and these two arrays don't have the same address in memory so the result is &lt;code&gt;false&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff6x55suksu82pav6s2tv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff6x55suksu82pav6s2tv.png" alt="comparing objects"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  cloning objects
&lt;/h1&gt;

&lt;p&gt;you might want to make a copy of an object that doesn't have the same reference so you can change stuff in one place without affecting other places, that's called &lt;strong&gt;object cloning&lt;/strong&gt; and you can read about it &lt;a href="https://www.javascripttutorial.net/object/3-ways-to-copy-objects-in-javascript/" rel="noopener noreferrer"&gt;here&lt;/a&gt; or &lt;a href="https://flaviocopes.com/how-to-clone-javascript-object/#deep-copy-vs-shallow-copy" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;And that's it for this article, hope you understand it and find it helpful &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>primitives vs objects in JAVASCRIPT : mutability</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Thu, 04 Mar 2021 15:10:15 +0000</pubDate>
      <link>https://dev.to/anwarr_________/primitives-vs-objects-in-javascript-mutability-2poo</link>
      <guid>https://dev.to/anwarr_________/primitives-vs-objects-in-javascript-mutability-2poo</guid>
      <description>&lt;p&gt;Knowing how things behave is really important in programming as it gives a better understanding of the code you write and &lt;br&gt;
it helps fixing bugs easily.&lt;/p&gt;

&lt;p&gt;This article is the first of a 3 parts series that explains how primitives and objects behave differently in the context of : mutability , copying(value vs reference) , being passed to functions.&lt;/p&gt;

&lt;p&gt;first things first let's define primitives and objects: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;primitives are strings , numbers , booleans , null , &lt;br&gt;
undefined , symbols , and bigInts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;objects are arrays and functions, and of course objects 😀&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  MUTABILITY
&lt;/h1&gt;

&lt;p&gt;always easier to understand with an example, let's create a string called &lt;code&gt;me&lt;/code&gt; and give it the value &lt;code&gt;awesome&lt;/code&gt; and an array of five numbers&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frow5mlvinayaoytq4q96.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frow5mlvinayaoytq4q96.png" alt="Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now let's do some modifications, in &lt;code&gt;me&lt;/code&gt; I want the first character to be &lt;code&gt;A&lt;/code&gt; and in &lt;code&gt;nums&lt;/code&gt; I want the first number to be &lt;code&gt;0&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnfl8yksi0g3s9ucyv9o0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnfl8yksi0g3s9ucyv9o0.png" alt="example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;result :&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsb26ehdao53ey33ofn68.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsb26ehdao53ey33ofn68.png" alt="result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;note that &lt;code&gt;me&lt;/code&gt; didn't change at all while the first element in &lt;code&gt;nums&lt;/code&gt; became &lt;code&gt;0&lt;/code&gt; as we wanted&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now for some reason I'll try to update the &lt;code&gt;length&lt;/code&gt; property of both &lt;code&gt;me&lt;/code&gt; and &lt;code&gt;nums&lt;/code&gt; and see if any changes happen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fppo8o6ksxgb0bpu4jge8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fppo8o6ksxgb0bpu4jge8.png" alt="example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;results :&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8sevufm2vwdobzfuego0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8sevufm2vwdobzfuego0.png" alt="results"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Again, the string &lt;code&gt;me&lt;/code&gt; doesn't correspond to any change while &lt;code&gt;nums&lt;/code&gt; works as expected&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  why is that ?
&lt;/h2&gt;

&lt;p&gt;That's because strings are immutable, meaning that we can only access the string's characters and properties like &lt;code&gt;length&lt;/code&gt; but we can't modify them at all.&lt;/p&gt;

&lt;p&gt;On the other hand arrays are mutable, we can access and modify array elements and properties.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In fact, all primitives(&lt;em&gt;strings, numbers , booleans , etc&lt;/em&gt;) are immutable while all objects are mutable.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You can modify object properties or array elements but you can't modify a string's character and you can't modify a number.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you want to make an object immutable you can use the built-in method &lt;code&gt;Object.freeze&lt;/code&gt; you can read about it here &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze" rel="noopener noreferrer"&gt;mdn reference&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  MUTATING VS REASSIGNMENT
&lt;/h2&gt;

&lt;p&gt;An interesting question is: &lt;br&gt;
"How can't we modify the string &lt;code&gt;me&lt;/code&gt; while we can say &lt;code&gt;me = "Awesome";&lt;/code&gt; and that'll update the string ? "&lt;/p&gt;

&lt;p&gt;well, the statement `me = "Awesome" is called &lt;em&gt;reassignment&lt;/em&gt; and that's not the same as &lt;em&gt;mutating&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;let me explain the difference, think of a variable as a &lt;strong&gt;box&lt;/strong&gt; containing a &lt;strong&gt;value&lt;/strong&gt;, &lt;em&gt;reassigning&lt;/em&gt; means throwing the &lt;strong&gt;Current value&lt;/strong&gt; away and replacing it with another one.&lt;/p&gt;

&lt;p&gt;while &lt;em&gt;mutating&lt;/em&gt; means changing something about the &lt;strong&gt;value&lt;/strong&gt; but without throwing it away.&lt;/p&gt;

&lt;p&gt;for example : &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff1dyoywuqhrpgbqupt01.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff1dyoywuqhrpgbqupt01.png" alt="example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  CONCLUSION
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;primitives are immutable &lt;/li&gt;
&lt;li&gt;objects are mutable&lt;/li&gt;
&lt;li&gt;mutability is not the same as reassignment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's it for this article, hope you find it helpful and stay tuned for the rest of this series&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>One of the most important rules I learned after a year of learning frontend</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Sun, 10 Jan 2021 03:25:43 +0000</pubDate>
      <link>https://dev.to/anwarr_________/one-of-the-most-important-rules-i-knew-after-a-year-of-learning-frontend-4660</link>
      <guid>https://dev.to/anwarr_________/one-of-the-most-important-rules-i-knew-after-a-year-of-learning-frontend-4660</guid>
      <description>&lt;p&gt;Most of the time when I was writing code, I was subconsciously working according to one concept "If it works, don't touch it" which was really time wasting and a lot of the time frustrating. Now I try to code according to another concept "Think before you code". In this article I'm gonna talk about this concept and why it's really important. &lt;/p&gt;

&lt;h1&gt;
  
  
  If it works, don't touch it
&lt;/h1&gt;

&lt;p&gt;When you write code as a beginner this is probably how you solve a problem: &lt;br&gt;
"write some stuff then run it , doesn't work ? write more stuff then run it again, maybe copy some snippets from stackoverflow and so on until it works."&lt;br&gt;
Even with css, you probably try different properties on the element and keep trying until you get the desirable result.&lt;br&gt;
I have been there but I discovered that this is not a right way of writing code because most of the time you're not really understanding what your code does, another problem is that you're probably have some lines of code that either redundant or repetitive. &lt;/p&gt;

&lt;h1&gt;
  
  
  Think before you code
&lt;/h1&gt;

&lt;p&gt;A better approach is by planning things out before writing any code, There two magical tools that'll help you make a good plan these tools are: a pen and a paper.&lt;br&gt;
Say you wanna build a website, spend some time away from your code editor and start ask yourself some questions : "What colors or fonts do I wanna use ?" , "how do I want it to look on desktops? what about mobile ?" , "Do I want to repeat some style ?".&lt;br&gt;
Think deeply about each question and write the answers, draw a basic shape of your layout, believe me this'll save you from repeating yourself and from wasting a lot of the time trying things out waiting for your code to work. Kevin powell has an amazing article about that topic(&lt;a href="https://www.kevinpowell.co/article/creating-a-website-getting-over-the-anxiety-of-starting-with-a-blank-file/"&gt;Creating a website - getting over the anxiety of starting with a blank file&lt;/a&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  Applying it to javascript
&lt;/h2&gt;

&lt;p&gt;Take you time understanding the problem and the relation between the input and the output, think about things you need in order to solve the problem, the data type you're dealing with and whether you need an auxiliary data type or not , write some pseudo code. &lt;/p&gt;

&lt;h1&gt;
  
  
  What if things didn't work after planning?
&lt;/h1&gt;

&lt;p&gt;At this point, start searching to understand why your code isn't working and by searching I mean looking for articles/videos that talk about the topic that's confusing you, take your time searching until you figure out what's wrong this searching process will help you develop your skills and understanding of the language you're learning, and please never copy snippets of code without understanding them.       &lt;/p&gt;

</description>
      <category>css</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>productivity</category>
    </item>
    <item>
      <title>A Simple introduction to recursion</title>
      <dc:creator>Anwar</dc:creator>
      <pubDate>Sun, 01 Nov 2020 13:41:39 +0000</pubDate>
      <link>https://dev.to/anwarr_________/a-simple-introduction-to-recursion-3a0i</link>
      <guid>https://dev.to/anwarr_________/a-simple-introduction-to-recursion-3a0i</guid>
      <description>&lt;p&gt;Recursion is one of the topics that confuses people when seeing it for the first time,  this article introduces the main idea of recursion in order to make things a little bit clearer and get you up and running with it.&lt;/p&gt;

&lt;h1&gt;
  
  
  What exactly is recursion ?
&lt;/h1&gt;

&lt;p&gt;It's easier to understand it through an example&lt;/p&gt;

&lt;p&gt;Say you were asked to calculate the sum of 13 and 18, most of us thinks about it like this: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;since 10 + 10 = 20&lt;/li&gt;
&lt;li&gt;and 3 + 8 = 11&lt;/li&gt;
&lt;li&gt;so the sum is 20 + 11 = 31&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;what exactly did we do here ? &lt;/p&gt;

&lt;p&gt;instead of calculating the sum right away, we &lt;em&gt;broke&lt;/em&gt; &lt;br&gt;
the problem into &lt;em&gt;two small problems&lt;/em&gt; of the same type, the difference is that we can easily solve the &lt;em&gt;two small problems&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;That's exactly what recursion is all about, it's about breaking a problem into several small problems that are easier to solve.&lt;/p&gt;
&lt;h2&gt;
  
  
  Enough talking, let's write some code
&lt;/h2&gt;

&lt;p&gt;Now that you (hopefully) understand the main idea behind recursion, let's see how can we implement it.&lt;/p&gt;

&lt;p&gt;Let's start by writing a function that counts from a given number &lt;code&gt;n&lt;/code&gt; up to &lt;code&gt;5&lt;/code&gt; in our console, this is done very easily using a loop like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function countTo5(n){ 
    for(let i =n; i&amp;lt;=5; i++){ 
        console.log(i)
    }
}

countTo5(1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but let's try doing it with recursion (&lt;strong&gt;Make sure you read the comment in the code below in order to understand how it works&lt;/strong&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function countTo5(n){ 
   if(n === 5) { 
       console.log(n)
       return;
   }
   console.log(n);

   return countTo5(n+1)
}

countTo5(1)
/* 
    first call
    n = 1 
    1===5 ? no 
    console.log(1)
    return countTo5(2)
    -----------------
    second call 
    n = 2
    2===5 ? no 
    console.log(2)
    return countTo5(3)
    -----------------
    third call 
    n = 3
    3===5 ? no 
    console.log(3)
    return countTo5(4)
    ------------------
    forth call 
    n = 4
    4===5 ? no 
    console.log(4)
    return countTo5(5)
    ------------------
    fifth call 
    n = 5
    5===5 ? yes
    console.log(5)
    the function stops
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;note: it's absolutely better to solve it with loops, recursion is used here for explanation purposes only&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Base Case
&lt;/h2&gt;

&lt;p&gt;A loop becomes infinite if we don't have a stopping condition. similar to loops, if recursion doesn't have something that makes it stop it will execute over and over until it causes a &lt;a href="https://www.freecodecamp.org/news/understanding-the-javascript-call-stack-861e41ae61d4/"&gt;stack overflow&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;in the example above, our base case was this if statement&lt;br&gt;
 &lt;code&gt;if(n === 5) { &lt;br&gt;
       console.log(n)&lt;br&gt;
       return;&lt;br&gt;
   }&lt;/code&gt;   &lt;/p&gt;

&lt;p&gt;In most cases the base case is going to be a conditional statement. &lt;/p&gt;

&lt;h3&gt;
  
  
  conclusion
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;recursion is a way of solving problems by breaking it into smaller problems&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;we can say that recursion is an elegant way of looping &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;we must have a base case in our recursion or else we'll get stack overflow&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hope this helps guys, this is my first article ever so I'd love to know your opinions about it, hopefully it becomes first of many useful article, here is some further reading and videos &lt;/p&gt;

&lt;p&gt;-&lt;a href="https://javascript.info/recursion#two-ways-of-thinking"&gt;https://javascript.info/recursion#two-ways-of-thinking&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-&lt;a href="https://www.youtube.com/watch?v=lMBVwYrmFZQ"&gt;https://www.youtube.com/watch?v=lMBVwYrmFZQ&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-&lt;a href="https://www.youtube.com/watch?v=k7-N8R0-KY4"&gt;https://www.youtube.com/watch?v=k7-N8R0-KY4&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-&lt;a href="https://www.freecodecamp.org/news/understanding-recursion-in-javascript/"&gt;https://www.freecodecamp.org/news/understanding-recursion-in-javascript/&lt;/a&gt;&lt;/p&gt;

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