<?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: Dheeraj Menon</title>
    <description>The latest articles on DEV Community by Dheeraj Menon (@dheerajmnk).</description>
    <link>https://dev.to/dheerajmnk</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%2F582089%2F5a10261f-af9a-44fa-9efe-df12910a3a81.png</url>
      <title>DEV Community: Dheeraj Menon</title>
      <link>https://dev.to/dheerajmnk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dheerajmnk"/>
    <language>en</language>
    <item>
      <title>Scope in JavaScript: Understanding Lexical Environment and Scope Chain</title>
      <dc:creator>Dheeraj Menon</dc:creator>
      <pubDate>Thu, 24 Mar 2022 16:02:57 +0000</pubDate>
      <link>https://dev.to/dheerajmnk/scope-in-javascript-understanding-lexical-environment-and-scope-chain-3mlh</link>
      <guid>https://dev.to/dheerajmnk/scope-in-javascript-understanding-lexical-environment-and-scope-chain-3mlh</guid>
      <description>&lt;p&gt;The concept of Scope in JavaScript is directly related to Lexical Environment. Understanding the Lexical Environment is thus very helpful to learn about Scope and have the prerequisite knowledge to dive into Closures.&lt;/p&gt;

&lt;p&gt;Scope is the region in which a particular variable can be accessed in the code. It determines the accessibility of a variable. So, how is the scope of a variable decided? This is where Lexical Environment comes into play.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lexical Environment
&lt;/h2&gt;

&lt;p&gt;In JavaScript, every code block has an internal associated object known as the Lexical Environment. It consists of 2 parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Environment Record - An object that stores all local variables as its properties.&lt;/li&gt;
&lt;li&gt;A reference to the outer lexical environment.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In short, the lexical environment of any code block is its local memory and the lexical environment of its parent.&lt;/p&gt;

&lt;p&gt;Let's understand this through an example. Consider the following code. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1d8qalioek76eeupjly6.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1d8qalioek76eeupjly6.JPG" alt="Code" width="656" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we assigned the value 10 to a variable inside the main function. When we try to access this value from a function inside the main function, we successfully obtain 10. But when we try to access it outside the main function, an "Uncaught ReferenceError" is thrown.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxg6kzrh3h66fd84bhmzq.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxg6kzrh3h66fd84bhmzq.JPG" alt="Code Result" width="702" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why does this happen? We need to understand the mechanism of the lexical environment to know why.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lexical Environment Mechanism
&lt;/h3&gt;

&lt;p&gt;When we run this program, a Global Execution Context is created and it is pushed onto the Call Stack. The main function is now invoked and an execution context is created which is also pushed onto the call stack. Since the inner function is invoked inside the main function, another execution context is created and pushed onto the call stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F522radn047mnvmhnqudq.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F522radn047mnvmhnqudq.JPG" alt="Call Stack" width="515" height="553"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Whenever an execution context is created, a lexical environment is also created. For the above 3 execution contexts, there are also 3 lexical environments. &lt;/p&gt;

&lt;p&gt;Because of lexical environment, alongside the environment record, we are able to get a reference to the parent lexical environment. Since inner function is lexically inside main function, it will have access to the lexical environment of the main function. Similarly, the main function will have access to the lexical environment of the global execution context. The lexical environment of the global execution context has a reference that points to null.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa1segethew0k53c7hq2h.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa1segethew0k53c7hq2h.JPG" alt="Call Stack 2" width="461" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let us see how the lexical environment is used in this program. When JavaScript engine encounters the console.log(x) inside the inner function, it tries to find out the value of x inside the local memory of inner function. &lt;/p&gt;

&lt;p&gt;Since the variable does not exist inside the inner function, it uses the reference and goes into the lexical environment of its parent which is the main function. It searches for the value of x and finds it. Hence, we are able to see 10 printed on the console as our first result. &lt;/p&gt;

&lt;p&gt;As for our console.log(x) outside the main function, the JavaScript engine tries to find the value of x in the global execution context. Since it is not found, the engine then uses the reference to parent lexical environment which points to null. Hence, we get an error in this case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lexical Environment in the browser
&lt;/h3&gt;

&lt;p&gt;It's time to observe the Call Stack and Scope in the browser for the above example. Note that Lexical Environment is a specification object and only exists theoretically in JavaScript to describe how things work. But the Scope will do a fine job in helping us understand the lexical environment for each execution context in the call stack because it will show us the local memory for each lexical environment and the references to all outer lexical environments.&lt;/p&gt;

&lt;p&gt;Note: (anonymous) refers to the global execution context.&lt;/p&gt;

&lt;h5&gt;
  
  
  Global Execution Context
&lt;/h5&gt;

&lt;p&gt;The Global Execution Context is added to the bottom of the call stack when we run a program. Its scope is global i.e any variable present inside it can be accessed anywhere in the program. It has the window object as well. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbhl3jkg8c4v6y7hznqa2.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbhl3jkg8c4v6y7hznqa2.JPG" alt="GEC" width="702" height="129"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Execution Context of main function
&lt;/h5&gt;

&lt;p&gt;The Lexical Environment of our main function is the local memory of main function along with the lexical environment of global execution context. In the local memory, we have the definition of inner function and the value of x.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqtewmfgaf65kfn7ag8j5.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqtewmfgaf65kfn7ag8j5.JPG" alt="Main" width="703" height="127"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Execution Context of inner function
&lt;/h5&gt;

&lt;p&gt;The Lexical Environment of our inner function is the local memory of inner function along with the lexical environment of main function and its parent lexical environment as well. The lexical environment of our main function is called a "Closure". A closure remembers its outer variables and can access them. In our example, we can see that the inner function is enclosed inside the main function. Because of this, the inner function is able to access the variable in the main function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw663j23rlyzdqk7jagym.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw663j23rlyzdqk7jagym.JPG" alt="Inner" width="702" height="129"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Scope Chain
&lt;/h3&gt;

&lt;p&gt;The mechanism of finding variables shown above in which our code started searching for the variable in its lexical environment and continued its search using the reference to outer lexical environment is known as the Scope Chain. &lt;/p&gt;

&lt;p&gt;If the variable is not found, the JavaScript Engine will look into the outer scope until the variable is found or the global scope is reached. If the variable is not even found in the global scope, we use its reference to the null value and a reference error is thrown.&lt;/p&gt;

&lt;h4&gt;
  
  
  References
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=uH-tVP8MUs8" rel="noopener noreferrer"&gt;Namaste JavaScript&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://javascript.info/closure" rel="noopener noreferrer"&gt;javascript.info&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>The CSS Cascade Algorithm Explained</title>
      <dc:creator>Dheeraj Menon</dc:creator>
      <pubDate>Thu, 10 Feb 2022 15:49:49 +0000</pubDate>
      <link>https://dev.to/dheerajmnk/the-css-cascade-algorithm-explained-323m</link>
      <guid>https://dev.to/dheerajmnk/the-css-cascade-algorithm-explained-323m</guid>
      <description>&lt;p&gt;The "C" in CSS stands for Cascading. It is what gives the style sheets its cascading nature so understanding the cascade is an important part of learning CSS. &lt;/p&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;It is entirely possible for 2 or more competing CSS rules to be applied on the same HTML element. How does the browser decide which rule to choose in such case? &lt;/p&gt;

&lt;p&gt;Consider the following 2 CSS rules that were applied on the same button.&lt;/p&gt;

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

&lt;p&gt;Without the cascade algorithm, the browser would have a tough time determining the "winner" of this conflict.&lt;/p&gt;

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

&lt;p&gt;So, what is the cascade? &lt;/p&gt;

&lt;p&gt;The cascade is the algorithm used by the browser to solve conflicts where multiple CSS rules are applied to the same HTML element. It takes all the values given to a certain property, sorts them by their precedence and outputs the value with the highest precedence.&lt;/p&gt;

&lt;p&gt;Note: Only CSS declarations i.e property-value pairs participate in this cascade.&lt;/p&gt;

&lt;p&gt;Now, let's look at the different stages of this cascade algorithm.&lt;/p&gt;

&lt;h3&gt;
  
  
  Order of Appearance
&lt;/h3&gt;

&lt;p&gt;The order in which CSS rules appear in the style sheet is taken into consideration. &lt;/p&gt;

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

&lt;p&gt;In this example, the color orange was declared last. Because the crimson color was declared before the orange color, it is ignored by the browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxa4ub3v464i5dlv5cazj.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxa4ub3v464i5dlv5cazj.JPG" alt="Order" width="483" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Specificity
&lt;/h3&gt;

&lt;p&gt;Specificity determines which selector is the most specific using a certain weighing mechanism. By making a rule more specific, it would win out over other rules that appear later in the style sheet.&lt;/p&gt;

&lt;p&gt;Let us go back to our previous example and help crimson beat orange.&lt;/p&gt;

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

&lt;p&gt;All classes have higher specificity than all elements. So in this example, the color is crimson because it has a class of heading. It does not matter that orange appeared later in the style sheet. Specificity is checked before order.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz1uueaquqht29vdul7pc.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz1uueaquqht29vdul7pc.JPG" alt="Specificity" width="453" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: An ID is even more specific than a class. In fact, ID selectors have the highest specificity among all selectors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Importance
&lt;/h3&gt;

&lt;p&gt;All CSS rules written by a developer have normal importance by default. We can give our rules higher importance by simply using the "!important" property in our rule.&lt;/p&gt;

&lt;p&gt;Let's look at this in action. &lt;/p&gt;

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

&lt;p&gt;Consider an ID selector used at the absolute bottom of the style sheet. That's the best possibility in both the specificity and order aspect.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl4vtawlgpmq5efz0xhe0.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl4vtawlgpmq5efz0xhe0.JPG" alt="Importance" width="537" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But it still loses out to any random CSS rule that has the !important property. This means that importance plays an even bigger role than specificity and order in the cascade battle. So, the only way to beat an important rule is with an important rule that has higher specificity or better order.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx0s9bxrdi3iirc3f6bd3.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx0s9bxrdi3iirc3f6bd3.jpeg" alt="Importance meme" width="800" height="563"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: There are 4 types of importance rules possible. Animation rule has higher importance than a normal rule. Important rule has higher importance than an animation rule. Finally, Transition rule has higher importance than an important rule. This is because this rule is expected to always change the default visual state of the elements it is applied upon.&lt;/p&gt;

&lt;h3&gt;
  
  
  Origin
&lt;/h3&gt;

&lt;p&gt;Now, let's understand the final factor in the cascade algorithm. The CSS written by a developer is not the only CSS that is applied to a web page. &lt;/p&gt;

&lt;p&gt;When we define HTML elements, they have some CSS rules by default. Let's look at what default CSS rule a heading tag has.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4e88pcapnofqnx1bsoer.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4e88pcapnofqnx1bsoer.JPG" alt="Origin Agent CSS" width="800" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The H1 element has a total of 7 property-value pairs! This is not surprising. After all, this is how the browser differentiates headings from normal text. All these default styles are present in the browser's "user agent stylesheet" as the picture displays. &lt;/p&gt;

&lt;p&gt;The author, i.e the developer has the power to make changes in these predefined styles in their own stylesheet which is called the "author stylesheet". Let's change the default font-size of the H1 tag.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm43hndwcekak3po3blau.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm43hndwcekak3po3blau.JPG" alt="Origin Author CSS" width="800" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we used our stylesheet to override the default font-size of 2em that a H1 tag has by default. &lt;/p&gt;

&lt;p&gt;However, if a user agent stylesheet has an !important property in any of its rule, then not even an author stylesheet with the !important property can override it. &lt;/p&gt;

&lt;p&gt;There's also something called as a "user stylesheet" involved in this hierarchy but since most modern browsers do not directly support this feature anymore, this article will not cover it. Here's an image that'll help you understand the whole origin hierarchy. (Credits to web.dev)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5vjbrru7ifvnuzt10n49.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5vjbrru7ifvnuzt10n49.JPG" alt="Origin" width="703" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;The Chrome Devtools is a nice way to debug issues caused due to the cascade. For any chosen element, it'll display all the rules that were applied to it and strike off those rules which were not accepted by the browser. If you still have issues fixing them, you can always rely on the !important property to come to your rescue.&lt;/p&gt;

&lt;p&gt;That's about it! Thank you for reading. &lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to implement Search feature using JavaScript</title>
      <dc:creator>Dheeraj Menon</dc:creator>
      <pubDate>Mon, 17 Jan 2022 07:37:02 +0000</pubDate>
      <link>https://dev.to/dheerajmnk/how-to-implement-search-feature-using-javascript-mhi</link>
      <guid>https://dev.to/dheerajmnk/how-to-implement-search-feature-using-javascript-mhi</guid>
      <description>&lt;p&gt;A search bar is a very common feature used in a wide variety of applications. In this article, I will be walking you through a simple approach to implement the search feature using array methods. Note that there are many ways to do this, and this is just one of them. I will be using the classic to-do list project as my reference, but you can also use this approach in other areas such as searching for a certain post in a blog! &lt;/p&gt;

&lt;h3&gt;
  
  
  Defining HTML Structure
&lt;/h3&gt;

&lt;p&gt;Our HTML file consists of a single container div. It has 3 sections. The first is our search bar which has a form tag that takes text as input.&lt;/p&gt;

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

&lt;p&gt;The next section is an unordered list which contains the list of all todos and the option for deleting them.&lt;/p&gt;

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

&lt;p&gt;The last section consists of a form tag that lets the user add new todos.&lt;/p&gt;

&lt;p&gt;PS: You can view the complete code at &lt;a href="https://github.com/dheerajmnk/todo_list" rel="noopener noreferrer"&gt;todo-list&lt;/a&gt;. I'm trying to keep only the relevant parts in this article.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Approach
&lt;/h3&gt;

&lt;p&gt;Whenever a user types in a character in the search form, we will perform a filtering function. For doing this, we will use an event listener that listens to the "keyup" event in the input field of the form.&lt;br&gt;
If we do not get a match to our existing todos, we will hide those todos by applying a class. This class will have a CSS rule that hides its display.&lt;/p&gt;

&lt;h4&gt;
  
  
  Listen to keyup event
&lt;/h4&gt;

&lt;p&gt;We define a constant "search" to get a reference to the input field of the search form. Now we attach the keyup event to this reference. Every time the user types a letter, we will fire a callback function to get the value of whatever the user has typed until that moment. This value will be stored in a constant "term".&lt;/p&gt;

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

&lt;p&gt;We use the trim() method to get rid of any white spaces before or after the todo. Now, we perform the filterTodos() function on the value we obtain i.e "term". Everytime a new key is entered, this function is called.&lt;/p&gt;

&lt;h4&gt;
  
  
  Filter Todos function
&lt;/h4&gt;

&lt;p&gt;Firstly, we define a constant "list" to get a reference to all the todos. &lt;/p&gt;

&lt;p&gt;We use an arrow function that takes the "term" as a parameter. The next step is to filter through the todos and apply classes to the ones that don't match the input term so that we can hide them.&lt;br&gt;
To perform this function, we need the help of Array Methods so we will convert our list into an array.&lt;/p&gt;

&lt;p&gt;Now, we can use the array filter method. It will go through all the items taking "todo" as a parameter and fire a callback function for each of them. If the text content of our todos DO NOT(!) contain the term, they will be filtered out. This method will return an array of all the filtered items.&lt;/p&gt;

&lt;p&gt;Now, we can cycle through this array using for-each method. We take todo as a parameter and add a "filtered" class for all the items in the array. I have used method chaining here to make the code look a bit cleaner.&lt;/p&gt;

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

&lt;p&gt;There's one thing we have to take care of in this function. What if the user enters a letter that adds the "filtered" class to a todo and then immediately removes that letter? We would need a way to then remove the newly attached "filtered" class. We can achieve this by doing exactly the opposite of above code. If the text content of our todos contain the term, they will be filtered. Then we can cycle through them and remove the "filtered" class.&lt;/p&gt;

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

&lt;h4&gt;
  
  
  CSS Time
&lt;/h4&gt;

&lt;p&gt;Now that we have applied the "filtered" class to all the items that do not match, we would want the user to not be able to see them. For this, we will simply use the CSS display property of none.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Summing up
&lt;/h3&gt;

&lt;p&gt;There's one final problem with this that has to be fixed. What if the user inputs an upper case value? "BUY XYZ" will not match to "buy xyz". To fix this, we can convert every value to lower case by using toLowerCase() method and perform our filter todos function only after that.&lt;/p&gt;

&lt;p&gt;Since my JavaScript code snippets have been all over the place, here is what everything covered in this article finally looks like after performing the case conversion.&lt;/p&gt;

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

&lt;p&gt;Feel free to correct me if I have made any mistakes in this article. Thank you for reading.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Beginner's Guide to Computational Thinking</title>
      <dc:creator>Dheeraj Menon</dc:creator>
      <pubDate>Thu, 06 May 2021 13:30:10 +0000</pubDate>
      <link>https://dev.to/dheerajmnk/beginner-s-guide-to-computational-thinking-3dka</link>
      <guid>https://dev.to/dheerajmnk/beginner-s-guide-to-computational-thinking-3dka</guid>
      <description>&lt;p&gt;There are a lot of resources available for learning computer programming, but very few resources for learning the thinking approach required to program. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjnmfa1w3raqzvipihoyv.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjnmfa1w3raqzvipihoyv.JPG" alt="Alt Text" width="440" height="125"&gt;&lt;/a&gt;If the above picture represents programming, then Computational Thinking is the step that comes before programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Computational Thinking?
&lt;/h2&gt;

&lt;p&gt;Computational Thinking is the process of approaching a problem systematically and expressing a solution that can be carried out even by a computer.&lt;/p&gt;

&lt;p&gt;It is a blueprint for a computer program. The blueprint can then be implemented using a programming language. &lt;/p&gt;

&lt;p&gt;So, how does Computational Thinking work? It involves 4 elements. Each of these elements arises from a Question.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Problem identification
&lt;/h2&gt;

&lt;p&gt;Question: Have we identified a problem that is solvable by a computer?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftaafwrxdtuj7qe7mu4q4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftaafwrxdtuj7qe7mu4q4.jpg" alt="Alt Text" width="800" height="800"&gt;&lt;/a&gt;&lt;br&gt;
Some problems cannot be solved with a computer. We must determine whether a problem is a good candidate for using a computer. It's difficult for computers to solve subjective problems, and hence we come up with problems that consist of data that are usable by a computer. The things to think about here are:&lt;br&gt;
i Topic: What is the big problem you are trying to solve? Can it be stated in a problem statement?&lt;br&gt;
ii Data: What data do you have to contribute to a problem-solving approach? What other data might you need?&lt;br&gt;
iii Feasibility: Can you solve the problem with what you know? What other data might you need?&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Decomposition
&lt;/h2&gt;

&lt;p&gt;Question: Can we break our problems down into smaller sub-problems?&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbc1nkfv0vk5oouc6g437.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbc1nkfv0vk5oouc6g437.JPG" alt="Alt Text" width="490" height="282"&gt;&lt;/a&gt;&lt;br&gt;
Once we have a problem statement, the next step would be to break down the problem into smaller sub-problems. These sub-problems then become easier to solve and help in contributing to solve the large problem. Further, these sub-problems could be broken down into even smaller problems. It follows the same notion as elements chemically being composed of single atoms. &lt;/p&gt;

&lt;h2&gt;
  
  
  3. Pattern recognition
&lt;/h2&gt;

&lt;p&gt;Question: Can we see familiar patterns that can lead us to potential solutions?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlptxqlyknysfacensiw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlptxqlyknysfacensiw.png" alt="Alt Text" width="608" height="600"&gt;&lt;/a&gt;&lt;br&gt;
Finding solutions to similar problems or finding similar problems that have been solved before can help in solving the decomposed problems more efficiently. Consider how the current problem is similar to other problems you have solved in the past. How can these similarities help in coming up with a solution?&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Abstraction
&lt;/h2&gt;

&lt;p&gt;Question: Can we identify non-essentials aspects of our problem that we can ignore?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flx04463scf9fwn64vt3j.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flx04463scf9fwn64vt3j.jpg" alt="Alt Text" width="248" height="203"&gt;&lt;/a&gt;&lt;br&gt;
Usually, problems have several non-essentials aspects that can be ignored because they only distract us from solving the main problem. This is when we must filter out parts of the problem so that we can concentrate on the necessary aspects that we want to solve. It's when we create a hierarchy of elements and ignore the least relevant ones. This helps with arriving at an effective solution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Computational Thinking is an iterative non-linear process. These are not steps to follow for solving a problem but rather things to think about when solving a problem. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fun Fact&lt;/strong&gt;: Have you heard of the 4 C's of the 21st century: Critical Thinking, Creativity, Collaboration and Communication? Many people argue that Computational Thinking should be the 5th C. &lt;/p&gt;

</description>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
