<?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: Shihabudheen US</title>
    <description>The latest articles on DEV Community by Shihabudheen US (@codermonkey).</description>
    <link>https://dev.to/codermonkey</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%2F385216%2F162a952f-0d28-42f8-a860-cb5dbaa15141.png</url>
      <title>DEV Community: Shihabudheen US</title>
      <link>https://dev.to/codermonkey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codermonkey"/>
    <language>en</language>
    <item>
      <title>OWASP top 10</title>
      <dc:creator>Shihabudheen US</dc:creator>
      <pubDate>Sat, 05 Dec 2020 07:28:19 +0000</pubDate>
      <link>https://dev.to/codermonkey/owasp-top-10-2aep</link>
      <guid>https://dev.to/codermonkey/owasp-top-10-2aep</guid>
      <description>&lt;p&gt;&lt;a href="https://owasp.org/" rel="noopener noreferrer"&gt;OWASP&lt;/a&gt; stands for &lt;strong&gt;Open Web Application Security Project&lt;/strong&gt;, which is an open-source foundation for web app security. They have curated a list of top 10 vulnerabilities and the list is a good place to start with for making your apps more secure.&lt;/p&gt;

&lt;p&gt;Here we will discuss the 2017 release candidate 2 of OWASP top 10 vulnerabilities.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Injection
In programming, the commands and the data to be operated upon are defined by code. &lt;/li&gt;
&lt;/ol&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%2Fnu3gw2g73ub6sui4vfjt.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%2Fnu3gw2g73ub6sui4vfjt.png" alt="code" width="678" height="850"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For example, consider a signup form. Here we get the input(data) from the user and act up it using the commands we used to code the application. What if the input intended to be data acted as a command for the application. With that, the user can take the application to unindented states. This is an example of an injection attack. There are SQL injection, LDP injection, OS injection, XPath injection etc.&lt;br&gt;
   Access control is one way to prevent this type of attack.&lt;/p&gt;

&lt;p&gt;Prevention:&lt;br&gt;
     - input validation: can include blacklisting and whitelisting of characters and their data types and lengths.&lt;br&gt;
     - prepared statements and stored procedures: the user must only be able to pass the parameters rather than a complete dynamic query. The developer can also have stored procedures(named queries) so that all required queries are kind of well defined.&lt;br&gt;
     - least privilege: ensure you only provide the required level of access. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Broken Authentication and session management
Authentication is verifying who you are and what belongs to you. A session starts when an authenticated user starts using his account/resources. The most common was of authentication is a username and password.
At times people might trick the user with some emails or phone calls which sound urgent in order to acquire their passwords and user names. This is called &lt;strong&gt;Social engineering/Pishing&lt;/strong&gt;.
With the increased computation power availability hackers can run periodic and systematic attacks to get access to an account and this is called &lt;strong&gt;Automated Attacks&lt;/strong&gt;. One way is &lt;em&gt;credential stuffing&lt;/em&gt; where the hacker periodically tries out the user credentials exposed in a security breach. As most of the people reuse the passwords this works so often. Another one is &lt;em&gt;brute-foroce attack&lt;/em&gt; where the attacker will guess to the password and try different possible combinations of credentials.
&lt;strong&gt;Broken application logic&lt;/strong&gt; can also lead to broken auth. Suppose if you have a less efficient forgot password reset mechanism hackers can get hold of your accounts easily.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;limit the number of retries&lt;/li&gt;
&lt;li&gt;stronger passwords&lt;/li&gt;
&lt;li&gt;encrypt the passwords well&lt;/li&gt;
&lt;li&gt;two-factor authentication where the user uses a password as well as an OTP to login.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Sensitive Data exposure&lt;br&gt;
At times the sensitive data might be leaked or exposed. It is always advised to encrypt data while storing and sending. Encryption(🔐) involves an algorithm(mathematical formula) and a key(🔑). If one has to decrypt the data they must know the algorithm and 🔑 used to encrypt it.&lt;br&gt;
Always try to store the least possible sensitive information which is only required.&lt;br&gt;
Prevention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;enforce PCI standards&lt;/li&gt;
&lt;li&gt;enforce laws and regulations&lt;/li&gt;
&lt;li&gt;reduce the scope. Always process, collect, store and transmit only the least required data.&lt;/li&gt;
&lt;li&gt;encrypt sensitive data at rest and transit. TLS(successor of SSL), HTTPS(HTTP + TLS) and HSTS(HTTP Strict Transport Security). HSTS convert all HTTP connections to HTTPS.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;XML External Entities(XXE)&lt;br&gt;
XXE is more like an injection attack that happens in a system where XML is used for inter-application communication. &lt;br&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%2Fs7haxlgd0qg13zbmy7sf.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%2Fs7haxlgd0qg13zbmy7sf.png" alt="XML parser" width="636" height="820"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;XML is used to transfer data and files.&lt;br&gt;
  A malicious XML input from an external source is sent to an application with an intent to take down the entire application or Remote Code Execution(RCE).&lt;br&gt;
  Example of one such attack is &lt;strong&gt;XML Bomb&lt;/strong&gt;, where an XML string get reproduced into 10x after each parsing finally results in DoS(Denial of Service)/Billions Laugh Attack attack. With this, the application is no more able to handle further requests due to memory overflow. &lt;/p&gt;

&lt;p&gt;Prevention:&lt;br&gt;
    - disable XML external entities&lt;br&gt;
    - whitelist, blacklist and validating inputs&lt;br&gt;
    - upgrade the XML processor and libs&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Broken access control&lt;br&gt;
Authorization is more related to enforcing user roles and allowing authenticated users to perform certain actions that their user role facilitates. &lt;br&gt;
Every application will have a certain kind of user roles, who are privileged to perform different levels of actions. Access control is broken if user roles are not being strictly enforced.&lt;br&gt;&lt;br&gt;
Prevention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;enforce proper access control&lt;/li&gt;
&lt;li&gt;logging and monitoring access control failures&lt;/li&gt;
&lt;li&gt;manual testing of access control by security researchers(penetration testing), QA or developers in unit or integration tests&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Security misconfiguration&lt;br&gt;
An application may consist of different infrastructure and software components. At times the misconfiguration of these components can end up making our applications vulnerable to attacks. Few examples are,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Error handling&lt;/em&gt;: during development, it is common to enable error handling. But once the application is being deployed in production we should ensure this error handling and logging being turned off so that the hackers can't know what exactly is failing and use it against us.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Default passwords and settings&lt;/em&gt;: many of the infra and software components are shipped with default passwords. We. should them to be removed and strong passwords are being used. Suppose if the hacker new which service we are using and is able to get access using the default password, then it will be troublesome.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Outdated libraries and frameworks&lt;/em&gt;
The best approach is to provide &lt;strong&gt;least privilege&lt;/strong&gt; to the services and they should have only the required privileges to access DB and other services. And in general no GOD users.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Prevention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;harden all systems&lt;/li&gt;
&lt;li&gt;update and patch regularly&lt;/li&gt;
&lt;li&gt;test configurations&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Cross-site scripting(XSS)
Using &lt;code&gt;&amp;lt;script/&amp;gt;&lt;/code&gt; to inject malicious scripts through an HTML input. They are mainly of the following types:

&lt;ul&gt;
&lt;li&gt;reflected cross-site scripting, which is more like pishing when the user clicks on a link that in turn executes a malicious code&lt;/li&gt;
&lt;li&gt;stored XSS, happens when a user visits a page with a malicious payload. User doesn't have to do anything, he/she just need to visit the page. This kind of attack is more dangerous as it can spread too quickly and can end up being a &lt;em&gt;worm&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;XSS is used to&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;steal credentials&lt;/li&gt;
&lt;li&gt;install keyloggers&lt;/li&gt;
&lt;li&gt;defame websites&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prevention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;enable Content Security Policy(CSP&lt;/li&gt;
&lt;li&gt;applying Context-Sensitive Encoding. It ensures software doesn't interpret data as instructions as we tell it not to.&lt;/li&gt;
&lt;li&gt;use HTML entity encoding and URL encoding, which tells the software the inputs as data rather than code.&lt;/li&gt;
&lt;li&gt;escape unstructured HTTP data&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Insecure deserialization
Serialization is the process of converting digital information into a different format so that it can be stored well. Deserialisation is the reverse process of converting the store information into digital format.
There are chances for hackers to tamper the serialized information and cause chaos after deserialisation and this is referred to as insecure deserialisation. Most of the time developers use libraries for serialising and deserialising data. But most of these libs are not foolproof and may miss many guards.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Prevention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use encryption and have in place integrity checks&lt;/li&gt;
&lt;li&gt;logging and error reporting to detect insecure deserialisation&lt;/li&gt;
&lt;li&gt;isolate code that deserialises&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Using components with unknown vulnerabilities 
The software has bugs. In modern-day development, we make use of a lot of open-source libraries, frameworks and systems. But if those components have any vulnerabilities we are also getting vulnerable by using it. 
There is a list maintained by (CVE)[&lt;a href="https://cve.mitre.org/index.html" rel="noopener noreferrer"&gt;https://cve.mitre.org/index.html&lt;/a&gt;]. If an attacker knows a bug in a component it can be his/her doors to our system too.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Prevention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;continuously monitor the components and dependencies for security flaws&lt;/li&gt;
&lt;li&gt;if the patches are time taking, apply &lt;em&gt;virtual patch&lt;/em&gt; using &lt;strong&gt;Web Application Firewall(WAF)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Insufficient logging and monitoring
Logging is noting down what is having in a system continuously. Logging is kind of an electronic record of things happening. This can include login, logouts, error, input validation failures, etc. We not always should just log it but monitor them closely too, to understand what is actually happening. To make it effective there must be an incident reporting system in place too. 
The time to detect a breach is nearly 197 days. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Preventions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ensure you log what is required and no sensitive information. But it can have sufficient user context so that we can know who is doing what.&lt;/li&gt;
&lt;li&gt;the logs must be well monitored so that we can spring to act quickly.&lt;/li&gt;
&lt;li&gt;have an Incident Response Plan, which says how to respond to an incident. This lay down the steps to be carried out and who will do what.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>security</category>
      <category>webdev</category>
      <category>owasp</category>
      <category>productivity</category>
    </item>
    <item>
      <title>🎣 [Hookable] ⚛️[React]</title>
      <dc:creator>Shihabudheen US</dc:creator>
      <pubDate>Sun, 20 Sep 2020 15:27:09 +0000</pubDate>
      <link>https://dev.to/codermonkey/hookable-react-4p73</link>
      <guid>https://dev.to/codermonkey/hookable-react-4p73</guid>
      <description>&lt;p&gt;Spoiler 🚫: this one is not worth reading. Upfront you are warned!⚠️&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;react&lt;/code&gt; is a library to define the blueprint of UI. It doesn't know how to render, it is just to define the UI.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;react-dom&lt;/code&gt; is the one that takes care of the rendering part. We can pass the actual DOM element into who the react defined UI should be shoved into.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;ReactDom.render(domElement,reactElement)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;for markup react uses something called JSX, which is not pure HTML. Under the hood, it is babel which transpile this to JavaScript
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div isBold&amp;gt;Shihab&amp;lt;/div&amp;gt;

...
React.createElement('&amp;lt;div&amp;gt;',{ isBold:true },'Shihab')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;React.createElement()&lt;/code&gt;, first and second params are fixed as type and props respectively. The rest of the arguments are treated as children, and it can be of any count.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;React.createElement('&amp;lt;div&amp;gt;',{ isBold:true },'Shihab',' ','learning',' ','React')

...
&amp;lt;div&amp;gt;Shihab is learning React&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;React is UI in terms of state. What we tell React is for a given state, how the UI should look like. This is what makes React declaratively. If you start to think of React in terms of state, it is much easier and cleaner to build things. To relate, think of like a UI designer. For the given condition(state) what should be the UI like. If you initially start thinking about the interactions and all the stuff, you are actually making things difficult.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  useState
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/usestate-lb94t?file=/src/index.js" rel="noopener noreferrer"&gt;CodeSandbox&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useState&lt;/code&gt; take an initialValue and return [currentValue,setValue]&lt;/li&gt;
&lt;li&gt;if value changes it should re-render
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function useState(initialValue){
  function setValue(newValue){ 
     initialValue = newValue
     render()
  }

  return [initialValue,setValue]
}

function render(){
  ReactDOM.render(....)
}

render()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But here, there is a problem. It is the component which calls the useState. So each time the component(function) gets called, it is calling useState afresh. In that case, if we try to set a new value to the initial value, it wouldn’t hold. Between the renders, it will lose value. So we should have some mechanism to persist the values, across renders. For that, we can use a global variable, let us call it &lt;code&gt;states&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;states&lt;/code&gt; will have the [currentValue,setValue] getting pushed for all renders.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const states=[]

function useState(initialValue){
  function setValue(newValue){ 
     states[0][0] = newValue
     render()
  }

  const tuples = [initialValue,setValue]
  states.push(tuples)
  return tuples
}

function render(){
  ReactDOM.render(....)
}

render()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Still, there are problems. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is evident that we keep on pushing into the &lt;code&gt;states&lt;/code&gt; array and soon it will run out of space.&lt;/li&gt;
&lt;li&gt;if we have multiple useStates inside the component, how we identify its current value from states array. So there should be some sort of identifier.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We only need to push to the states array on the initial render. On subsequent renders, we just have to find the respective value, update it and return its &lt;em&gt;tuple&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Since the useStates are being called once per &lt;code&gt;useState&lt;/code&gt; statements inside the component, we can have, a call counter for the &lt;code&gt;useState&lt;/code&gt;. On each call we increment it and on each render we reset it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const states=[]

let callCount=-1

function useState(initialValue){
  const id = ++callCount

  if(states[id]){
    return states[id]
  }

  function setValue(newValue){ 
     states[id][0] = newValue
     render()
  }

  const tuples = [initialValue,setValue]
  states.push(tuples)
  return tuples
}

function render(){
  callCount=-1
  ReactDOM.render(....)
}

render()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To note, &lt;em&gt;this is not the actual implementation of useState in React&lt;/em&gt;. This is just a thought. From the above snippet, we can conclude that,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useState should be called in the same order and the same no.of times between the renders. In other words, useStates can’t be called inside conditions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  useRef
&lt;/h3&gt;

&lt;p&gt;In the non-react world, we have different ways to reference or identify the DOM element. We usually use &lt;code&gt;querySelectors&lt;/code&gt;,&lt;code&gt;document.getElementById&lt;/code&gt;,etc. But in react, it is not encouraged much, because we can have multiple instances of the same component and if we use an &lt;code&gt;id&lt;/code&gt; to identify a DOM element, it would cause problems.&lt;/p&gt;

&lt;p&gt;So in React world, we have &lt;code&gt;refs&lt;/code&gt;. &lt;code&gt;refs&lt;/code&gt; are usually used to "ref"erence DOM element in React.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// create ref
const inputRef=useRef()

// assign ref
&amp;lt;input ref={inputRef}/&amp;gt;

// use ref
inputRef.current.focus()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note&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;handler(e){
 e.preventDefault()
}

&amp;lt;button onClick={(e)=&amp;gt;this.handler(e)}&amp;gt;Click Me&amp;lt;/button&amp;gt;

// same as
&amp;lt;button onClick={this.handler}&amp;gt;Click Me&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Implicit arguments of callbacks are passed by default. We don't need an intermediate param for them to be captured.&lt;/p&gt;

&lt;h3&gt;
  
  
  useEffect
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;we can think of them as side effects to be run
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(()=&amp;gt;{
  // effect
  return ()=&amp;gt; // clean up
},[...deps])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;effect&lt;/em&gt; run when any of the &lt;em&gt;deps&lt;/em&gt; changes and &lt;em&gt;clean up&lt;/em&gt; follows&lt;/li&gt;
&lt;li&gt;if no &lt;em&gt;deps&lt;/em&gt; is provided with the effect is triggered on each render and &lt;em&gt;clean up&lt;/em&gt; follows&lt;/li&gt;
&lt;li&gt;if &lt;em&gt;deps&lt;/em&gt; is an empty array, it runs only once in the entire life cycle of the component that too when the component mounts and &lt;em&gt;clean up&lt;/em&gt; is triggered on the unmounting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Note&lt;/em&gt;: suppose you have a fetch inside an useEffect and you want to avoid the unwanted state updates if the useEffect is triggered while the current fetch is in progress. Use the below snippet for the clean up the intermittent calls, just before the last&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(()=&amp;gt;{
 let isCurrent = true
 fetchUser(id).then(user=&amp;gt;{
   if(isCurrent) setUser(user)
 })
return ()=&amp;gt; isCurrent=false
},[id])

function clickHandler(id){
 setId(id)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;over here, no matter how many times the &lt;code&gt;clickHandler&lt;/code&gt; is triggered, only the last one will trigger a state update&lt;/p&gt;

&lt;h3&gt;
  
  
  useContext
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MyContext=React.createContext()

function App(){
  const [productId,setProductId]=useState(1)

  return (
    &amp;lt;MyContext.Provider value={{ productId,nsetProductId }}&amp;gt;
       &amp;lt;Header/&amp;gt;
       &amp;lt;Body&amp;gt;
         &amp;lt;Product/&amp;gt;
       &amp;lt;/Body&amp;gt;
       &amp;lt;Footer/&amp;gt;
    &amp;lt;/MyContext.Provider&amp;gt;
}

// deeply nested child component
function Product(){
  const { productId,setProductId } = useContext(MyContext)
  return (
    &amp;lt;Container&amp;gt;
      ...
    &amp;lt;/Container&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  useReducer
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;it helps to keep states together&lt;/li&gt;
&lt;li&gt;the changes can be a bit more self-descriptive &lt;/li&gt;
&lt;li&gt;no need to call different setState (from useState) to update different states
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fetchAds(){
 try{
   setLoading(true)
   ...
 }catch(err){
   setLoading(false)
   setError(err)
 }
}

...
function fetchAds(){
 try{
   dispatch({type:'FETCH_ADS'}) // more self-descriptive
   ...
 }catch(err){
   dispatch({type:'FETCH_ADS_FAILED'}) // no multiple setState calls
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;in fact, useState is implemented on top of useRedcer
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const initialState = {
  counter: 0
};

function reducer(state, action) {
  switch (action.type) {
    case "INCREMENT":
      return { ...state, counter: state.counter + 1 };
    ...
    default:
      throw Error(`Unhandled action type: ${action.type}`);
  }
}

function App(){
  const [state, dispatch] = useReducer(reducer, initialState);

  const { counter } = state;

  function increment() {
    dispatch({ type: "INCREMENT" });
  }

  ...

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;h1&amp;gt;Count: {counter}&amp;lt;/h1&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button onClick={increment}&amp;gt;Increment&amp;lt;/button&amp;gt;
        ...
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://codesandbox.io/s/usereducer-lcyd1?file=/src/App.js:486-1037" rel="noopener noreferrer"&gt;Codesand box&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note&lt;/em&gt;: in Redux reducers, we usually return the state as the default case inside the reducer switch. That is because we have the reducers separated as different files. When an action type is being dispatched all of the reducers get invoked but its handler resides inside the subset of global reducers.  But for &lt;code&gt;useReducer&lt;/code&gt;s we need not do that. These are most of the time local to the components and are only invoked from it. We will have a clear idea about the action types. And practically we should be notified if any unhandled action types are &lt;code&gt;dispatch&lt;/code&gt;ed. So it is ok if we throw an error in the default case of these reducers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;by using useReducer with the context we can implement our own Redux. &lt;a href="https://gist.github.com/shihabus/2a07acbdde0dced0da6f61abcc29047b" rel="noopener noreferrer"&gt;Sample gist&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>shihadified</category>
    </item>
    <item>
      <title>Modules in JavaScript</title>
      <dc:creator>Shihabudheen US</dc:creator>
      <pubDate>Sun, 06 Sep 2020 07:34:26 +0000</pubDate>
      <link>https://dev.to/codermonkey/modules-in-javascript-316h</link>
      <guid>https://dev.to/codermonkey/modules-in-javascript-316h</guid>
      <description>&lt;h3&gt;
  
  
  Why modules?
&lt;/h3&gt;

&lt;p&gt;In olden days, the scripts were added inside the markup. But as the projects started growing, maintainability became an issue. So people started to maintain scripts as separate files and it got imported via &lt;code&gt;&amp;lt;script src='...'/&amp;gt;&lt;/code&gt; tags for usage. &lt;/p&gt;

&lt;p&gt;Moreover, to share the variables and functions, all of them were pushed to the window(global scope). Often this leads to unexpected overrides and malicious usage(global namespace pollution).&lt;/p&gt;

&lt;p&gt;The problems with his approach were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the order of script loading was important. If one functionality has a dependency on another one, it should ensure the dependency is already loaded before executing. If not it might fail to execute and start throwing errors.&lt;/li&gt;
&lt;li&gt;sharing variables(states) and functions(behaviour) were difficult. The things to be shared were pushed to the global scope(window), which led to unexpected overrides(namespace collisions), malicious usage and global namespace pollution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Modules were introduced to solve these concerns.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a module?
&lt;/h3&gt;

&lt;p&gt;A file with functionality, which can be reused is called a &lt;em&gt;module&lt;/em&gt;. The modules helped to solve the global variable conflicts (scope issues). The variables and functions in a module are always file scoped. With modules, sharing was more explicit. Things to shared have to be &lt;code&gt;export&lt;/code&gt;ed and things to be consumed has to be &lt;code&gt;import&lt;/code&gt;ed.&lt;/p&gt;

&lt;p&gt;By default, module scripts &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use strict mode&lt;/li&gt;
&lt;li&gt;they are deferred while loading&lt;/li&gt;
&lt;li&gt;executed only once(in the first import) irrespective of the number of times it is being imported in the whole code (singleton).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With modules, the scripts can be split into multiple files, with less headache. The explicit link between the files was well evident, with import and export statements, and it became easier to combine and recombine them as chunks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Types of modules
&lt;/h3&gt;

&lt;h4&gt;
  
  
  ESModules
&lt;/h4&gt;

&lt;p&gt;ESModules is the language level module system in JS. This is currently under adoption. &lt;/p&gt;

&lt;p&gt;To use ESM in the browser we have to define the type as &lt;code&gt;module&lt;/code&gt; for the script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src='index.js' type='module'/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Apart from ESModules we have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AMD(Asynchronous Module Definition): legacy ones&lt;/li&gt;
&lt;li&gt;CommonJS: module system created for Node.js. If you want to use ESModules inside Node, then the module file must have an extension of &lt;code&gt;.mjs&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;UMD (Universal Module Definition): kind of universal modules, which supports both AMD and CommonJS styles.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Working of Modules
&lt;/h3&gt;

&lt;p&gt;When working with modules, we actually try to create a graph of dependencies and a module instance after parsing the files(creating module records). To do this, we just pass an entry file for the graph construction to start. This is done as,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src='main.js' type='module'/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three different steps in module loading are,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Construction: find, download, and parse all of the files into module records.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Instantiation: find boxes in memory to place all of the exported values in (but don’t fill them in with values yet). Then make both exports and imports point to those boxes in memory. This process is called &lt;em&gt;linking&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Evaluation: run the code to fill in the boxes with the variables’ actual values.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For ESMs, these steps are asynchronous and can be performed separately. &lt;/p&gt;

&lt;p&gt;But in the case of CommonJS, all of them are performed synchronous(w/o any breaks). Though each step need not be async. &lt;/p&gt;

&lt;p&gt;It is up to the loader how to load files. And the loader varies between the platforms(browser and Node).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/"&gt;More read&lt;/a&gt; &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>webpack</category>
      <category>webperf</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Next.js: the new normal</title>
      <dc:creator>Shihabudheen US</dc:creator>
      <pubDate>Mon, 31 Aug 2020 12:58:26 +0000</pubDate>
      <link>https://dev.to/codermonkey/next-js-the-new-normal-3imn</link>
      <guid>https://dev.to/codermonkey/next-js-the-new-normal-3imn</guid>
      <description>&lt;p&gt;Next.js is a full-stack framework based on React.js.&lt;/p&gt;

&lt;h3&gt;
  
  
  What it offers:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Prerendering: the entire HTML is created in the server and send to the client. So the client receives the HTML rather than the JS file. Once the HTML(string) is available it gets rehydrated at the client side. One can think of &lt;em&gt;rehydration&lt;/em&gt; as adding event listeners and making it interactive. All the routes are pre-rendered by default.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Scripts
&lt;/h5&gt;

&lt;p&gt;The common commands used to run and build a next project are the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts":{
  "dev": "next",   // start in dev mode
  "build": "next build", // start production build
  "start": "next start" // run production build
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Routing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;using &lt;code&gt;reach-router&lt;/code&gt; under the hood&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;file-system-based routing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;for that, we create a special folder called &lt;code&gt;pages&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;all the folder/file names become the routes for those files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;we can handle dynamic routes and receive the parameters like &lt;code&gt;notes/:id&lt;/code&gt;. For that we just need to create a file named &lt;code&gt;[id].js(x)&lt;/code&gt; inside a &lt;em&gt;notes&lt;/em&gt; folder. If the &lt;em&gt;notes&lt;/em&gt; folder has an &lt;em&gt;index&lt;/em&gt; file it will be treated as the &lt;code&gt;notes/&lt;/code&gt; route&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;to use the param inside the render function we can use &lt;code&gt;useRouter&lt;/code&gt; hook from &lt;code&gt;next/router&lt;/code&gt;. For classes, you have &lt;code&gt;withRouter&lt;/code&gt; HOC.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;notes/[id].js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useRouter } from 'next/router'

function App(){
 const router = useRouter()
 const {id} = router.query

 return (
    ...
 )
}

export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;note&lt;/em&gt;: In React, functional components are actually the render function. The entire function is the render method in case of functional components. With classes, we will have an explicit &lt;code&gt;render()&lt;/code&gt; with a return value. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if you want to handle slugs, like &lt;code&gt;notes/shihab/1&lt;/code&gt;, you can have a file named &lt;code&gt;[...slug].js&lt;/code&gt; inside the &lt;em&gt;notes&lt;/em&gt; directory. This time the router query will return an array-like &lt;code&gt;['shihab',1]&lt;/code&gt;. Even with catch-all routes, the index will still be used.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Navigation
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Link component
&lt;/h4&gt;

&lt;p&gt;For navigation &lt;code&gt;next/link&lt;/code&gt; expose a &lt;code&gt;Link&lt;/code&gt; element. It is always for &lt;em&gt;client-side&lt;/em&gt; routing. That means, on navigation, this will not trigger a network call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Link from 'next/link'

function App(){
  ...
  return {
    ....
    &amp;lt;Link href='/notes/[id]' as={`/notes/${id}`}&amp;gt;
      &amp;lt;a&amp;gt;Note&amp;lt;/a&amp;gt;
    &amp;lt;/Link&amp;gt;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;as&lt;/code&gt; path will be the exact path URL, the &lt;code&gt;href&lt;/code&gt; will be the file's relative location. The &lt;code&gt;href&lt;/code&gt; prop takes a page name as it is in the pages directory. For dynamic routes, you will need the &lt;code&gt;as&lt;/code&gt; prop as well.&lt;/p&gt;

&lt;p&gt;You must have an &lt;em&gt;a&lt;/em&gt; tag as the child of the Link component, but the &lt;em&gt;href&lt;/em&gt; lives on the Link.&lt;/p&gt;

&lt;p&gt;For server-side routing, you can readily use an anchor tag like &lt;code&gt;&amp;lt;a href='/docs'&amp;gt;Server side routing&amp;lt;/a&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Programmatic routing
&lt;/h4&gt;

&lt;p&gt;In order to navigate from code, one can use &lt;code&gt;router.push()&lt;/code&gt; from &lt;code&gt;next/router&lt;/code&gt;'s &lt;code&gt;useRouter&lt;/code&gt; hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useRouter } from 'next/router'

function naviagteOnSuccess(){
 const router = useRouter()

 ....
 router.push('/notes/[id]',`/notes/${note.id}`)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Styling
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;if you are using global CSS, &lt;code&gt;pages/_app.js&lt;/code&gt; is the only place you can import it. If you try to import it in other places Next.js will throw an error. This is more tied to the bundling of styles and loading them&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next.js readily supports &lt;em&gt;CSS Modules&lt;/em&gt;. With CSS modules we get file scoped styles. How it works is, with each import of CSS module file, a file specific class name gets added(prepended) to the classes you use. So the style you use is specific to that particular file and doesn't collide with others. The CSS modules will only work with non-pure selectors like classes and ids, etc and not with element selectors(div, span, p,...). The filename should be like &lt;code&gt;file-name.module.(s)css&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Special files
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;_app.js&lt;/code&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;if you want to hijack the entry file of Next, &lt;code&gt;_app.js&lt;/code&gt; file is the place. If you want to inject global styles, props or anything, it should happen here. This &lt;code&gt;_app.js&lt;/code&gt; is automatically created for you out of the box if you don't.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Next.js config
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;next-config.js&lt;/code&gt; in the root of the project&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  TS support
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Just create a &lt;code&gt;.tsconfig.json&lt;/code&gt; in the root.&lt;/li&gt;
&lt;li&gt;Next will ask you to add some libs and dependencies. Add them.&lt;/li&gt;
&lt;li&gt;Bhoom, now Next will auto-populate the &lt;em&gt;tsconfig&lt;/em&gt; for you. No more traction in setting up TS.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  API routes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Next is a full-stack framework. You can have your API route handlers inside a directory &lt;code&gt;pages/api&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The routing is the same as that for pages.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Data fetching
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;by default fetch is available&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Data can be fetched on the server and the client. Client-side data fetch is the same, what we do in a normal React app. The components may be rendered in the Server, but data fetching will only happen on the client in this case. That means, if you fetch the data in the client(using hooks or lifecycle methods), they aren't triggered on Server. The Server will render the view with the components initial State, that's all. &lt;em&gt;No, waiting until the client fetches or manipulation is over.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To fetch data on the server we have&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;getStaticProps&lt;/li&gt;
&lt;li&gt;getStaticPaths&lt;/li&gt;
&lt;li&gt;getServerSideProps&lt;/li&gt;
&lt;li&gt;&lt;p&gt;getInitialProps&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All of the above methods are only meant to run on the server(except getInitialProps, during subsequent calls).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;they are not even added to the client bundle&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;these methods can access DB, file system and all the things that can be done on the server-side&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the return value(objects) of these methods are injected into or send to the client-side components as JSON files&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  getStaticProps
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;to pass down any static props to the components, that are available during the build time&lt;/li&gt;
&lt;li&gt;it may receive the props from the &lt;em&gt;getStaticPaths&lt;/em&gt; method&lt;/li&gt;
&lt;li&gt;the return value is always an object&lt;/li&gt;
&lt;li&gt;this object is available as the props inside the component&lt;/li&gt;
&lt;li&gt;when building dynamic pages you will have the &lt;code&gt;params&lt;/code&gt; passed from &lt;em&gt;getStaticPaths&lt;/em&gt;, inside the &lt;em&gt;getStaticProps&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;it is only called once at the build time (when building the app using &lt;code&gt;next build&lt;/code&gt; command)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export async function getStaticProps(context) {
  return {
    props: {}
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  getStaticPaths
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;if you want to generate static pages you can use this method&lt;/li&gt;
&lt;li&gt;it should return an array of &lt;code&gt;paths&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;the pages are created for the paths at build time&lt;/li&gt;
&lt;li&gt;if the pages need some data to be fetched, we use the &lt;em&gt;getStaticProps&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;it might not be required to statically generate all the pages in advance, so you can opt for runtime &lt;em&gt;SSR&lt;/em&gt; using &lt;code&gt;fallback: true&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;by using fallback you can show some loaders if required when the page is being built
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export async function getStaticPaths() {
  // get all the paths for your posts from an API
  // or file system
  const results = await fetch('/api/posts')
  const posts = await results.json()
  // create the paths array
  const paths = posts.map(post =&amp;gt; ({params: {slug: 
  post.slug}}))
  /*
  [
    {params: {slug: 'get-started-with-node'}},
    {params: {slug: 'top-frameworks'}}
  ]
  */
  return {paths}
}

export async function getStaticProps({ params }) {
  const res = await fetch(`/api/post/${params.slug}`)
  const post = await res.json()
  return {
    props: {post}
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  getServerSideProps
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;called on every request on the &lt;em&gt;server&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;used if you want to do some data fetching for dynamic SSR routes&lt;/li&gt;
&lt;li&gt;you will have access to HTTP header, query params,req and res headers&lt;/li&gt;
&lt;li&gt;even if it is client-side navigation, this method is triggered on the server-side and data is sent down. This is actually an extra roundtrip 😢.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export async function getServerSideProps() {
  const response = await fetch(`https://somedata.com`)
  const data = await response.json()

  return { props: { data } }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  getInitialProps
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;not recommended as per docs, but not yet deprecated 💪&lt;/li&gt;
&lt;li&gt;on Server-Side Rendering(SSR) pages it is run on the server and data is passed down as JSON&lt;/li&gt;
&lt;li&gt;for Client-Side Rendering(CSR) it runs on the client&lt;/li&gt;
&lt;li&gt;used to fetch data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: &lt;em&gt;when the page is fetched upon URL/address bar navigation, it is SSR. On client side navigation it is CSR.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  When to use what
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Do you need &lt;em&gt;data at runtime&lt;/em&gt; but &lt;em&gt;don't need SSR&lt;/em&gt;? Use &lt;em&gt;client-side&lt;/em&gt; data fetching.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Do you need &lt;em&gt;data at runtime&lt;/em&gt; but do &lt;em&gt;need SSR&lt;/em&gt;? Use &lt;em&gt;&lt;em&gt;getServerSideProps&lt;/em&gt;&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Do you have pages that rely on data that is &lt;em&gt;cachable&lt;/em&gt; and &lt;em&gt;accessible at build time&lt;/em&gt;? Like from a CMS? Use &lt;em&gt;&lt;em&gt;getStaticProps&lt;/em&gt;&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do you have the same requirement as above but the &lt;em&gt;pages have dynamic URL params&lt;/em&gt;? Use &lt;em&gt;&lt;em&gt;getStaticProps&lt;/em&gt;&lt;/em&gt; and &lt;em&gt;&lt;em&gt;getStaticPaths&lt;/em&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Rendering modes
&lt;/h3&gt;

&lt;p&gt;Basically 3 rendering modes&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Static: pages are built at run time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Server Side: page are built on each request and cached after the initial hit&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Client-side: the rendering happens on the client. The server will not send the HTML markup string. By default, the pages are pre-rendered while using Next.js.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The type of rendering is chosen based on the data fetching strategy we choose(mostly). By default, the pages are pre-rendered by Next. Pre-rendering means, the server sends down an HTML markup string to the client. Once the request is received the client will try to make it interactive by injecting listeners and handlers (hydration).&lt;/p&gt;

&lt;p&gt;By choosing the appropriate data fetching strategy we can decide the rendering mode for the app.&lt;/p&gt;

&lt;p&gt;If your component works with &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DOM APIs&lt;/li&gt;
&lt;li&gt;only on client data,
there is no point in server-side rendering them. We can opt-out of SSR by using,
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const NoSSR=dynamic(()=&amp;gt;import('../component'),{
 loading:()=&amp;gt;&amp;lt;div&amp;gt;Loading.....&amp;lt;/div&amp;gt;,
 ssr:false
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;&amp;lt;NoSSR/&amp;gt;&lt;/code&gt; will always be client rendered.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deployment
&lt;/h3&gt;

&lt;p&gt;By default, it requires a Node.js environment. By using &lt;code&gt;next export&lt;/code&gt;  we can create a pure static build from our Next project and server it.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>nextjs</category>
      <category>shortnotes</category>
    </item>
    <item>
      <title>CORS in short</title>
      <dc:creator>Shihabudheen US</dc:creator>
      <pubDate>Fri, 28 Aug 2020 05:47:50 +0000</pubDate>
      <link>https://dev.to/codermonkey/cors-in-short-422b</link>
      <guid>https://dev.to/codermonkey/cors-in-short-422b</guid>
      <description>&lt;h4&gt;
  
  
  What is CORS ❓
&lt;/h4&gt;

&lt;p&gt;CORS is actually a method to access the resources📁 that are forbidden 🚫  to be used by the client🌐, by default. There exists a &lt;em&gt;&lt;em&gt;same-origin policy&lt;/em&gt;&lt;/em&gt; which ensures clients can only access the resources in their own domain. That means if I am a client with domain &lt;code&gt;xyz.com&lt;/code&gt; I can only access resources in the domain &lt;code&gt;xyz.com&lt;/code&gt;.If I try to access something from &lt;code&gt;lmn.com&lt;/code&gt; it will be blocked 🚫 by the browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/DFj5uwugndWt2IHs4q/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/DFj5uwugndWt2IHs4q/source.gif" alt="cors"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Why CORS❔
&lt;/h4&gt;

&lt;p&gt;The CORS mechanism in the browser helps us to use the resources available in a different domain. With all the CORS enabled ✅ request there is an &lt;code&gt;Origin&lt;/code&gt; header that gets added. In the response sent from the server 🗄️, there will be an &lt;code&gt;access-allowed-origins&lt;/code&gt; header which contains the details of the origins which can utilise that response. The browser will see 🔍 if the origin matches the allowed ones. If it is matched, the response can be consumed by the client. Else, it will throw the CORS error ⛔.&lt;/p&gt;

&lt;p&gt;One point to understand is, CORS is only applicable to clients like browsers. It will not come into picture when the cURL or postman requests are being made. &lt;/p&gt;

&lt;p&gt;The same-origin policy is beneficial 👌 because it prevents 🚫 malicious 🦠  websites and servers from accessing our data. As I told, if the resource is to be accessible, it should be either in the same origin or should be whitelisted 📄 by the server.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>computerscience</category>
      <category>security</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Linters: don't wait to test</title>
      <dc:creator>Shihabudheen US</dc:creator>
      <pubDate>Thu, 20 Aug 2020 19:45:46 +0000</pubDate>
      <link>https://dev.to/codermonkey/linters-don-t-wait-to-test-4mej</link>
      <guid>https://dev.to/codermonkey/linters-don-t-wait-to-test-4mej</guid>
      <description>&lt;p&gt;&lt;code&gt;Linters&lt;/code&gt; help you to analyse your code statically, i.e without actually even running it. This helps us:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;to catch the errors and pitfalls in the code far before doing the testing&lt;/li&gt;
&lt;li&gt;enforce style and coding practises so that, throughout the project, the conventions are strictly followed. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://eslint.org/"&gt;ESLint&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;ESLint&lt;/em&gt; is an open-source project originally created by &lt;em&gt;Nicholas C. Zakas&lt;/em&gt; which provides a pluggable linting utility for JavaScript. It parses your code, analyses it and runs linting rules that may trigger warnings or errors to let you know if your code is right or wrong.&lt;/p&gt;

&lt;h4&gt;
  
  
  Installation
&lt;/h4&gt;

&lt;p&gt;ESLint can be installed either globally or locally.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i -g eslint&lt;/code&gt; or &lt;code&gt;npm i -d eslint&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It is better to install ESLint project-wise because it will save you from running into conflicts. &lt;/p&gt;

&lt;p&gt;If you use VSCode, we can use the &lt;a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint"&gt;ESLint&lt;/a&gt; plugin which is really handy. &lt;/p&gt;

&lt;h4&gt;
  
  
  Configuration of ESLint
&lt;/h4&gt;

&lt;p&gt;ESLint is highly configurable. We can do that either, using&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;configuration comments&lt;/li&gt;
&lt;li&gt;configuration files&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The second approach is easier and productive so we will explain that.&lt;/p&gt;

&lt;h5&gt;
  
  
  Configuration files
&lt;/h5&gt;

&lt;p&gt;When using configuration files, it is entire project-specific. The configuration file can be a JSON, YAML or JS file. It is named &lt;code&gt;.eslintrc.*&lt;/code&gt; and placed at the root of the project. Or else it can go inside the &lt;code&gt;package.json&lt;/code&gt; files under the key &lt;code&gt;eslintConfig&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To create a configuration file you can run the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx eslint --init

// or

yarn run eslint --init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but make sure you have &lt;code&gt;package.json&lt;/code&gt; in the project root. If not you have to create one before running the init command.&lt;/p&gt;

&lt;h5&gt;
  
  
  Available options
&lt;/h5&gt;

&lt;p&gt;The configuration files can take many options. A few of them are &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;parserOptions&lt;/code&gt;: tells ESLint how you want it to parse your code. The available options are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ecmaVersion&lt;/code&gt;: to specify the version of ECMAScript syntax you want to use. For &lt;code&gt;es6&lt;/code&gt; syntax support we can use &lt;code&gt;{ "parserOptions": { "ecmaVersion": 6 } }&lt;/code&gt;, but for the latest keyword support we need to mention it using the &lt;code&gt;env&lt;/code&gt;. By setting &lt;code&gt;{ "env": { "es6": true } }&lt;/code&gt; the &lt;code&gt;es6&lt;/code&gt; syntax support is enabled automatically.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sourceType&lt;/code&gt;: set to &lt;code&gt;script&lt;/code&gt;(default) or &lt;code&gt;module&lt;/code&gt; if your code is in ECMAScript modules.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ecmaFeatures&lt;/code&gt;: an object indicating which additional language features you'd like to use.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;globalReturn&lt;/code&gt;: enable global &lt;code&gt;return&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;jsx&lt;/code&gt;: enable &lt;code&gt;jsx&lt;/code&gt; support&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;impliedStrict&lt;/code&gt;: enable global strict mode (version &amp;gt; ECMA5)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;code&gt;parser&lt;/code&gt;: ESLint uses &lt;code&gt;espree&lt;/code&gt; by default as the parser. We can change it by passing a &lt;code&gt;parser&lt;/code&gt; option in the configuration. Even with a separate parser, &lt;code&gt;parserOptions&lt;/code&gt; are to be passed. The supported parsers are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;esprima&lt;/li&gt;
&lt;li&gt;babel-eslint&lt;/li&gt;
&lt;li&gt;@typescript-eslint/parser&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;plugins&lt;/code&gt;: plugins are a set of ESLint rules related to some specific subject. As an example, &lt;code&gt;eslint-plugin-react&lt;/code&gt; contains many rules related to React. If needed the &lt;code&gt;eslint-plugin-&lt;/code&gt; prefix can be omitted from the plugin name.&lt;br&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    // ...
    "plugins": [
        "jquery", // means eslint-plugin-jquery
        "@jquery/jquery", // means @jquery/eslint-plugin-jquery
        "@foobar" // means @foobar/eslint-plugin
    ]
    // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Caution&lt;/em&gt;: you have to install the plugin as a &lt;code&gt;dev&lt;/code&gt; &lt;br&gt;
  dependency for your rules to work properly.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;processor&lt;/code&gt;: some plugins may come with processors, which helps to extract JS code from other file types. Or it can also convert the JS code to other formats/types. &lt;a href="https://eslint.org/docs/user-guide/configuring#specifying-processor"&gt;more...&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;env&lt;/code&gt;: it is used to specify which environments your script is designed to run in. Each environment brings with it a certain set of predefined global variables. For example, when using testing tools like &lt;em&gt;protractor&lt;/em&gt;, there are a few global keywords that are &lt;em&gt;protractor&lt;/em&gt; specific. We can use &lt;code&gt;env&lt;/code&gt; to enable them. To enable an env, just add it in the object with value as true and the environment as the key.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "env": {
        "browser": true,
        "node": true
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;globals&lt;/code&gt;: if there are any user-defined global variables that are being accessed inside the script, that can go inside the &lt;code&gt;globals&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "globals": {
        "var1": "writable",
        "var2": "readonly"
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;rules&lt;/code&gt;: which rules are enabled and at what error level. Following are the error levels available:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;-&lt;code&gt;off&lt;/code&gt;/&lt;code&gt;0&lt;/code&gt; - turn the rule off&lt;/p&gt;

&lt;p&gt;-&lt;code&gt;warn&lt;/code&gt;/&lt;code&gt;1&lt;/code&gt; - turn the rule on as a warning (doesn't affect exit code)&lt;/p&gt;

&lt;p&gt;-&lt;code&gt;error&lt;/code&gt;/&lt;code&gt;2&lt;/code&gt; - turn the rule on as an error (exit code is 1 when triggered)&lt;/p&gt;

&lt;h4&gt;
  
  
  Ignoring files and directories
&lt;/h4&gt;

&lt;p&gt;In order to ignore files from getting linted, we can do it either by creating an &lt;code&gt;ignorePatterns&lt;/code&gt; field in the configuration or by creating a &lt;code&gt;.eslintignore&lt;/code&gt; file in the project root.&lt;/p&gt;

&lt;h3&gt;
  
  
  More read
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/yannickcr/eslint-plugin-react"&gt;For React&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>codequality</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Proxy in short</title>
      <dc:creator>Shihabudheen US</dc:creator>
      <pubDate>Thu, 13 Aug 2020 20:09:59 +0000</pubDate>
      <link>https://dev.to/codermonkey/pro-xy-1399</link>
      <guid>https://dev.to/codermonkey/pro-xy-1399</guid>
      <description>&lt;h3&gt;
  
  
  What is proxy❓
&lt;/h3&gt;

&lt;p&gt;Proxies are servers 🏛️ that sit in between your machine 💻 and the server 🗄️ you are trying to reach. It is kind of a middleman 👮‍♀️. When you request data from a server, that request is intercepted and forwarded by the proxy server. For the data to reach back, it has to pass through the same proxy again.&lt;/p&gt;

&lt;p&gt;When you are behind a proxy 🕸️, your computer's IP will never be shared. All your internet traffic would be seen as coming from a different IP, that is of the proxy server. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/yEeoTTV3a8wbkpHopd/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/yEeoTTV3a8wbkpHopd/giphy.gif" alt="proxy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why to use a proxy❔
&lt;/h3&gt;

&lt;p&gt;Proxy servers are usually used to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ensure security 🔐&lt;/li&gt;
&lt;li&gt;site whitelisting 📄&lt;/li&gt;
&lt;li&gt;monitoring 🛰️&lt;/li&gt;
&lt;li&gt;fake location 🗺️&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Depending upon the purpose, there are different types of proxy servers. Some of them are:&lt;/p&gt;

&lt;p&gt;1.Transparent proxy&lt;/p&gt;

&lt;p&gt;Transparent proxies are the simplest kind of proxy. They pass all of your information along, but with the proxy's IP address. These proxies don't offer any kind of privacy protection.&lt;/p&gt;

&lt;p&gt;2.Anonymous proxy&lt;/p&gt;

&lt;p&gt;Anonymous 👻 proxies are a commonly used type of proxy. They never pass your IP address to the website you are browsing although they will identify themselves as a proxy in the request. This helps you to keep your browsing activity private 🦹‍♀️.&lt;/p&gt;

&lt;p&gt;3.Rotating proxy&lt;/p&gt;

&lt;p&gt;Rotating proxies work a little differently from the others. Every time a client connects to the proxy, a new IP address is created for it. So they never use the same IP address more than once.&lt;/p&gt;

&lt;p&gt;4.Reverse proxy&lt;/p&gt;

&lt;p&gt;Reverse proxies are completely different from everything we've covered so far. A reverse proxy hides the IP address of a server you're trying to send a request to. When a server needs security and privacy from clients, that's when these types of proxies come in.&lt;/p&gt;

&lt;h4&gt;
  
  
  VPNs
&lt;/h4&gt;

&lt;p&gt;VPN(Virtual Private Network)s  are also meant for security and monitoring. But it will control and monitor all the network activities, including FTP downloads and uploads, and background OS operations like updates. In that way, VPNs are much more secure 🔐 than proxy servers. When you are using a VPN, even the ISP(Internet Service Provider) can't see your internet activity. All they can see is, just connected 🔌 to a VPN. So, your requests are highly private and concealed when using a VPN.&lt;/p&gt;

&lt;p&gt;Compared to proxy servers, VPNs are expensive 💰 to run and maintain.&lt;/p&gt;

</description>
      <category>internet</category>
      <category>network</category>
      <category>security</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Build tools (purely front end)</title>
      <dc:creator>Shihabudheen US</dc:creator>
      <pubDate>Thu, 13 Aug 2020 19:01:22 +0000</pubDate>
      <link>https://dev.to/codermonkey/build-tools-purely-front-end-1c1g</link>
      <guid>https://dev.to/codermonkey/build-tools-purely-front-end-1c1g</guid>
      <description>&lt;p&gt;🚨 With newer HTTP 2/3 a few of things being discussed below are outdated. But the usage of build tool can’t still be ruled out.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why build tools❓
&lt;/h3&gt;

&lt;p&gt;Often times we love ♥️ to keep all our files separate and tidy. For example, consider a simple HTML project. We will have separate files for JS, styles and HTML. As the project grows and with more pages getting added, the file sizes and their number may also increase. Keeping files separate, maintenance is a lot easier. In case of a bug, it is easier to track and patch. In short, development becomes hassle-free😎.&lt;/p&gt;

&lt;p&gt;But what about production?🤔 In prod, the story is different, as we can't afford too many files. For each file, we need to make a separate HTTP call and that can be quite expensive. So in production, it is better to keep the file count as minimum as possible. &lt;/p&gt;

&lt;p&gt;One way is to combine these files. We can combine all our JS, CSS and HTML code into individual files. If we start doing it manually, it can be really cumbersome and tedious😫. We may easily lose the track too. This is where we can rely upon a set of tools termed &lt;code&gt;build-tools&lt;/code&gt;🧰.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are build tools❔
&lt;/h3&gt;

&lt;p&gt;As the name indicates, build-tools are tools, that help us in creating builds. Builds are production-ready executables of our project. Build tools, help us do more than concatenating our files. &lt;/p&gt;

&lt;p&gt;We can use it for ♦️file compression, ♦️removal of white spaces and unwanted codes, ♦️linking of files and much more. If we can think of all these operations as individual tasks, build-tools are helping us to automate them. Run a single command and build tools will do its magic✨.&lt;/p&gt;

&lt;p&gt;There are a lot of build tools out there and each serves different use cases. To name a few, 👉 Grunt, 👉 Gulp, 👉 &lt;br&gt;
 parcel.js and 👉 Webpack. Some of them are easy to use, but a few require a steep learning curve. So it is better to learn a tool, only if it is required. &lt;/p&gt;

&lt;p&gt;For small projects, one might not require a build tool at all. But if your project is quite big and complex it is wise to use a build tool. &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>frontend</category>
      <category>tools</category>
    </item>
    <item>
      <title>Rethinking JS [short notes]</title>
      <dc:creator>Shihabudheen US</dc:creator>
      <pubDate>Mon, 10 Aug 2020 17:35:50 +0000</pubDate>
      <link>https://dev.to/codermonkey/scribble-3mkj</link>
      <guid>https://dev.to/codermonkey/scribble-3mkj</guid>
      <description>&lt;h2&gt;
  
  
  Mental model 🧠
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Mental models are how we think 🤔 about something&lt;/li&gt;
&lt;li&gt;Slow and fast thinking&lt;/li&gt;
&lt;li&gt;Slow thinking is laborious, front lobe&lt;/li&gt;
&lt;li&gt;⚡️ Fast thinking is less tiring and most often preferred(default)&lt;/li&gt;
&lt;li&gt;Mental models are essential to write good code, easy to reason about and less prone to errors&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Context
&lt;/h2&gt;

&lt;p&gt;You are on the JS asteroid in the space. You see stars⭐️, planets🪐 and asteroids ☄️ floating in space 🌌.&lt;/p&gt;

&lt;h2&gt;
  
  
  Values and Expressions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Values
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;values are things. They are like numbers in Math, words in a sentence and dots in geometry. It is a thing 🧱. We can't 🚫 do much to them, but we can do things with them&lt;/li&gt;
&lt;li&gt;there are two types of values in JS. Primitive and Composite&lt;/li&gt;
&lt;li&gt;Primitive values are numbers and strings(and few more). They are like far distant stars and we can only look and refer them, but we can't change them or affect them.&lt;/li&gt;
&lt;li&gt;Composite values are different. We can manipulate them from code. Like functions and objects. They are like rocks closer to the asteroid that we are on.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Expression
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;expressions are kind of questions ❓ that we ask JS. The expressions always result in values. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;typeof&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;to know the type of value we can use &lt;code&gt;typeof&lt;/code&gt; operator. &lt;br&gt;
&lt;code&gt;typeof _value&lt;/code&gt; will give us the type of the value as &lt;em&gt;string&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The types can be, &lt;br&gt;
Primitive&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;undefined (undefined)&lt;/li&gt;
&lt;li&gt;null (object)&lt;/li&gt;
&lt;li&gt;number (number)&lt;/li&gt;
&lt;li&gt;bigint&lt;/li&gt;
&lt;li&gt;symbol&lt;/li&gt;
&lt;li&gt;string&lt;/li&gt;
&lt;li&gt;boolean&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Composite&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;object (object)&lt;/li&gt;
&lt;li&gt;function (function)&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Primitives are immutable
&lt;/h2&gt;

&lt;p&gt;In JS, primitives are immutable. 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 name='yikes'
name[0]='l' // can't change
console.log(name) // 'yikes'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though string appears to be similar to an array, which is not a primitive we might have an intuition that we can mutate or change it. But in practice, we can't since the strings are primitive. This also applies to all the primitives.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let number=10
number.value='ten'
console.log(number) // 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the addition of a property is also a kind of mutation, this too is not allowed on Primitives.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variables
&lt;/h3&gt;

&lt;p&gt;Variables are like wires. We can connect the variables to values. In order to connect a variable wire to a value, we use assignment statements.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let x='Shihab'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the variable wire x is connected to &lt;em&gt;string value&lt;/em&gt; Shihab. The RHS of an assignment is always an expression.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let world='World'
let say='Hello '+ world
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we are asking JS, what is &lt;code&gt;'Hello '+world&lt;/code&gt; it is an expression which resolves to a &lt;em&gt;value&lt;/em&gt; &lt;code&gt;'Hello World'&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The RHS of &lt;code&gt;let x='Shihab'&lt;/code&gt; is also an expression, since it also resolves to a value 'Shihab'. We call it &lt;em&gt;literlas&lt;/em&gt; since we write down the exact value. &lt;/p&gt;

&lt;p&gt;In JS, we always &lt;em&gt;pass the value&lt;/em&gt; and not the variable itself. We cannot change what the variable points to, but at times we can change the value itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num=10
function double(x){
   x=x*2
}
double(num) // here we pass the value 10 
            // and not the reference to it
console.log(num) // 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr=[10,20]
function mutate(input){
  input[0]=30
}
mutate(arr)
console.log(arr) // [30,20]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because we pass the value of arr which is [10,20]. Since arrays are mutable, we were able to mutate the value. And the function &lt;em&gt;cannot change the value arr was wired to&lt;/em&gt;, thus we get [30,20] when trying to print arr.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h1vQ4Alt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200809/variables.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h1vQ4Alt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200809/variables.png" alt="image" width="333" height="237"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Counting Values
&lt;/h2&gt;

&lt;p&gt;We should always think, values as having a precise count.&lt;/p&gt;

&lt;p&gt;Undefined ----&amp;gt; Undefined [1]&lt;br&gt;
null -----&amp;gt; null&lt;br&gt;
Boolean -----&amp;gt; true or false [2]&lt;br&gt;
Number ----&amp;gt; 18 quintillion [...]&lt;br&gt;
BigInit ---&amp;gt; Use for arbitrary precision and no round-off. Mainly used in financial calculations.&lt;br&gt;
String ---&amp;gt; A string for each conceivable string that exists in the universe. A string has properties but it is not as same as other objects. Since the string is primitive it is immutable.&lt;br&gt;
Symbols ---&amp;gt; recently new&lt;br&gt;
Objects ---&amp;gt; Each time it creates a brand new Object&lt;br&gt;
Function ---&amp;gt; Each function expressions are distinct. Like any other things in JS, functions are expressions too. When it is called with () [Call expression] JS resolves it to the return value of it. If not, it resolves to function expression or body. Function are also Objects, but special objects. Whatever you can do with Objects can be done with functions too. But what makes function different is, the can be invoked.&lt;/p&gt;

&lt;p&gt;In this we way, we have can better place and point our variables to values. In our model, there should be only two booleans, and one &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;null&lt;/code&gt;. All the time, when a primitive is being referred, JS actually &lt;em&gt;summons&lt;/em&gt; them. But in the case of Objects {} and functions (), it creates a brand new value for us.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a2oEMHOX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200809/string.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a2oEMHOX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200809/string.png" alt="" width="520" height="284"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1ipgSxr4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200809/obj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1ipgSxr4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200809/obj.png" alt="" width="378" height="258"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f5vL0gv2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200809/func.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f5vL0gv2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200809/func.png" alt="" width="562" height="254"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Equality in JS
&lt;/h2&gt;

&lt;p&gt;In JS there are mainly 3 types of equalities&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Same Value &lt;code&gt;Object.is()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Strict equality &lt;code&gt;===&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Loose equality &lt;code&gt;==&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Same Value
&lt;/h3&gt;

&lt;p&gt;Same value returns &lt;code&gt;true&lt;/code&gt; is we are pointing to the same values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gxwz9g9C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200809/same-value.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gxwz9g9C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200809/same-value.png" alt="" width="874" height="340"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Strict Value
&lt;/h3&gt;

&lt;p&gt;It is same as &lt;code&gt;Object.is()&lt;/code&gt; expect for&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NaN === NaN // false
0 === -0 // true
-0 === 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To test, if a number is &lt;code&gt;NaN&lt;/code&gt; we can use &lt;code&gt;Number.isNaN()&lt;/code&gt; or &lt;code&gt;num !== num&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Loose Equality
&lt;/h3&gt;

&lt;p&gt;It just compares the sameness of values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2=='2'
true==0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Properties
&lt;/h2&gt;

&lt;p&gt;Properties are similar to variables. They also point to values, but they start from an Object and they belong to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sherlock={
 surname:'Homes',
 address:{
  city:'London'
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9rF7B7kA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/object.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9rF7B7kA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/object.png" alt="Object" width="880" height="254"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even though it seems like a single object is being created there are actually two distinct objects here. An object can never reside inside another object, even though it might seem nested from code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sherlock={
 surname:'Holmes',
 age:64
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Rules of reading a property
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;console.log(sherlock.age)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/Y4QVGQxzKLi6NWL4Bn/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/Y4QVGQxzKLi6NWL4Bn/source.gif" alt="Read property" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Properties will have names, which are strings basically. They must be unique within an object,i.e. an object cannot have two keys with the same name. The names are &lt;em&gt;case sensitive&lt;/em&gt; too.&lt;/p&gt;

&lt;p&gt;These &lt;em&gt;rules&lt;/em&gt; look roughly like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Figure out the value of the part before the dot (.).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If that value is &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;, throw an error immediately.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check whether a property with that name exists in our object.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;a. If it exists, answer with the value this property points to.&lt;/p&gt;

&lt;p&gt;b. If it doesn’t exist, answer with the &lt;code&gt;undefined&lt;/code&gt; value.&lt;/p&gt;

&lt;p&gt;If a property is missing, we get an &lt;code&gt;undefined&lt;/code&gt;. But it doesn't mean that we have that property on the object pointing to &lt;code&gt;undefined&lt;/code&gt;. It is more like, we are asking JS for the value (expression) and it is replying us that it is not defined, &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Assigning to a property
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;sherlock.age=65&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;figure out which wire is on the left side&lt;br&gt;
&lt;a href="https://i.giphy.com/media/YNDeBFbMKydC7ZiWFV/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/YNDeBFbMKydC7ZiWFV/source.gif" alt="Read left" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;we figure out which value is on the right side&lt;br&gt;
&lt;a href="https://i.giphy.com/media/eid5NvMYm5yOPUF9Qz/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/eid5NvMYm5yOPUF9Qz/source.gif" alt="Read right" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;point the wire on the left side to the value on the right side&lt;br&gt;
&lt;a href="https://i.giphy.com/media/Xev7nPYAXhMpRzbH0e/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/Xev7nPYAXhMpRzbH0e/source.gif" alt="Point" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Mutation
&lt;/h3&gt;

&lt;p&gt;Suppose we have the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sherlock={
 surname:'Holmes',
 address:{
   city:'London'
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/kFBjlccU7RbwRzVDMg/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/kFBjlccU7RbwRzVDMg/source.gif" alt="sherlock object" width="480" height="282"&gt;&lt;/a&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 john={
 surname:'John',
 address: sherlock.address
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/J4U1l5AinL8PSzvBv0/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/J4U1l5AinL8PSzvBv0/source.gif" alt="john object" width="480" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we want to change &lt;code&gt;john&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;john.surname='Lennon'
john.address.city='Malibu'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/ftpkfq5GOxRA5HlbRN/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ftpkfq5GOxRA5HlbRN/source.gif" alt="mutation" width="480" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But we observe we could see &lt;code&gt;sherlock.address.city&lt;/code&gt; has also changed to &lt;code&gt;Malibu&lt;/code&gt; from &lt;code&gt;London&lt;/code&gt;. This is because both &lt;code&gt;sherlock.address&lt;/code&gt; and &lt;code&gt;john.address&lt;/code&gt; pointed to the same Object. &lt;/p&gt;

&lt;p&gt;So because of this, the mutation can be dangerous. It might unintentionally change the values at all the places where it is being referred.&lt;/p&gt;

&lt;p&gt;In order to avoid mutation, we could have done the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When mutating &lt;code&gt;john&lt;/code&gt;,
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;john={
 surname:'Lennon',
 address:{ city: 'Malibu' }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oo9yMd0_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed936.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oo9yMd0_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed936.png" alt="mutation 1" width="880" height="781"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;john.surname='Lennon'
john.address={ city:'Malibu' }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--985LO7pf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--985LO7pf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-1.png" alt="mutation 2" width="880" height="681"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Is Mutation that Bad?
&lt;/h3&gt;

&lt;p&gt;The mutation is not bad at all, but we should pay closer attention to it. The bliss with the mutation is, it helps us update or change a value realtime at multiple places. If think the other way, that is misery with it too.&lt;/p&gt;

&lt;p&gt;Even though you declare an Object with &lt;code&gt;const&lt;/code&gt; it will not presentation mutation to the Object. It will only prevent the reassignments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const x = {
  name:'Shihab'
}

x.name = 'Shifa' // allowed
x.age = 22 // allowed

x = {} // not allowed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Prototype &lt;code&gt;__proto__&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let human={
 teeth:32
}

let gwen={
 age:19
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Krytl07J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Krytl07J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-2.png" alt="" width="836" height="472"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(gwen.teeth) // undefined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But we can access &lt;code&gt;teeth&lt;/code&gt; property of &lt;code&gt;human&lt;/code&gt; in &lt;code&gt;gwen&lt;/code&gt; by,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let gwen={
 __proto__: human
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/MCoic0hBdqXimi1HGh/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/MCoic0hBdqXimi1HGh/source.gif" alt="giphy" width="600" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(gwen.teeth) // 32&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;With adding &lt;code&gt;__proto__&lt;/code&gt; we instruct JS, to &lt;em&gt;continue&lt;/em&gt; searching for &lt;code&gt;teeth&lt;/code&gt; in &lt;code&gt;__proto__&lt;/code&gt; too. &lt;/p&gt;

&lt;h3&gt;
  
  
  Prototype Chain
&lt;/h3&gt;

&lt;p&gt;The search for the values will continue until the base &lt;code&gt;prototype&lt;/code&gt; is reached. In JS the base &lt;code&gt;prototype&lt;/code&gt; is &lt;code&gt;Object.__proto__&lt;/code&gt; which is set to &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As you can see, so this is kind of a chain that is getting created when we as JS to look for a property on an Object. This is being referred to as &lt;code&gt;prototype chain&lt;/code&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 mammal={
 brainy:true
}

let human={
 __proto__:mammal,
 teeth:32
}

let gwen={
 __proto__:human,
 age:19
}

console.log(gwen.brainy) // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1hcx3p2v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1hcx3p2v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-4.png" alt="" width="880" height="736"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Shadowing
&lt;/h3&gt;

&lt;p&gt;When an Object has the same property on it and as well as inside the &lt;code&gt;__proto__&lt;/code&gt;, the own &lt;em&gt;shadows&lt;/em&gt; the value on &lt;code&gt;__proto__&lt;/code&gt;. This is called &lt;em&gt;Shadowing&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B19x3qJ3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B19x3qJ3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-5.png" alt="" width="810" height="602"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Assignments
&lt;/h3&gt;

&lt;p&gt;The property assignments directly happen on the Object and not on the &lt;code&gt;__proto__&lt;/code&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 human={
 teeth:32
}

let gwen={
 __proto__:human
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Qa-OuFtY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Qa-OuFtY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-6.png" alt="" width="823" height="524"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On &lt;code&gt;gwen.teeth=31&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Qze0n9S7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Qze0n9S7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-7.png" alt="" width="825" height="524"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To check if the property belongs to an Object or its &lt;code&gt;__proto__&lt;/code&gt;, we have a method called &lt;code&gt;hasOwnProperty&lt;/code&gt; on Object. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;ObjectName.hasOwnProperty(prop)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the &lt;code&gt;prop&lt;/code&gt; is a property on &lt;code&gt;ObjectName&lt;/code&gt;, it will return &lt;code&gt;true&lt;/code&gt; if not &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Object prototype
&lt;/h3&gt;

&lt;p&gt;When we create a new Object, there is a &lt;code&gt;__proto__&lt;/code&gt; that gets added by default. It is the prototype of the Object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6TgguIqL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6TgguIqL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-9.png" alt="" width="880" height="615"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To terminate the prototype chain of any Object we can just assign &lt;code&gt;null&lt;/code&gt; to its &lt;code&gt;__proto__&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Polluting prototype
&lt;/h3&gt;

&lt;p&gt;All the &lt;em&gt;in-built&lt;/em&gt; methods and properties of Objects, Arrays and Strings are defined in the &lt;code&gt;__proto__&lt;/code&gt; of their base. In this way, these get shared among all the values, that are being created out of it. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FkTWGgyD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FkTWGgyD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-10.png" alt="" width="880" height="715"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But this practice of sharing is highly discouraged.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0wAOSPi7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-11.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0wAOSPi7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.techpowerup.org/200810/unnamed-11.png" alt="" width="880" height="785"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;But the sharing of methods and properties via the prototype chain is the base of classes and all other features. But the direct usage of &lt;em&gt;polluting prototype&lt;/em&gt; is not recommended.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;proto&lt;/strong&gt; vs. prototype
&lt;/h3&gt;

&lt;p&gt;You might be wondering: what in the world is the prototype property? &lt;/p&gt;

&lt;p&gt;The story around this is confusing. Before JavaScript added classes, it was common to write them as functions that produce objects, 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;function Donut() {
  return { shape: 'round' };
}

let donut = Donut();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’d want all donuts to share a prototype with some shared methods. However, manually adding &lt;code&gt;__proto__&lt;/code&gt; to every object looks gross:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Donut() {
  return { shape: 'round' };
}

let donutProto = {
  eat() {
    console.log('Nom nom nom');
  }
};

let donut1 = Donut();
donut1.__proto__ = donutProto;
let donut2 = Donut();
donut2.__proto__ = donutProto;

donut1.eat();
donut2.eat();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a shortcut, adding &lt;code&gt;.prototype&lt;/code&gt; on the function itself and adding &lt;code&gt;new&lt;/code&gt; before your function calls would automatically attach the &lt;code&gt;__proto__&lt;/code&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 Donut() {
  return { shape: 'round' };
}
Donut.prototype = {
  eat() {
    console.log('Nom nom nom');
  }
};

let donut1 = new Donut(); // __proto__: Donut.prototype
let donut2 = new Donut(); // __proto__: Donut.prototype

donut1.eat();
donut2.eat();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this pattern has mostly fallen into obscurity, but you can still see prototype property on the built-in functions and even on classes. To conclude, a function’s &lt;code&gt;prototype&lt;/code&gt; specifies the &lt;code&gt;__proto__&lt;/code&gt; of the objects created by calling that function with a &lt;code&gt;new&lt;/code&gt; keyword.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
      <category>programming</category>
    </item>
    <item>
      <title>An async boring sync example😆</title>
      <dc:creator>Shihabudheen US</dc:creator>
      <pubDate>Wed, 29 Jul 2020 16:59:38 +0000</pubDate>
      <link>https://dev.to/codermonkey/an-async-boring-sync-example-523p</link>
      <guid>https://dev.to/codermonkey/an-async-boring-sync-example-523p</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;When learning about JavaScript, one would have definitely come across the terms single-threaded, synchronous and asynchronous. In this article, I am trying to explain what it actually means (or at least how I understood). &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The boring example 🤖.
&lt;/h2&gt;

&lt;p&gt;Let us start with an example. Suppose, you are running a takeaway restaurant. Your menu includes 🍵(tea), ☕(coffee),🥪(sandwich) and 🍔(burger). Yes, you only serve four items for now 🤣. Currently, you operate via a single counter since you are the only employee. You have to take the order, do the billing, and prepare it, and all by yourself. Pretend you are a born multi-tasker 🦸‍♂️. &lt;/p&gt;

&lt;p&gt;With that said, you can only serve a single customer at any point in time. The time to fulfil an order is as follows:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Item&lt;/th&gt;
&lt;th&gt;Time 🕐&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;coffee&lt;/td&gt;
&lt;td&gt;2 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sandwich&lt;/td&gt;
&lt;td&gt;15 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;burger&lt;/td&gt;
&lt;td&gt;15 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;tea&lt;/td&gt;
&lt;td&gt;2 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Since it is a single queue system even ☕ and 🍵 takers have to wait if they behind have any 🍔 or 🥪 buyers in the queue. People are people😬, and no one wants to spend their whole day just waiting in a long queue. Soon you found that you are losing the customers, all because of this long wait time😩.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gifyu.com/image/WjHn" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs7.gifyu.com%2Fimages%2FsyncCounter.gif" alt="syncCounter.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/l0HlBO7eyXzSZkJri/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l0HlBO7eyXzSZkJri/giphy.gif" alt="waiting"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the question is how can you retain your customers and earn more❓ The answer is super simple, reduce the wait time and server more people 🏆. Let us see two approaches (there are n other approaches as well).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3ohhwABuFKvlqx4u4w/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3ohhwABuFKvlqx4u4w/giphy.gif" alt="Let us do it"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach 1️⃣
&lt;/h2&gt;

&lt;p&gt;We can add new 🤵(assistants) for processing the orders and 👨‍🍳(cooks) for preparation. Now an assistant/waiter will serve a burger or sandwich order. The ready to serve orders can be still managed by you🏋️‍♂️. Each time a  🍔 or 🥪 order comes up, you call upon a waiter to get it done. The waiter will take the order, inform the cook and wait until the cook prepares the order. Once it is ready, the order is well packed and handed over to the customer by the waiter. To ensure super fast delivery⚡️ for an order, a cook and a waiter work together from order taking to delivery. So the 🥪 and  🍔 customers are no more blocking the ☕ or 🍵 orders.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gifyu.com/image/WjHQ" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs7.gifyu.com%2Fimages%2FmultiThread.gif" alt="multiThread.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;❎ The problem with this approach is, the waiter keeps on doing what they are meant to do, waiting... They are simply waiting 🏄🏾‍♀️ while the food is being cooked. Until an order is placed, the cook is waiting🏌🏾‍♂️ too. In short, now the waiter and cook waste a lot of time just, waiting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach 2️⃣
&lt;/h2&gt;

&lt;p&gt;Let us try introducing a 🏷(token) system only for the 🥪 and 🍔 orders. We take the order at a single counter and assign a 🏷 for each 🥪 and 🍔 orders. The customer clears the counter after collecting their token. Once the order is ready 🍛, the token number is called out 📣 and the 📦(parcel) is handed over through the main counter. Here too, we rely on extra 👨‍🍳(cooks) for preparation and a 🤵(single waiter). The called out customers join the queue to collect their order.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gifyu.com/image/WjHW" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs7.gifyu.com%2Fimages%2FsingleThread.gif" alt="singleThread.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this approach, the overall 🕰(time) has 📈increased (but still lower than the existing model), but the wait time is somewhat judiciously distributed. &lt;/p&gt;

&lt;h2&gt;
  
  
  Sync vs Async 🎊
&lt;/h2&gt;

&lt;p&gt;Now let us get into the meat👽. The currently existing model of operation, the one before the optimization is kind of synchronous flow. Succeeding customers are awaiting for the previous order to be fulfilled. As the customers are blocked by the guys in front of them, we call it a blocking model. &lt;/p&gt;

&lt;p&gt;The new two approaches can be considered as asynchronous or non-blocking (still there is a small wait time). As a separate 🤵-👨‍🍳 pair is working on a single order, the first approach is more like a multi-threaded one. The second approach is still kind of single-threaded but still unblocking.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some JS stuff
&lt;/h2&gt;

&lt;p&gt;As per docs, JS is said to be synchronous and single-threaded. Inherently synchronous operations are blocking as we just saw above. Being said, JS is synchronous have you ever felt it 🤔? Has your screen frozen when scrolling through your Facebook posts? Next time while googling, try to type and search at the same time(nearly instantaneous). I was able to search for videos, while my YouTube was still playing in mini-player mode. We know that JS is doing all of this in our browser and we were never blocked from multi-task. So is JavaScript actually synchronous? Let me know the answers in comments...👇&lt;/p&gt;

&lt;p&gt;If you were paying attention, by now you would have the answer...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/fngcM35fl2ySrblJLV/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/fngcM35fl2ySrblJLV/giphy.gif" alt="down"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JS has a single thread (in JS engine) which processes all your tasks. The time taking jobs(n/w calls, timing functions) are pushed out and processed by separate engines. Once they re done, they maintain a secondary queue(callback/microtask queue). Once the high priority/synchronous tasks are completed, the items in the secondary queue are pushed to the main queue(call stack), where they are served one by one, by the main thread. In short, we make the fewer priority tasks WAIT ⏰&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>basics</category>
      <category>computerscience</category>
      <category>concept</category>
    </item>
    <item>
      <title>JS: 🌈 Feel proud of it </title>
      <dc:creator>Shihabudheen US</dc:creator>
      <pubDate>Mon, 20 Jul 2020 14:04:06 +0000</pubDate>
      <link>https://dev.to/codermonkey/js-feel-proud-of-it-4okh</link>
      <guid>https://dev.to/codermonkey/js-feel-proud-of-it-4okh</guid>
      <description>&lt;h2&gt;
  
  
  What is JavaScript❓
&lt;/h2&gt;

&lt;p&gt;JavaScript is a scripting language. According to Stackoverflow’s 2020 survey insights, JavaScript is the second most loved 🤟 programming language. &lt;/p&gt;

&lt;p&gt;JavaScript was ‘born to make web live’ and thus the creator called it LiveScript. But from the scripting language that Brendan Eich created to make Mosaic(browser) lively, it has come a long way.  Now JS is in your browser, on your phone and even in space 🚀.&lt;/p&gt;

&lt;p&gt;So, take a moment to appreciate yourself for choosing JS and sticking with it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/lEVZJzy4w15qE/giphy-downsized-large.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/lEVZJzy4w15qE/giphy-downsized-large.gif" alt="Respect"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  They says🗣️
&lt;/h2&gt;

&lt;p&gt;As always, Wikipedia is our one-stop for all the questions. Wiki📚 says,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;JavaScript, often abbreviated as JS, is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm. It has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-class functions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is a lovely definition I would say, still there are many things unclear. What is ECMAScript, just-in-time compiled and what on earth is first-class functions? So let us try to answer a few. (These are not my answers, this is what Google gave me!!!)😆&lt;/p&gt;

&lt;h2&gt;
  
  
  I says 🙋‍♂️
&lt;/h2&gt;

&lt;p&gt;Let us start with answering where is JS running🏃‍♂️? Most of the time it is running inside our browsers. Nowadays, browsers are so complex and it has many engines, compilers, tokenizers, this and that, running together just to show us a web page. As JS is mighty, browsers have a dedicated engine only for JS called, JavaScript Engine ⚙️, shortened as JSE.&lt;/p&gt;

&lt;h3&gt;
  
  
  ECMAScript
&lt;/h3&gt;

&lt;p&gt;Out there we have many browsers and they have their own tweaks and quirks for the JSE. But JS is guaranteed✅ to run the same everywhere. There shouldn’t be anything like, in Chrome but not in Firefox. This is where &lt;a href="https://www.ecma-international.org/"&gt;ECMA&lt;/a&gt; comes into the picture. ECMA is entrusted with standardising JS. And for this, ECMA has a  general-purpose language called ECMAScript and JS is a language standardised based on it. That’s all about ECMAScript.&lt;/p&gt;

&lt;p&gt;Oftentimes, one will come across terms like ES5 and ES6, associated with JS for sure. The JS community is so active and they come up with newer syntactic sugars (less code, do more) and new features, to keep the language up and going. With each year, ECMA releases a new version for JS specifications with new additions and features. These are being referred to as ES5(2009), ES6(2015) and so on (yeap, there is a career gap)🤣. When writing this article, the latest version of ECMA out is ES11. But it is up to the browsers what version to use and when to adopt new changes. As of today (mid-2020), ES5 is the only 100% all browser supported ECMA version. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/BgBf6pW9qOgQU/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/BgBf6pW9qOgQU/giphy.gif" alt="Old"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Just-in-time complied
&lt;/h3&gt;

&lt;p&gt;Computers💻 are dumb machines and they can only understand two states ON and OFF, or 0 and 1. But learning a binary language to code is cumbersome and too much. As developers most of the time we are coding in high-level languages which are more human-readable. Under the hood ⚒️, the high-level language code gets translated to machine friendly binary streams by translators👨‍🏫. &lt;/p&gt;

&lt;p&gt;And currently, what we have is two types of translators out there. One guy is a pro, who translates the entire code and creates the low-level equivalent in a single go. The other guy is still a noob and can only translate the code line by line. We call the pros as compilers and the noobs as interpreters.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iDpGTHi5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://s7.gifyu.com/images/compilerad13b2781c006b61.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iDpGTHi5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://s7.gifyu.com/images/compilerad13b2781c006b61.gif" alt="Compiler"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mdFpw_k4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://s7.gifyu.com/images/reduced.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mdFpw_k4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://s7.gifyu.com/images/reduced.gif" alt="Interpreter gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As of today, JS is said to be an interpreted language(I don't agree completely)🙊. That means code conversion and execution is always and only, one line at a time. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The past is history, the future is a mystery and the present is the only truth! &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is what, just in time compilation signifies. &lt;/p&gt;

&lt;h3&gt;
  
  
  First-class functions
&lt;/h3&gt;

&lt;p&gt;And (un)fortunately, JS is a programming language with first-class functions. With that, we can assign a function to a variable, pass around the functions as arguments to other functions and return a function from another function. I would say the most beautiful and interesting trait of the language is evident when a function is returned from another function. (Sorry, out of scope for this article.)🙇‍♂️&lt;/p&gt;

&lt;h2&gt;
  
  
  🗽 This is not the end, but just a beginning
&lt;/h2&gt;

&lt;p&gt;With all this said, we have just scratched the tip of an iceberg[🗻+🧊]. There are a lot of unsaid, and I am well aware. What I want to prove is, JavaScript is such a beautiful language. It is liberal enough that we don’t want to mention the data types of our variables and no yelling at missed semicolons (I know, it is a bad convention but still). This article lives its purpose if this makes you interested in learning more about the language and its nitty-gritty and helps you to admire its beauty 🏖️. &lt;/p&gt;

&lt;p&gt;In upcoming articles, I will help you relive the splendours of JS. I help you understand how a single-threaded blocking/synchronous language runs the entire show on its own! (❓) I promise👍 it will not be the conventional syntax and code snippets explanations, but more will be of how things are done under the hood.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/zhPWlqR2CUQ6s/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/zhPWlqR2CUQ6s/giphy.gif" alt="May the force be with you"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>introduciton</category>
      <category>webdev</category>
      <category>fordummies</category>
    </item>
  </channel>
</rss>
