<?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: Mateusz Janusz</title>
    <description>The latest articles on DEV Community by Mateusz Janusz (@mateusz_janusz).</description>
    <link>https://dev.to/mateusz_janusz</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%2F130449%2F4c6490cd-bfc9-40c0-8bc2-b3131dcf94d5.png</url>
      <title>DEV Community: Mateusz Janusz</title>
      <link>https://dev.to/mateusz_janusz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mateusz_janusz"/>
    <language>en</language>
    <item>
      <title>Hoisting in JavaScript</title>
      <dc:creator>Mateusz Janusz</dc:creator>
      <pubDate>Sat, 16 Jul 2022 11:49:17 +0000</pubDate>
      <link>https://dev.to/mateusz_janusz/hoisting-in-javascript-1dk6</link>
      <guid>https://dev.to/mateusz_janusz/hoisting-in-javascript-1dk6</guid>
      <description>&lt;p&gt;Have you ever wondered why you were able to call functions before you wrote them in your code? This is possible thanks to hoisting.&lt;/p&gt;

&lt;p&gt;Hoisting is a process of moving the declaration of variables and functions to the top by the &lt;a href="https://dev.to/mateusz_janusz/the-javascript-engine-4o5f"&gt;interpreter&lt;/a&gt;. This happens during compilation, before code execution.&lt;/p&gt;

&lt;p&gt;So no matter where variables and functions are declared, they are moved to top of their scope. It allows functions to be used in code before they are declared in the code.&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;printName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 'My name is John'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;printName&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="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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;My name is &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  variables are partially hoisted
&lt;/h2&gt;

&lt;p&gt;It's important to remember that functions are fully hoisted but variables are only partially hoisted.&lt;br&gt;
This is because JavaScript only hoists declarations, not initializations.&lt;/p&gt;

&lt;p&gt;In other words, JS engine is allocating variables in memory but without their value. Their values get assigned (initialization) during code execution. Before execution, the variable has its default initialization: it's &lt;code&gt;undefined&lt;/code&gt; for variables declared using &lt;code&gt;var&lt;/code&gt;, otherwise uninitialized.&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="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;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns 'undefined' (JavaScript has hoisted the variable declaration only)&lt;/span&gt;

&lt;span class="kd"&gt;var&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="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// initialization and declaration &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;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// after the line is executed, it returns 'John'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because of this, we can use variables before we declare them. However, we have to be careful because the hoisted variable can be initialized with &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; hoisting
&lt;/h3&gt;

&lt;p&gt;With &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt;, just as with &lt;code&gt;var&lt;/code&gt;, the variable is hoisted to the top of the block, but it is not initialized with a default value. So an exception (Reference error) will be thrown if a variable declared with let or const is used before it is initialized.&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="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;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: ReferenceError: name is not defined&lt;/span&gt;
&lt;span class="kd"&gt;const&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="s1"&gt;John&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;h3&gt;
  
  
  using 'strict mode'
&lt;/h3&gt;

&lt;p&gt;We can also use strict mode that was introduced in the es5 version of JavaScript. In a restricted mode, the compiler won't tolerate the usage of variables before they are declared.&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use strict&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;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: ReferenceError: name is not defined&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="s1"&gt;John&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;h2&gt;
  
  
  Function expressions vs declarations
&lt;/h2&gt;

&lt;p&gt;Function declarations are hoisted completely to the top, but we should not forget that function expressions (similar to variables) are not fully hoisted. Function expressions are hoisted to the top of the block, but they are not initialized until code is executed.&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;declaration&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// This function has been hoisted&lt;/span&gt;

&lt;span class="c1"&gt;// Function declaration&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;declaration&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am hoisted&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="nf"&gt;expression&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: "TypeError: expression is not a function&lt;/span&gt;

&lt;span class="c1"&gt;// Function expression&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;expression&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am declared during execution&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>frontend</category>
    </item>
    <item>
      <title>The Javascript Engine</title>
      <dc:creator>Mateusz Janusz</dc:creator>
      <pubDate>Mon, 11 Jul 2022 07:53:19 +0000</pubDate>
      <link>https://dev.to/mateusz_janusz/the-javascript-engine-4o5f</link>
      <guid>https://dev.to/mateusz_janusz/the-javascript-engine-4o5f</guid>
      <description>&lt;p&gt;A computer cannot understand Javascript, so we need another computer program that can execute JS code and translate it into a language that is understandable by machines. This computer program is called Javascript Engine.&lt;/p&gt;

&lt;p&gt;The very first JavaScript engine was created by &lt;a href="https://en.wikipedia.org/wiki/Brendan_Eich" rel="noopener noreferrer"&gt;Brendan Eich&lt;/a&gt; in 1995 for the Netscape web browser.&lt;/p&gt;

&lt;p&gt;ECMAScript is the standardized specification of JavaScript, therefore a Javascript engine is also called ECMAScript engine. There is many of them and most of them are developed by web browser vendors.&lt;/p&gt;

&lt;p&gt;Nowadays, the most popular (the most used) engine is called V8. It was created by Google for its Chrome browser. It is written in C++ and it was developed with a big focus on performance. It was crucial for Google to develop an engine that can handle heavy applications such as Google Maps. They achieved it thanks to just-in-time compilation which significantly improved execution times. V8 can run standalone, or can be embedded into any C++ application. Apart from Google Chrome, Chromium project, Electron.js, also server-side JavaScript runtime Node.js is using the V8 engine.&lt;/p&gt;

&lt;p&gt;There are other popular JavaScript engines used by different browsers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Safari is using Apple's own engine &lt;a href="https://developer.apple.com/documentation/javascriptcore" rel="noopener noreferrer"&gt;JavaScriptCore&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Firefox has &lt;a href="https://spidermonkey.dev" rel="noopener noreferrer"&gt;SpiderMonkey&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Edge was initially using &lt;a href="https://github.com/chakra-core/ChakraCore" rel="noopener noreferrer"&gt;Chakra&lt;/a&gt; but has been rebuilt using Chromium and the V8 engine.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/List_of_ECMAScript_engines" rel="noopener noreferrer"&gt;See the list of all ECMAScript engines&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  How engine works
&lt;/h2&gt;

&lt;p&gt;Let's focus on how Javascript engine works step by step.&lt;/p&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fw5gvueldi44c80fc4wcm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fw5gvueldi44c80fc4wcm.png" alt="JS engine flow" width="571" height="451"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;ol&gt;
&lt;li&gt;Firstly, raw JS code goes into a &lt;strong&gt;Parser&lt;/strong&gt;. Parser does a lexical analysis, it breaks code into a series of tokens (sequence of characters), so later they can get converted into Abstract Syntax Tree.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AST&lt;/strong&gt; is a hierarchical tree like structure of program representation. It breaks things down for system to understand what code does.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Interpreter&lt;/strong&gt; takes AST and splits out Bytecode. This process in V8 engine is known as Ignition.&lt;/li&gt;
&lt;li&gt;In the meantime, &lt;strong&gt;Profiler&lt;/strong&gt; watches for optimizing the code. When it finds a code that is repeated, it will move this code into a compiler.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compiler&lt;/strong&gt; modifies the code to optimize it, so it runs faster. It updates Bytecode with optimized parts of code. In V8 Engine, this compiler is called TurboFan&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Interpreter vs Compiler
&lt;/h2&gt;

&lt;p&gt;Interpreters and compilers translates one language into another language which is understandable by a machine.&lt;/p&gt;

&lt;p&gt;Interpreters are quick to pick up and running. An interpreter allows us to run code right away, it translates and reads a file, line by line on the fly. The problem with it is that it eventually gets slow, that's why we need compilers.&lt;/p&gt;

&lt;p&gt;A compiler doesn't translate code on the fly. Instead, it tries to understand what code does, it takes our code and it changes it to something else to optimize it. So for instance, it won't run the loop all times when it sees a code which has the same input or output, it will translate it into single run code.&lt;/p&gt;

&lt;p&gt;What if we combine an interpreter and a compiler into one? We get &lt;strong&gt;JIT (just-in-time) Compiler&lt;/strong&gt;!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>frontend</category>
    </item>
    <item>
      <title>How to do human-friendly code reviews</title>
      <dc:creator>Mateusz Janusz</dc:creator>
      <pubDate>Sun, 20 Dec 2020 16:56:46 +0000</pubDate>
      <link>https://dev.to/mateusz_janusz/how-to-do-code-reviews-and-keep-a-positive-relationship-with-your-colleagues-2161</link>
      <guid>https://dev.to/mateusz_janusz/how-to-do-code-reviews-and-keep-a-positive-relationship-with-your-colleagues-2161</guid>
      <description>&lt;p&gt;Doing code reviews is not an easy task. Doing a good code review is even more difficult and responsible job. It is not only a technical process but also a social one. In this article, I discuss a few techniques that may improve your code reviews and make them more human friendly to keep positive relationships with your colleagues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explain
&lt;/h2&gt;

&lt;p&gt;You don't always need to do it, but sometimes it's appropriate to give a bit more explanation about your suggestions. It's a good practice to explain both your suggested change and the reason for the change. This helps the developer understand why you are making your comment and there is a chance that the author of the pull request may learn something new. &lt;/p&gt;

&lt;p&gt;If you can, provide a link to a section in your company's style guide, to language or library documentation, or to the best practice you’re following. Make sure that the developer really understands how your suggestion improves code health.&lt;/p&gt;

&lt;p&gt;Code can sometimes be just messy or written in an unintuitive way. Instead of formulating judgments like "This is bad" or "This line of code is unnecessary", try to explain why this code should be refactored. You could write your comment in a nicer way, for instance, "I found this code hard to understand, what about splitting this complex function?". Referring to the issue in this way helps the developer understand why you are making your comment.&lt;/p&gt;

&lt;p&gt;By the way, try to encourage developers to simplify code or add code comments instead of just explaining the complexity to you, so those changes can be helpful also to future code readers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Give guidance
&lt;/h2&gt;

&lt;p&gt;It's not easy to find a good balance between pointing out problems and providing direct guidance. Code review is an opportunity for developers to learn and protect themselves from mistakes. But giving a ready solution to a problem is just taking away that opportunity. &lt;/p&gt;

&lt;p&gt;Instead, let developers explore, research, and make a decision. This helps them to learn. On the other hand, if you leave comments like this one "Can we simplify this complex function?", you're not actually helping much. So instead of only pointing out problems, you should also share some suggestions, instructions, or maybe even a code example. &lt;/p&gt;

&lt;p&gt;Remember that a hidden goal of code reviews is also to improve developers' skills so they require less and less guidance over time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do not judge
&lt;/h2&gt;

&lt;p&gt;Code review comments should be written in a neutral language. You should never critique the author, instead focus on the code and possible improvements. &lt;/p&gt;

&lt;p&gt;If you find something difficult to understand, ask for clarification rather than assuming ignorance or making judgments in comments, like "this can never work". If you want to critique the code of a colleague, make sure to do so in a respectful way and always include some possible ways for improvement. &lt;/p&gt;

&lt;p&gt;To make sure you're not critiquing the author, avoid using the word "you" in your message. When the author sees "you" in a comment, they may take the criticism personally. Consider this sentence:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"You made this code hard to understand." &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A comment like this brings the focus away from the code, and the author can interpret it as a personal attack.&lt;/p&gt;

&lt;p&gt;Writing comments without the word "you" shouldn't be hard, you have at least two options. &lt;/p&gt;

&lt;h3&gt;
  
  
  Replace "you" with "we"
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;"This function &lt;strong&gt;you&lt;/strong&gt; wrote is hard to read. Can &lt;strong&gt;you&lt;/strong&gt; simplify it?"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"I find this function difficult to understand. Can &lt;strong&gt;we&lt;/strong&gt; simplify it?"&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Which comment would you prefer to receive as an author?&lt;/p&gt;

&lt;p&gt;I bet that you would prefer the second one. That's because the second example brings focus to the code and the comment is not judging anybody.&lt;/p&gt;

&lt;h3&gt;
  
  
  Replace commands with requests
&lt;/h3&gt;

&lt;p&gt;Another option is to omit the subject from the sentence. Look at these two examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;"You should split this function into smaller ones."&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"What about splitting this function so the code is easier to read and debug in the future?"&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The second example is not only formulated in a friendly way but it also brings reasoning to your comment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Point out good work
&lt;/h2&gt;

&lt;p&gt;Don't limit your code review to only finding errors. Pointing out also good work definitely improves the team spirit and is a good motivator. Don't forget to do it especially if you have learned something from this code or if you noticed a solution you wouldn’t have come up with yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Everyone makes mistakes
&lt;/h2&gt;

&lt;p&gt;Remember that we're all humans and it doesn't matter if it's a beginner or experienced developer's code, the truth is that everyone makes mistakes. It may be caused by a bad day, bad mood, lack of domain knowledge, or overload with duties.&lt;/p&gt;

&lt;p&gt;I hope that my tips will encourage you to strive for a stress-free and developer-friendly code review. Let's remember that good code reviews not only improve the merged code but they bring benefits to all team members. We can also inspire others and contribute to their self-development.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;
This article was originally published on my personal site &lt;a href="https://mateuszjanusz.dev" rel="noopener noreferrer"&gt;mateuszjanusz.dev&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>tips</category>
      <category>motivation</category>
      <category>career</category>
      <category>learned</category>
    </item>
    <item>
      <title>JS Concepts: Value vs. Reference</title>
      <dc:creator>Mateusz Janusz</dc:creator>
      <pubDate>Sat, 19 Dec 2020 10:29:42 +0000</pubDate>
      <link>https://dev.to/mateusz_janusz/js-concepts-2-value-vs-reference-145k</link>
      <guid>https://dev.to/mateusz_janusz/js-concepts-2-value-vs-reference-145k</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article is a part of a series covering fundamentals of the JavaScript language. The intention of this series is to help developers advance their knowledge and really understand how the JavaScript language works. And to help myself get better at writing 😉&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;In the previous article, we talked about &lt;a href=""&gt;call stack&lt;/a&gt;. Today, we're going to discuss the difference between value and reference in JavaScript. At the end of this article, you will be able to tell why primitives and objects behave differently and how to avoid mistakes while manipulating them. 💪&lt;/p&gt;

&lt;h1&gt;
  
  
  Types in JavaScript
&lt;/h1&gt;

&lt;p&gt;In JavaScript we have two categories of types.&lt;/p&gt;

&lt;h3&gt;
  
  
  Value types (primitives)
&lt;/h3&gt;

&lt;p&gt;Types that are passed by value:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;String&lt;/li&gt;
&lt;li&gt;Number&lt;/li&gt;
&lt;li&gt;BigInt&lt;/li&gt;
&lt;li&gt;Boolean&lt;/li&gt;
&lt;li&gt;Symbol&lt;/li&gt;
&lt;li&gt;undefined &lt;/li&gt;
&lt;li&gt;null&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Reference types
&lt;/h3&gt;

&lt;p&gt;Types that are passed by reference:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Object&lt;/li&gt;
&lt;li&gt;Array&lt;/li&gt;
&lt;li&gt;Function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's have a closer look at both of them.&lt;/p&gt;

&lt;h1&gt;
  
  
  Value vs. Reference
&lt;/h1&gt;

&lt;p&gt;When you assign a variable, the JavaScript engine decides whether the value is a primitive or a reference value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Primitive
&lt;/h2&gt;

&lt;p&gt;When we assign a value as primitive, &lt;strong&gt;the value is actually stored in the variable&lt;/strong&gt;. This means that when you manipulate the  variable, you are working on the actual value stored in the variable. If you assign primitive variables to other variables using &lt;code&gt;=&lt;/code&gt;, their values are copied to new variables. We say that &lt;strong&gt;they are copied by value&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;p&gt;When we assign non-primitive value to the variable, &lt;strong&gt;we copy them by reference&lt;/strong&gt;. In other words, variables are given a reference to that value, so &lt;strong&gt;they don’t actually contain the value&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;When you assign a non-primitive variable to other variable using &lt;code&gt;=&lt;/code&gt;, its reference is copied to the new variable and so they now both point to the same object’s location in memory. Consequently, if you decide to manipulate only one of them, you are actually working on the reference, which means you manipulate both variables!&lt;/p&gt;

&lt;p&gt;This is crucial to understand as it’s often the reason behind bugs.&lt;/p&gt;

&lt;h1&gt;
  
  
  Examples
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Primitive values
&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&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;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 5&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;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;

&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 5&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;b&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;p&gt;As you can see in the example above, the value stored in the variable &lt;code&gt;b&lt;/code&gt; has been changed, but the value of variable &lt;code&gt;a&lt;/code&gt; remains intact. This is because variables &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; have no relationship. By copying value from variable &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;b&lt;/code&gt;, we created a new independent value.&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;let&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;increaseIndex&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;increaseIndex&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;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;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The index value stays 0 even after executing &lt;code&gt;increaseIndex&lt;/code&gt; function. This is because primitive values are copied by value. We're dealing with two independent values here, so changing the copied value has no effects on the original value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Non-primitive values
&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;person_one&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Adam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&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;person_two&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;person_one&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;person_one&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="c1"&gt;// 'Adam'&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;person_two&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="c1"&gt;// 'Adam'&lt;/span&gt;

&lt;span class="nx"&gt;person_two&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="s1"&gt;George&lt;/span&gt;&lt;span class="dl"&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;person_one&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="c1"&gt;// 'George'&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;person_two&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="c1"&gt;// 'George'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By changing the name of &lt;code&gt;person_two&lt;/code&gt;, we're also modifying value in &lt;code&gt;person_one&lt;/code&gt; because both &lt;code&gt;person_one&lt;/code&gt; and &lt;code&gt;person_two&lt;/code&gt; are pointing to the same object. When the variable &lt;code&gt;person_two&lt;/code&gt; is created and assigned to &lt;code&gt;person_one&lt;/code&gt;, we're creating an alias to the original object, not a new object.&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;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Adam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&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;changeName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;person&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="s1"&gt;George&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;changeName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&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;person&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="c1"&gt;// 'George'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the &lt;code&gt;person&lt;/code&gt; name was changed after executing &lt;code&gt;changeName&lt;/code&gt; function. This is because when we pass an object into the function, we're passing a reference to that object. When we change a property of that object within the function, the change will be reflected in the outer scope.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Primitives are copied by their value&lt;/li&gt;
&lt;li&gt;Objects are copied by their reference&lt;/li&gt;
&lt;li&gt;When you manipulate variable that stores a primitive value, you are working on the value stored in the variable&lt;/li&gt;
&lt;li&gt;When you manipulate an object, you are working on the reference to that object, not on the actual object&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;
This article was originally published on my personal blog &lt;a href="https://mateuszjanusz.dev" rel="noopener noreferrer"&gt;mateuszjanusz.dev&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>JS Concepts: Call Stack</title>
      <dc:creator>Mateusz Janusz</dc:creator>
      <pubDate>Sat, 02 Mar 2019 11:38:33 +0000</pubDate>
      <link>https://dev.to/mateusz_janusz/js-concepts-call-stack-12n7</link>
      <guid>https://dev.to/mateusz_janusz/js-concepts-call-stack-12n7</guid>
      <description>&lt;p&gt;This article is a part of a series covering fundamentals of the JavaScript language. The intention of this series is to help developers advance their knowledge and really understand how the JavaScript language works. And to help myself get better at writing 😉&lt;/p&gt;




&lt;p&gt;Today, we're going to find out what is a call stack and how does it work in JavaScript. Let's begin! 👏&lt;/p&gt;

&lt;h1&gt;
  
  
  What is a call stack
&lt;/h1&gt;

&lt;p&gt;We can say that the call stack is a kind of a to-do list of function invocations. It is a structure that stores a list of functions to be called.&lt;/p&gt;

&lt;p&gt;The reason why we call it a call stack is because it stores data in a stack. Stack is an abstract data type where elements are inserted and removed according to the &lt;strong&gt;L&lt;/strong&gt;ast-&lt;strong&gt;I&lt;/strong&gt;n &lt;strong&gt;F&lt;/strong&gt;irst-&lt;strong&gt;O&lt;/strong&gt;ut (LIFO) principle. &lt;/p&gt;

&lt;p&gt;It's like a stack of pancakes; you always add to the top of the stack and remove from the top of the stack&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F32cza2sxrhsgvojnqmww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F32cza2sxrhsgvojnqmww.png" alt="how the stack works" width="773" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because JavaScript is mainly single threaded, it can only execute one line of code at any given time. Basically, it can only do one thing at a time.&lt;/p&gt;

&lt;p&gt;If in a JavaScript application, you call a function, that function may resolve in any other function calls. Those functions may be waiting on results of other calls before the main function finishes. So the application needs to keep track of things that were invoked and things that are waiting to be returned. It needs to keep a history of functions that were invoked but are not done yet.&lt;/p&gt;

&lt;h1&gt;
  
  
  How does it work
&lt;/h1&gt;

&lt;p&gt;Whenever you call a function, the details of the call are saved on top of the stack. Whenever a function returns, the information is taken off the top of the stack. &lt;/p&gt;

&lt;p&gt;The code execution is synchronous. Whenever JavaScript engine finds a function invocation, it creates a new execution context for that function and pushes it to the top of the stack. The call stack knows the next function to be executed and will remove it after execution. It reaches to the context below it in the current stack until the memory is clear.&lt;/p&gt;

&lt;p&gt;Let's look at the 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;function&lt;/span&gt; &lt;span class="nf"&gt;foo&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;foo&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;World&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="nf"&gt;bar&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 what happens if you run this code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;function bar() is called; empty stack frame (execution context) is created;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Call Stack:&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;bar()&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;function bar() calls function foo() which is added to the top of the stack&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Call Stack:&lt;/em&gt; &lt;br&gt;
&lt;strong&gt;foo()&lt;/strong&gt;&lt;br&gt;
bar()&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;function foo() returns and prints 'Hello' to the console&lt;/li&gt;
&lt;li&gt;function foo() is removed from top of the stack&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Call Stack:&lt;/em&gt;&lt;br&gt;
&lt;del&gt;foo()&lt;/del&gt;&lt;br&gt;
&lt;strong&gt;bar()&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;now, function bar() is executed and it returns, 'World' is printed to the console&lt;/li&gt;
&lt;li&gt;function bar() is popped off the stack&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Call Stack:&lt;/em&gt;&lt;br&gt;
&lt;del&gt;bar()&lt;/del&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;we ended up with an empty stack, the memory is cleared&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Call Stack:&lt;/em&gt;&lt;br&gt;
EMPTY&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can also observe this if your browser throws an error stack trace in the console. It indicates where in the function it failed. Executed functions are presented from top to bottom, like in the stack. See the example below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fz1ygo865wak911soeqlb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fz1ygo865wak911soeqlb.png" alt="Error stack trace" width="800" height="69"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Stack overflow
&lt;/h1&gt;

&lt;p&gt;We've all been there... This happens when we run an infinite loop. &lt;/p&gt;

&lt;p&gt;A function invocated recursively (a function calls itself) can hit a limit on the size of the stack in a browser. That's what we call stack overflow. If this happens, a browser will stop all calls and throw 'Maximum call stack size exceeded' error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fmaw6lx87ha4airv9aynj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fmaw6lx87ha4airv9aynj.png" alt="RangeError: maximum call stack exceeded" width="800" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;Call stack is an ordered set of stack frames where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;most recently executed function is at the top of the stack&lt;/li&gt;
&lt;li&gt;the first invoked function is at the bottom of the stack&lt;/li&gt;
&lt;li&gt;the stack is processed from top to bottom&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other takeaways are: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript is single threaded, it can only do one thing at a time&lt;/li&gt;
&lt;li&gt;Stack is a data structure where you always add to the top of the stack and remove from the top of the stack (LIFO)&lt;/li&gt;
&lt;li&gt;Function execution is synchronous&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;
This article was originally published on my &lt;a href="https://mateuszjanusz.dev" rel="noopener noreferrer"&gt;personal blog&lt;/a&gt;.&lt;/p&gt;

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