<?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: Asad Khan</title>
    <description>The latest articles on DEV Community by Asad Khan (@bagfaceasadkhan).</description>
    <link>https://dev.to/bagfaceasadkhan</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%2F919214%2Fedb6bb43-f2e7-4763-a602-c82c638e5d47.jpg</url>
      <title>DEV Community: Asad Khan</title>
      <link>https://dev.to/bagfaceasadkhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bagfaceasadkhan"/>
    <language>en</language>
    <item>
      <title>Type Conversion and Coercion in Javascript💫.</title>
      <dc:creator>Asad Khan</dc:creator>
      <pubDate>Thu, 01 Sep 2022 15:44:16 +0000</pubDate>
      <link>https://dev.to/bagfaceasadkhan/type-conversion-and-coercion-in-javascript-12al</link>
      <guid>https://dev.to/bagfaceasadkhan/type-conversion-and-coercion-in-javascript-12al</guid>
      <description>&lt;p&gt;Conversion of one datatype to another in any programming language is a very crucial thing to know.&lt;br&gt;
There are two ways in Javascript by which one datatype is converted into another.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Type conversion (Explicit-Manual).👦🏼&lt;/li&gt;
&lt;li&gt;Type coercion (Implicit-Automatic).💻&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Type Conversion 👦🏼
&lt;/h2&gt;

&lt;p&gt;Type conversion is a method to convert variable of one datatype into another datatype explicitly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = "1";
console.log(typeof a)//string
a = Number(a);
console.log(typeof b);//number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example we explicitly converted the "a" variable of datatype string into datatype of number using Number().&lt;/p&gt;

&lt;p&gt;Since value of the "a" variable in the first line was a legit number i.e 1 it got converted.&lt;/p&gt;

&lt;p&gt;But this would not work if "a" had a value of lets say "bagfaceasadkhan".&lt;/p&gt;

&lt;p&gt;Using Number method on this would give use NaN i.e Not a Number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = "bagfaceasadkhan";
console.log(typeof a)//string
a = Number(a);//NaN
console.log(typeof b);//udefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly we can convert various datatypes into other datatypes.&lt;/p&gt;

&lt;p&gt;There are 7 primary datatypes in Javascript.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Number&lt;/li&gt;
&lt;li&gt;String&lt;/li&gt;
&lt;li&gt;Boolean&lt;/li&gt;
&lt;li&gt;Undefined&lt;/li&gt;
&lt;li&gt;Null&lt;/li&gt;
&lt;li&gt;BigInt&lt;/li&gt;
&lt;li&gt;Symbol&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Major conversions happen between Number, String, Boolean, Undefined and some times Null.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Things to always remember.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When a number is converted to boolean all the numbers will be converted to true except for 0.&lt;/li&gt;
&lt;li&gt;When a boolean is converted to number true will be converted to 1 and false to 0.&lt;/li&gt;
&lt;li&gt;When a string whose value is not actually a number i.e if it is not "1" or "10" and is something like "bagfaceasadkhan" then upon converting it to number will give output as NaN i.e Not a Number.&lt;/li&gt;
&lt;li&gt;When a string is converted to boolean all the strings are converted to true except for an "" (empty string).&lt;/li&gt;
&lt;li&gt;Undefined when converted to number will give NaN, when converted to string will give "undefined", when converted to boolean will give false.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Tip for conversion to boolean.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All the falsy values i.e  "", undefined, null and 0 when converted to boolean will give false rest all will give true.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Some code examples based on simple conversions.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = true;//Boolean
let numA = Number(a)//1
let stringA = String(a)//"true"
let b = false;//Boolean
let numB = Number(b)//0
let stringB = String(b)//"false"
let c = 1;//Number
let cString = String(c)//"1"
let boolC = Boolean(c)//"true"
let d = 0;//Number
let dString = String(d)//"0"
let boolD = Boolean(d)//false
let e = undefined;//undefined
let eString = String(e)//"undefined"
let eNum = Number(e)//NaN
let eBool = Boolean(e)//false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Type Coercion 💻
&lt;/h2&gt;

&lt;p&gt;Type Coercion is a mechanism in which a variable of one datatype is automatically converted to another datatype under the hood.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 1;
let b = "2";
console.log(a+b)//"12"//One automatically got converted to a string.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example a variable with a datatype of number and b variable with a datatype of string was involved in an addition operation where a variable got coerced i.e automatically converted to datatype of string and then just like string concatination "1"+"2" we got the output as "12".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(1+true+3-1);//4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code example boolean value true got coerced to number datatype hence the operation became 1+1+3-1 = 4.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Things to keep in mind while dealing with type coercion.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;(+) If any one of the operand of + operator is a string and other operand of any other datatype it will try to convert the other operand to string and then will concatinate the two operands.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets take an example program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(1+3+"3"+4+"5")//"3345"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above given example the execution will go as follows.&lt;/p&gt;

&lt;p&gt;All the arithematic operators executes from left to right.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;1 + 2 = 3 // both the operands are number&lt;/li&gt;
&lt;li&gt;3 + "3" = "33"// here since one operand is a string and another number the number is coerced to string and then operands are concatinated giving the output as "33".&lt;/li&gt;
&lt;li&gt;"33" + 4 = "334"//here again one operand is number and another is string.&lt;/li&gt;
&lt;li&gt;"334" + "5" = "3345"// here since both are string it will be a simple string concatination giving the final output as "3345".&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;(-) If anyone of the operand of - operator is string and another is of any datatype it will coerce the string operand into number.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets take an example program.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;10 - 2 = 8 // both the operands are number so simple subtraction.&lt;/li&gt;
&lt;li&gt;8 - "3" = 5// one operand is string which is coerced to number and then simple subtraction.&lt;/li&gt;
&lt;li&gt;5 - 2 = 3// simple subtraction.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If one of the two operands is a string the non-string operand will be coerced to string if the operator is +.&lt;/li&gt;
&lt;li&gt;If one of the two operands or both are a string the string operand will be coerced to number if the operator is any arithmetic opertor other than +.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Some questions to try.&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;console.log(3+4+"5"+6+7);
&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;console.log(3*4*"5");
&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;console.log("3"+"5"-2+"3")
&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;console.log(true+1+"33");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>html</category>
    </item>
  </channel>
</rss>
