<?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: mimansha-swarup</title>
    <description>The latest articles on DEV Community by mimansha-swarup (@mimanshaswarup).</description>
    <link>https://dev.to/mimanshaswarup</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%2F523342%2Fc692c3dc-50aa-48dd-b366-76d1b5f1c2ed.jpg</url>
      <title>DEV Community: mimansha-swarup</title>
      <link>https://dev.to/mimanshaswarup</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mimanshaswarup"/>
    <language>en</language>
    <item>
      <title>JavaScript Hoisting. What happens behind JavaScript ?</title>
      <dc:creator>mimansha-swarup</dc:creator>
      <pubDate>Thu, 26 Aug 2021 10:53:15 +0000</pubDate>
      <link>https://dev.to/mimanshaswarup/javascript-hoisting-what-happens-behind-javascript-148a</link>
      <guid>https://dev.to/mimanshaswarup/javascript-hoisting-what-happens-behind-javascript-148a</guid>
      <description>&lt;p&gt;If  you are looking for hoisting in JavaScript you might come across this definition.&lt;em&gt;"Hoisting is JavaScript's default behavior of moving declarations to the top".&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  But what actually happens behind JavaScript ?
&lt;/h2&gt;

&lt;p&gt;Hoisting reefer to the process where compiler allocate memory for variable and function before executing code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:-&lt;/strong&gt; Declarations that are made using var are initialized with a default value of undefined. Declarations made using let and const are not initialized as part of hoisting.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Lets see how JavaScript work Behind?
&lt;/h2&gt;

&lt;p&gt;When a JavaScript Code is executed &lt;strong&gt;&lt;em&gt;Execution Context&lt;/em&gt;&lt;/strong&gt; is created.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Execution Context?
&lt;/h3&gt;

&lt;p&gt;Execution Context is an abstract concept that hold information about environment within which code is being executed &lt;br&gt;
When we run JavaScript Code Execution Context is Created in two phase &lt;strong&gt;Memory Creation&lt;/strong&gt; and &lt;strong&gt;Code Execution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tw4ly7Yi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0sohf56259v1bzd0vtu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tw4ly7Yi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0sohf56259v1bzd0vtu.png" alt="Execution Context"&gt;&lt;/a&gt;&lt;br&gt;
if we break down Execution Context  we have 2 component&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Memory&lt;/li&gt;
&lt;li&gt;Code&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Memory Part is also called as &lt;em&gt;Variable Environment&lt;/em&gt;.&lt;br&gt;
Code Part is known as &lt;em&gt;Thread of Execution&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Consider a code block&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello JavaScript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output of this code block will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2
Hello JavaScript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we will run this code Global Execution Context will be created and memory will be allocated to each variable and function after that only code will be allocated.&lt;br&gt;
when variable n is encountered JavaScript reserve a memory space for &lt;em&gt;n&lt;/em&gt;. Then JavaScript will store a special value &lt;em&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/em&gt;&lt;br&gt;
and for function it will store entire function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Gs87UPd0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mpypupvc5dy5xzz8ay90.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Gs87UPd0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mpypupvc5dy5xzz8ay90.png" alt="Memory allocation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is what happens under the hood.&lt;br&gt;
Now consider&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello JavaScript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we are logging 'n' and calling function before declaring , we should be getting error but JavaScript works differently like we saw memory is allocated before execution &lt;br&gt;
that's why  output will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;undefined
Hello JavaScript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so this is how it works you can access these variable and method even before actually initialization in your code &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>html</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Learn Value, Types and Operators in JavaScript</title>
      <dc:creator>mimansha-swarup</dc:creator>
      <pubDate>Fri, 04 Dec 2020 21:04:36 +0000</pubDate>
      <link>https://dev.to/mimanshaswarup/value-types-and-operators-in-javascript-b39</link>
      <guid>https://dev.to/mimanshaswarup/value-types-and-operators-in-javascript-b39</guid>
      <description>&lt;h1&gt;
  
  
  So in this series of blog I will be blogging chapters of Eloquent JavaScript
&lt;/h1&gt;

&lt;p&gt;Let's see what is data and how machine interpret data.We store our data on Electronic devices for example we stored a image on a computer now this image is been stored in a long sequence of bits&lt;br&gt;
and bits have two value 0 and 1, true and false,high volt and low volt etc&lt;/p&gt;
&lt;h2&gt;
  
  
  Table of Content
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Numbers&lt;/li&gt;
&lt;li&gt;Special Numbers&lt;/li&gt;
&lt;li&gt;String&lt;/li&gt;
&lt;li&gt;Boolean Value&lt;/li&gt;
&lt;li&gt;Logical Operator&lt;/li&gt;
&lt;li&gt;Ternary Operator&lt;/li&gt;
&lt;li&gt;Empty Value&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Numbers
&lt;/h3&gt;

&lt;p&gt;name itself is self explanatory that this means numerical values for example &lt;code&gt;13&lt;/code&gt; is a number.&lt;br&gt;
JavaScript use 64 bit to store number value, the largest number that can be stored is 2 power 64 i.e 2^64 which is around 18000000000000000000 in short 18 followed by 18 zeros but there is a catch this is theoretical but the problem is js also need to store -ve values also so for that JS use one bit to store - or +if number is -ve it will have 1 and even if value is +ve 0 will be used  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;FunFact : In real JS can store upto 9000000000000000 which is 9 followed by 15 zeros&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Special Number
&lt;/h3&gt;

&lt;p&gt;There are 3 special numbers in JS who are termed as number but are nothing like number. These are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Infinity&lt;/li&gt;
&lt;li&gt;- Infinity&lt;/li&gt;
&lt;li&gt;NaN
Infinity Name itself says that this value refers to infinite value and - Infinity means negative of infinite&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;FunFact: If we  subtract Infinity from Infinity we will get NaN &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bkBMMk9d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lgmixc84ah0hzy6t0mjo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bkBMMk9d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lgmixc84ah0hzy6t0mjo.png" alt="Infinity-Infinity"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;NaN means not a number this you will get NaN as result when a calculation has done but result is not meaningful for eg: 0/0&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xYS-Berm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cfr4adkrvtwx7wvmcpyk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xYS-Berm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cfr4adkrvtwx7wvmcpyk.png" alt="0/0"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Strings
&lt;/h3&gt;

&lt;p&gt;so string are basically any text whether it's a letter ,word or sentence  wrapped with single quotes or double quotes back tick any value warped with above characters are considered as String eg:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; This is a String !&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is a String too.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="s2"&gt;`This is String with Back Tick`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are some special charterers for string one is newline character( \n )&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;example of&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;new line character&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="cm"&gt;/*example of
new line character*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;on string we can't perform any arithmetic operation but anyway we van add strings this is called as &lt;strong&gt;string concatenation&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;"app" + "le"&lt;/code&gt; this will result &lt;code&gt;"apple"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Back tick string are called &lt;strong&gt;template literal&lt;/strong&gt;&lt;br&gt;
using Back tick we can print the value of variable inside string this can be done using ${} in between curly braces you need to write variable name eg:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="s2"&gt;`The area of square is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;variableName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Boolean
&lt;/h3&gt;

&lt;p&gt;boolean are the true false value. If the expression is correct then it will give &lt;code&gt;True&lt;/code&gt; and if wrong &lt;code&gt;False&lt;/code&gt; value is given by the expression on evaluation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;True&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;FunFact: Js try to accept every calculation/command you give even if its odd and try to return a value for eg: if you add string with a number &lt;code&gt;'3'+1&lt;/code&gt; o/p will be &lt;code&gt;31&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Logical Operators (and,or,not)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;and(&amp;amp;&amp;amp;) : if both the value are true it will give true if one or more value is false it will return false&lt;/li&gt;
&lt;li&gt;or(||) :  if one or more value is true it will return true if both the value are false it will return false&lt;/li&gt;
&lt;li&gt;not(!) :  if value is true it will return false if value is false it will return true
&amp;gt; Note: || has lowest Priority after that &amp;amp;&amp;amp; and after that comparison operator(==,&amp;lt;,&amp;gt;,&amp;lt;=,&amp;gt;=,!=) have third lowest priority and then the rest
example:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;1 + 1 == 2 &amp;amp;&amp;amp; 10 * 10 &amp;gt; 50&lt;/code&gt; first arithmetic operators  will be evaluated&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: in arithmetic *,/,% operators have highest priority then +,-&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;10*10&lt;/code&gt; will be evaluated &lt;/p&gt;

&lt;p&gt;&lt;code&gt;1 + 1 == 2 &amp;amp;&amp;amp; 100 &amp;gt; 50&lt;/code&gt; now &lt;code&gt;1+1&lt;/code&gt; will be evaluated&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2== 2 &amp;amp;&amp;amp; 100&amp;gt; 50&lt;/code&gt;  next comparison operators will be     evaluated &lt;code&gt;2==2&lt;/code&gt; and &lt;code&gt;100&amp;gt;50&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;True &amp;amp;&amp;amp; True&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;True&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Ternary Operator
&lt;/h3&gt;

&lt;p&gt;ternary operators are kind of shortcut of Conditional statement or one liner of if else Syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Condn&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;st1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;st2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if the condition is evaluated as true statement 1 will be executed and if it is evaluated as false statement 2 will be executed&lt;/p&gt;

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

&lt;p&gt;these are another two special values null and undefined which  indicate absence of meaningful value&lt;/p&gt;

&lt;h3&gt;
  
  
  Automatic Type Conversion
&lt;/h3&gt;

&lt;p&gt;js automatically tries to convert type of variable on operation and it tries to accept all kind of operations example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fv0pm4jS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cj3qyc8sv1l4aw367bha.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fv0pm4jS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cj3qyc8sv1l4aw367bha.png" alt="js operations"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and in this process JS tries to do operation on odd values like it's adding sting to a number and giving output as string so we will not be getting expected type of value this is called as &lt;strong&gt;type coercion&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// name&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// hello&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the || operator will return the left value if it can be converted to true and will return the right value otherwise. &lt;br&gt;
That's why at first we are getting &lt;em&gt;name&lt;/em&gt; and in second line output is &lt;em&gt;hello&lt;/em&gt;&lt;/p&gt;

</description>
      <category>teamtanayejschallenge</category>
      <category>javascript</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Making Node CLI(Command Line Interface) quiz app</title>
      <dc:creator>mimansha-swarup</dc:creator>
      <pubDate>Mon, 30 Nov 2020 21:20:08 +0000</pubDate>
      <link>https://dev.to/mimanshaswarup/making-first-node-cli-command-line-interface-quiz-app-25h</link>
      <guid>https://dev.to/mimanshaswarup/making-first-node-cli-command-line-interface-quiz-app-25h</guid>
      <description>&lt;h2&gt;
  
  
  Let's Make a Quiz App which you can share with our family and Friends
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fiudfwnpeu5b8ylv82jom.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fiudfwnpeu5b8ylv82jom.png" alt="Preview"&gt;&lt;/a&gt;&lt;br&gt;
So to make this quiz app we will use &lt;a href="https://repl.it" rel="noopener noreferrer"&gt;Repl.it&lt;/a&gt;, you can also use VS Code or any other code editor.&lt;/p&gt;
&lt;h3&gt;
  
  
  Setting up the enviorment
&lt;/h3&gt;

&lt;p&gt;We will use two node modules&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/readline-sync" rel="noopener noreferrer"&gt;readline-sync&lt;/a&gt;: It can take input in various manner from user&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/chalk" rel="noopener noreferrer"&gt;chalk&lt;/a&gt;: it is an expressive api which adds colors and styling&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So if you are using repl you can directly use &lt;em&gt;require&lt;/em&gt; function&lt;br&gt;
to include these modules example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const rs =require('readline-sync');
const chalk = require('chalk');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are on VS Code&lt;br&gt;
open the project folder on terminal and use  command&lt;br&gt;
&lt;code&gt;npm init -y&lt;/code&gt;&lt;br&gt;
this mean to initialize package manager and &lt;em&gt;-y&lt;/em&gt; means yes &lt;br&gt;
so after this just run this command to get above mentioned modules&lt;br&gt;
&lt;code&gt;npm install --save readline-sync chalk&lt;/code&gt;&lt;br&gt;
now make a JavaScript file and write the require statement mentioned above&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: In repl you don't need to manually install modules&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Figuring out quiz app
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;So we want to make a quiz app which will take input for asked question and check whether the answer is correct or not 
-For every correct answer we will increment marks by 2 and for incorrect answer we will reduce by 1&lt;/li&gt;
&lt;li&gt;Also we will have to see if user beat the High score &lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Planing a bit more precise
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;we will greet the user and ask for the user name&lt;/li&gt;
&lt;li&gt;We need a global variable to keep account of score&lt;/li&gt;
&lt;li&gt;We need a function to evaluate whether user answer is correct or not&lt;/li&gt;
&lt;li&gt;We need a array of questions and answers which can be done by using array of objects with key vale question and answer&lt;/li&gt;
&lt;li&gt;We need a for loop to iterate this array
-At last need to check whether User Defeated High score or not&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Coding Part
&lt;/h3&gt;

&lt;p&gt;we have already imported required module and now we have to ask user name and greet as well as create a global variable, ok so it will be like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// importing
const rs =require('readline-sync');
const chalk = require('chalk');

console.log(chalk.green.bold("Welcome to The Game!!!\n"));

// asking name
var playerName = rs.question("HEy Whats ur Name : ");

//greeting user
console.log(chalk.yellow(`Welcome ${playerName}\n`));


//global variable to keep account of score
var score = 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;rs.question() helps us to take user input from console read this for &lt;a href="https://www.npmjs.com/package/readline-sync" rel="noopener noreferrer"&gt; more info&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;chalk.bold means make the string bold while printing it and chalk.yellow means the printed string should be of color yellow read this for &lt;a href="https://www.npmjs.com/package/chalk" rel="noopener noreferrer"&gt; more info&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now we will implement the function which will check user answer is correct or not if answer is correct we will increment the score by 2 and print correct in green color, and if it's not correct then we will print wrong in red color and print the correct answer after conditional statement we will just print some '-' for styling and the updated score of user&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// a function to check whether user answer is correct or not
function gamePlay(questions , correctAnswer){

    var answer  = rs.question(chalk.cyanBright
(questions));
    if(answer == correctAnswer){
        console.log(chalk.green('\tcorrect'));    
        score = score+2;
    }
    else{
        console.log(chalk.red('\twrong'));    
        console.log(chalk.greenBright("Correct Answer is: "+correctAnswer));
        score = score-1;

    }
        console.log(chalk.yellowBright('Your score is  :' + score));    
        console.log(chalk.blue('\n----------------\n'));    

} 

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

&lt;/div&gt;



&lt;p&gt;now we will create array of objects with key question and answer&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;qusArray = [
    {
    qus:"What is my name : ",
    ans:"mimansha"
},
{
    qus:"are you reading this : ",
    ans:"yes"
},
 {
    qus:"which year covid widely spread : ",
    ans:"2020"
}

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

&lt;/div&gt;



&lt;p&gt;Now we will use for loop and iterate through every object and while iterating we will be calling the function ,let's see how it is done and printing final score  after for loop cause when this loop will end all qus has been asked and user can't answer any more question&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//calling function
for(let i =0 ; i&amp;lt;qusArray.length;i++){
    gamePlay(qusArray[i].qus,qusArray[i].ans);

}
console.log('Final score is  :' + score);    

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

&lt;/div&gt;



&lt;p&gt;This might look bit confusing let me explain&lt;br&gt;
&lt;code&gt;gamePlay(qusArray[i].qus,qusArray[i].ans);&lt;/code&gt;&lt;br&gt;
see function gameplay has 2 parameter &lt;em&gt;questions&lt;/em&gt; and &lt;em&gt;correctAnswer&lt;/em&gt; so we are iterating through array by &lt;em&gt;qusarray[i]&lt;/em&gt;&lt;br&gt;
if i =0  first object will be accessed now we want the string stored with the key &lt;em&gt;qus&lt;/em&gt; example:&lt;br&gt;
&lt;code&gt;console.log(qusArray[0].qus)&lt;/code&gt;&lt;br&gt;
output will be &lt;em&gt;What is my name&lt;/em&gt;&lt;br&gt;
in same way we are accessing the ans string.&lt;/p&gt;

&lt;p&gt;Now we will make Dummy High Score array and printing the high score value also we are checking highest score of our dummy data and storing it inside a variable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//array of highscore
highScore = [
    {
        username: "Naruto",
        point: 2
    },
    {
        username: "MEE6",
        point: 1
    },
    {
        username: "random",
        point: 0
    }
];

//displaying highscore
console.log(chalk.bgYellow(" High Score "));

console.table(highScore);

// getting high score
var max = highScore[0].point;
for (let i = 1; i &amp;lt; highScore.length; ++i) {
  if (highScore[i].point &amp;gt; max) {
    max = highScore[i].point;
  }
}

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

&lt;/div&gt;



&lt;p&gt;This is The Last part of quiz app , We have to check if user beat the high score or not and print statement according to that ,let's do it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
//checking if user beat the hihg score
if(score&amp;gt;max){
    console.log(chalk.inverse.bold("\n Congrats u beat high score \n"));
}
else{
    console.log(chalk.inverse.bold("\n Better Luck Next Time \n"));

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

&lt;/div&gt;



&lt;p&gt;so final code will be something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const rs =require('readline-sync');
const chalk = require('chalk');

console.log(chalk.green.bold("Welcome to The Game!!!\n"));
// naae input
var playerName = rs.question("HEy Whats ur Name : ");
//greeting user
console.log(chalk.yellow(`Welcome ${playerName}\n`));
//global varibale to keep account of socre
var score = 0;
// a function to check whether user answer is correct or not
function gamePlay(questions , correctAnswer){
    var answer  = rs.question(chalk.cyanBright
(questions));
    if(answer == correctAnswer){
        console.log(chalk.green('\tcorrect'));    
        score = score+2;
    }
    else{
        console.log(chalk.red('\twrong'));    
        console.log(chalk.greenBright("Correct Answer is: "+correctAnswer));
        score = score-1;
    }
        console.log(chalk.yellowBright('Your score is  :' + score));    
        console.log(chalk.blue('\n----------------\n'));    
} 
//creating qus answer  objects
var firstQus = {
    qus:"What is my name : ",
    ans:"mimansha"
};
var secondQus = {
    qus:"are you reading this : ",
    ans:"yes"
};
var thirdQus = {
    qus:"which year covid widely spread : ",
    ans:"2020"
};
// list of all qus answer
qusArray = [firstQus,secondQus,thirdQus];
//calling function
for(let i =0 ; i&amp;lt;qusArray.length;i++){
    gamePlay(qusArray[i].qus,qusArray[i].ans);

}
console.log('Final score is  :' + score);    
//array of highscore
highScore = [
    {
        username: "Naruto",
        point: 2
    },
    {
        username: "MEE6",
        point: 1
    },
    {
        username: "random",
        point: 0
    }
];
//displaying highscore
console.log(chalk.bgYellow(" High Score "));
console.table(highScore);
// getting high score
var max = highScore[0].point;
for (let i = 1; i &amp;lt; highScore.length; ++i) {
  if (highScore[i].point &amp;gt; max) {
    max = highScore[i].point;
  }
}
//checking if user beat the hihg score
if(score&amp;gt;max){
    console.log(chalk.inverse.bold("\n Congrats u beat high score \n"));
}
else{
    console.log(chalk.inverse.bold("\n Better Luck Next Time \n"));

}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  When you put all the code and run it output will be like this
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2lq9k4ouig5hsbztfob3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2lq9k4ouig5hsbztfob3.png" alt="Output Window"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  You can share your Repl with your friends with some changes on link exapmle: &lt;em&gt;&lt;a href="https://repl.it/@MimanshaSwarup/cli-app#index.js" rel="noopener noreferrer"&gt;https://repl.it/@MimanshaSwarup/cli-app#index.js&lt;/a&gt;&lt;/em&gt;
&lt;/h4&gt;
&lt;h4&gt;
  
  
  by removing file name and add this at &lt;em&gt;?outputonly=1&amp;amp;lite=true&lt;/em&gt; ending of link example: &lt;a href="https://repl.it/@MimanshaSwarup/cli-app?outputonly=1&amp;amp;lite=true" rel="noopener noreferrer"&gt;https://repl.it/@MimanshaSwarup/cli-app?outputonly=1&amp;amp;lite=true&lt;/a&gt;
&lt;/h4&gt;
&lt;/blockquote&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>replit</category>
    </item>
  </channel>
</rss>
