<?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: Johan H. Guzman Gil</title>
    <description>The latest articles on DEV Community by Johan H. Guzman Gil (@johandev19).</description>
    <link>https://dev.to/johandev19</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%2F269099%2F7dd54016-a3ea-4cbb-b2f3-c943a2206da1.jpg</url>
      <title>DEV Community: Johan H. Guzman Gil</title>
      <link>https://dev.to/johandev19</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/johandev19"/>
    <language>en</language>
    <item>
      <title>Node.js - REPL (Read Eval Print Loop)</title>
      <dc:creator>Johan H. Guzman Gil</dc:creator>
      <pubDate>Tue, 07 Apr 2020 20:07:29 +0000</pubDate>
      <link>https://dev.to/johandev19/node-js-repl-read-eval-print-loop-4597</link>
      <guid>https://dev.to/johandev19/node-js-repl-read-eval-print-loop-4597</guid>
      <description>&lt;p&gt;Before start with this post, I would like to explain what is &lt;strong&gt;node.js.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Node.js is not entirely true a JavaScript that runs on the backend, it is a &lt;strong&gt;wrapper&lt;/strong&gt; of a &lt;strong&gt;VM&lt;/strong&gt; who is running the &lt;strong&gt;JavaScript V8 engine&lt;/strong&gt;,&lt;br&gt;
knowing that, node.js come with a lot API that we can use in conjunction with JavaScript but I will leave that topic for another post.&lt;/p&gt;

&lt;p&gt;Now let talk about &lt;strong&gt;REPL&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REPL&lt;/strong&gt; it is a node API that let us to write or test script in the terminal.&lt;br&gt;
To enter in to the REPL mode we need to type &lt;code&gt;node&lt;/code&gt; in the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;node
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that are going to see an empty &lt;code&gt;&amp;gt;&lt;/code&gt; its mean that we are into the &lt;strong&gt;REPL&lt;/strong&gt; mode, now we can have fun and write some code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hello REPL'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; // &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; hello REPL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;REPL let us write an inline code by default, if we want to write a multiple line of code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;function &lt;/span&gt;printHelloAliens&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then left &lt;code&gt;{&lt;/code&gt; open and press &lt;code&gt;enter&lt;/code&gt; you will see the next line with &lt;code&gt;...&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;function &lt;/span&gt;printHelloAliens&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it means that we are ready to continue with the function content, add a &lt;code&gt;console.log('hello aliens')&lt;/code&gt; and press &lt;code&gt;enter&lt;/code&gt; and you will see the next line with &lt;code&gt;...&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;function &lt;/span&gt;printHelloAliens&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;...console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hello aliens'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to finish with the function content with need to add &lt;code&gt;}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;function &lt;/span&gt;printHelloAliens&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;...console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hello aliens'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;...&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;after press &lt;code&gt;enter&lt;/code&gt; the terminal will print &lt;code&gt;undefined&lt;/code&gt;;&lt;br&gt;
if you want to test your function just invoke the function, in my case is &lt;code&gt;printHelloAliens()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;function &lt;/span&gt;printHelloAliens&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;...console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hello aliens'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;...&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;undefined
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;printHelloAliens&lt;span class="o"&gt;()&lt;/span&gt; // &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; hello aliens
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;they are a lot of options, if you want to know it just type &lt;code&gt;.help&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;.help
&lt;span class="go"&gt;.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;for this tutorial we're only going to see the &lt;code&gt;.editor&lt;/code&gt; command.&lt;br&gt;
The &lt;code&gt;.editor&lt;/code&gt; command allow you to write a multiple line of code more easy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;.editor
&lt;span class="go"&gt;// Entering editor mode (^D to finish, ^C to cancel)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now with can write a multiple line of code with just pressing &lt;code&gt;enter&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;// Entering editor mode (^D to finish, ^C to cancel)
function printMyName() {
 console.log('Johan Guzman')
}
&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For close the editor mode type &lt;code&gt;CTRL + d&lt;/code&gt; and the terminal will print &lt;code&gt;undefined&lt;/code&gt;;&lt;/p&gt;

&lt;p&gt;if you want to test your function just invoke the function, in my case is &lt;br&gt;
&lt;code&gt;printMyName()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;// Entering editor mode (^D to finish, ^C to cancel)
function printMyName() {
 console.log('Johan Guzman')
}
&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;undefined
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;printMyName&lt;span class="o"&gt;()&lt;/span&gt; // &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; Johan Guzman
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;REPL&lt;/strong&gt; is very helpful to test a node or JavaScript code without open the editor or write a little program.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>testing</category>
    </item>
    <item>
      <title>JavaScript - Global variable</title>
      <dc:creator>Johan H. Guzman Gil</dc:creator>
      <pubDate>Sat, 04 Apr 2020 03:31:31 +0000</pubDate>
      <link>https://dev.to/johandev19/javascript-global-variable-53ah</link>
      <guid>https://dev.to/johandev19/javascript-global-variable-53ah</guid>
      <description>&lt;p&gt;Hi, in this episode I will talk about Global variable in JavaScript.&lt;/p&gt;

&lt;p&gt;Let's write the next code:&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;function&lt;/span&gt; &lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&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;helloWorld&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&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;helloWorld&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello my aliens&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; hello my aliens&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;helloWorld&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; error: Uncaught ReferenceError: helloWorld is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The program is going to print the variable &lt;strong&gt;helloWorld&lt;/strong&gt; inside the function but outside the function it will break the code&lt;/p&gt;

&lt;p&gt;If we want to have access to the variable &lt;strong&gt;hellowWorld&lt;/strong&gt; outside the function &lt;strong&gt;print&lt;/strong&gt; we need to remove the reserved word &lt;strong&gt;var&lt;/strong&gt; and our code will look like this:&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;function&lt;/span&gt; &lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;helloWorld&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&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;helloWorld&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello my aliens&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; hello my aliens&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;helloWorld&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; hello my aliens&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we will see two identical messages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But the big question is, WHY THIS HAPPENS?&lt;/strong&gt;&lt;br&gt;
When JavaScript are compiling, and he found the word &lt;strong&gt;helloWorld&lt;/strong&gt; without the reserved word &lt;strong&gt;var&lt;/strong&gt;, it will put the variable &lt;strong&gt;helloWorld&lt;/strong&gt; on top of the file because it thinks that you are going to use it below.&lt;/p&gt;

&lt;p&gt;Your compiled code will look like this:&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;helloWorld&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;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;helloWorld&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&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;helloWorld&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello my aliens&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; hello my aliens&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;helloWorld&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; hello my aliens&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method of using the global variable will work but is not recommended. There are multiple ways to have a global variable, but the complexity of the code will determine which one is the correct one. Some example of these are &lt;code&gt;use a session store&lt;/code&gt;, &lt;code&gt;create a function that return the value&lt;/code&gt;, &lt;code&gt;enums&lt;/code&gt;, or use &lt;code&gt;a dictionary&lt;/code&gt;&lt;/p&gt;

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