<?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>Forem: Saswat Rath</title>
    <description>The latest articles on Forem by Saswat Rath (@thebooleanboy).</description>
    <link>https://forem.com/thebooleanboy</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%2F1065939%2Fd66ee8b9-c972-4f74-a894-e7abadf9dac2.jpg</url>
      <title>Forem: Saswat Rath</title>
      <link>https://forem.com/thebooleanboy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/thebooleanboy"/>
    <language>en</language>
    <item>
      <title>Coercion and Falsy values in Javascript.</title>
      <dc:creator>Saswat Rath</dc:creator>
      <pubDate>Thu, 20 Apr 2023 04:46:18 +0000</pubDate>
      <link>https://forem.com/thebooleanboy/coercion-and-falsy-values-in-javascript-iid</link>
      <guid>https://forem.com/thebooleanboy/coercion-and-falsy-values-in-javascript-iid</guid>
      <description>&lt;p&gt;In JavaScript, coercion is the process of converting a value from one type to another. This can be done implicitly or explicitly, and it can lead to some unexpected behavior if you're not careful. One area where this is particularly true is with falsy values.&lt;/p&gt;

&lt;p&gt;Falsy values are values that are considered false when evaluated in a boolean context. The following values are considered falsy in JavaScript:&lt;/p&gt;

&lt;p&gt;&amp;gt; false&lt;br&gt;
&amp;gt; null&lt;br&gt;
&amp;gt; undefined&lt;br&gt;
&amp;gt; 0&lt;br&gt;
&amp;gt; NaN&lt;br&gt;
&amp;gt; '' (an empty string)&lt;/p&gt;

&lt;p&gt;When you try to use one of these values in a boolean expression, JavaScript will automatically coerce it to a boolean value. For example, if you have the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (0) {
  // This code will not execute
} else {
  // This code will execute
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the value 0 is considered falsy, so the condition in the if statement evaluates to false. JavaScript coerces the 0 to the boolean value false, so the else block will execute instead.&lt;/p&gt;

&lt;p&gt;While coercion can be useful in some cases, it can also lead to unexpected behavior if you're not careful. One common issue is when comparing values using the double equals (==) operator. This operator performs type coercion if the types of the two values being compared are different.&lt;/p&gt;

&lt;p&gt;For example, consider the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if ('0' == 0) {
  // This code will execute
} else {
  // This code will not execute
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the string '0' is coerced to the number 0, so the condition in the if statement evaluates to true. This can be confusing if you're not expecting it, so it's generally a good idea to use the triple equals (===) operator instead, which does not perform type coercion.&lt;/p&gt;

&lt;p&gt;Another common issue with coercion is when dealing with null and undefined values. Because both of these values are considered falsy, they can sometimes be used interchangeably. However, they have different meanings in JavaScript. Undefined means that a variable has been declared but has not been assigned a value, while null means that a variable has been explicitly assigned the value null.&lt;/p&gt;

&lt;p&gt;If you're not careful, you can accidentally coerce null or undefined to a different type and end up with unexpected behavior. For example, consider the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x;
console.log(x + 1); // This will log NaN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the variable x has been declared but not assigned a value, so its value is undefined. When you try to add 1 to undefined, JavaScript coerces the undefined value to the NaN (not a number) value, which is not what you probably intended.&lt;/p&gt;

&lt;p&gt;I will conclude with the verdict that, coercion can be a useful feature in JavaScript, but it can also lead to unexpected behavior if you're not careful. Make sure you understand how JavaScript coerces values in different contexts and be aware of the potential pitfalls, especially when dealing with falsy values like null and undefined.&lt;/p&gt;

</description>
      <category>coercion</category>
      <category>falsy</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Global and Execution Context of Javascript.</title>
      <dc:creator>Saswat Rath</dc:creator>
      <pubDate>Mon, 17 Apr 2023 17:32:07 +0000</pubDate>
      <link>https://forem.com/thebooleanboy/global-and-execution-context-of-javascript-3oc9</link>
      <guid>https://forem.com/thebooleanboy/global-and-execution-context-of-javascript-3oc9</guid>
      <description>&lt;p&gt;JavaScript is a popular programming language that is commonly used to create interactive web applications. Understanding the global and execution context of JavaScript is essential to writing efficient and effective code. In this blog, we will explain what these contexts are and how they work.&lt;/p&gt;

&lt;p&gt;The Global Context:&lt;br&gt;
The global context is the outermost context in JavaScript. It is the default context that is created when a script is loaded into a web page. In the global context, all code that is not contained within a function is executed. This includes variable declarations and function definitions. Variables declared in the global context are accessible from any other context in the script.Below is a code example to understand the global context of Javascript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;globalVar = "Saswat";
myFunction();
function myFunction() {
  console.log(globalVar);
}


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

&lt;/div&gt;



&lt;p&gt;The Execution Context:&lt;br&gt;
The execution context is created whenever a function is called. Each function call creates a new execution context that has its own set of variables and functions. When a function is called, the execution context is added to the top of the call stack. The call stack keeps track of the order in which functions are called, and when a function returns, its execution context is removed from the stack. &lt;/p&gt;

&lt;p&gt;The Variable Environment:&lt;br&gt;
The variable environment is a collection of all the variables and functions that are accessible in the current context. The variable environment includes function arguments, local variables, and any variables declared in outer scopes that are accessible in the current context.&lt;/p&gt;

&lt;p&gt;The Lexical Environment:&lt;br&gt;
The lexical environment is similar to the variable environment, but it also includes information about the outer environment. The outer environment is the context in which the current context was created. This is important because it allows functions to access variables that are declared in outer scopes. The lexical environment is used to resolve variable names when they are referenced in a function.&lt;/p&gt;

&lt;p&gt;In conclusion, the global and execution context of JavaScript are essential to writing effective and efficient code. The global context is the outermost context in JavaScript, and it is where all code that is not contained within a function is executed. The execution context is created whenever a function is called, and it includes a variable environment and a lexical environment. The variable environment is a collection of all the variables and functions that are accessible in the current context, while the lexical environment includes information about the outer environment. Understanding these concepts is crucial for writing high-quality JavaScript code.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
