<?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: Michael Orji</title>
    <description>The latest articles on DEV Community by Michael Orji (@simplymichael).</description>
    <link>https://dev.to/simplymichael</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%2F37597%2Ff2d07851-68fc-4abe-a244-0876e6730a24.png</url>
      <title>DEV Community: Michael Orji</title>
      <link>https://dev.to/simplymichael</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/simplymichael"/>
    <language>en</language>
    <item>
      <title>Variable declaration and scoping rules</title>
      <dc:creator>Michael Orji</dc:creator>
      <pubDate>Mon, 06 Nov 2017 21:02:27 +0000</pubDate>
      <link>https://dev.to/simplymichael/variable-declaration-and-scoping-rules-c0k</link>
      <guid>https://dev.to/simplymichael/variable-declaration-and-scoping-rules-c0k</guid>
      <description>&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;In programming, the scope of a variable determines where that variable can be used within the program, and also what functions and objects have access to that variable.&lt;/p&gt;

&lt;p&gt;Usually, a variable can have either local or global scope. A variable declared within a block of code has local scope, and is only accessible by other code within the same block. Once the block within which it is declared is exited, the variable goes out of scope. A global variable, on the other hand, is accessible from anywhere within the currently executing script (or program), and usually lasts the entire lifetime of the program.&lt;/p&gt;

&lt;p&gt;In this write up, we wish to examine the various ways in which variables can be declared in JavaScript, and we shall see that the scope of a variable in JavaScript is affected by where and how it is declared.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Declaring variables&lt;/h2&gt;

&lt;p&gt;There are three keywords available for declaring variables in JavaScript. They are the var, let, and const keywords. JavaScript, being a dynamic and loosely typed language, also allows you to use variables without pre-declaring them, but this is not considered good practice and is highly discouraged.&lt;/p&gt;

&lt;p&gt;To declare a variable, we use any of the above listed keywords, followed by the variable name as follows:&lt;/p&gt;

&lt;p&gt;var a;&lt;/p&gt;

&lt;p&gt;let b;&lt;/p&gt;

&lt;p&gt;const c = 5;&lt;/p&gt;

&lt;p&gt;When you declare a variable using const, you must initialize it with a value -as we have done above - which cannot afterwards be changed. When declaring variables with var and let, we can optionally initialize the variables at the point of declaration:&lt;/p&gt;

&lt;p&gt;var a = 2;&lt;/p&gt;

&lt;p&gt;var b = 3;&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Declaring variables with the var keyword&lt;/h3&gt;

&lt;p&gt;When you declare a variable using the var keyword, the scope is as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;If the variable is declared outside of any functions, the variable is available in the global scope.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If the variable is declared within a function, the variable is available from its point of declaration until the end of the function definition.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike what you might be used to from other languages, variables declared with the var keyword have no block scope. In concrete terms, this means that if you declare a variable using var within a for, or any non-function block, the variable's scope extends beyond the block to the end of the block's parent scope. On the other hand, if you declare a variable inside a function with the var keyword, the variable is only available within the function definition, and cannot be accessed outside of the function. We, therefore, say that variables declared with var are function-scoped.&lt;/p&gt;

&lt;p&gt;Let's see some examples to help clarify what we are saying.&lt;/p&gt;

&lt;pre&gt;function varScope() {
   var a = 2;
   console.log(a); // outputs  2
}

console.log(a); // ReferenceError, a is not accessible outside the function.
&lt;/pre&gt;

&lt;p&gt;Let's see another example.&lt;/p&gt;

&lt;pre&gt;function varScope() {
   var a = 2;
   if(true) {
      var a =  "Jamie"; //change the value of a inside the "if" block
      console.log(a); //prints "Jamie"
   }
   console.log(a); //prints "Jamie": outside the "if" block, a still maintains the updated value 
   //a being function scoped, the (re-) declaration inside the if statement overwrote the previous value of a
   //when we assigned it a new value inside the conditional statement
}
console.log(a); // ReferenceError, again, a is not accessible outside the function.
&lt;/pre&gt;

&lt;p&gt;Finally, let's look at this one.&lt;/p&gt;

&lt;pre&gt;function forScope() {
   for(var i = 0; i &amp;lt; 5; i++) {
      console.log(i); //prints the values 0 through 4;
   }
   console.log(i); //prints 5;
}
&lt;/pre&gt;

&lt;p&gt;What just happened? Inside the for header, we declare and initialize the i variable. Then inside the loop, we iterate from 0 while the value of i is less than 5, bumping i on each iteration. When the value of i equals 5, the condition i &amp;lt; 5 evaluates to false, terminating our loop. However, since i is declared using var, its scope extends from its point of declaration to the end of the function. Hence, even after the loop, we can access the up-to-date value of i, which, in this case is 5.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Declaring variables with the let keyword&lt;/h3&gt;

&lt;p&gt;variables declared using the let keyword have three important characteristics.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They are &lt;strong&gt;block scoped&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;They are &lt;strong&gt;not&lt;/strong&gt; accessible before they are assigned&lt;/li&gt;
&lt;li&gt;They &lt;strong&gt;cannnot&lt;/strong&gt; be re-declared within the same scope&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see what this means using some examples.&lt;/p&gt;

&lt;pre&gt;function  letScope() {
   let a = 5;

   if  (true) {
      let a = "Jamie";  // using let creates a new a variable inside the "if" block
      console.log(a); //  prints "Jamie"
   }

   console.log(a); // 5,  outside the if block, the outer a shines through
}
console.log(a); // ReferenceError, a is not accessible outside the function.
&lt;/pre&gt;

&lt;p&gt;Here's what happens in this function.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Inside the function, we create an a variable using let, this variable exists throughout the scope of this function.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Inside the if block, we create another let -declared a variable. Being block scoped, we just created a new a variable.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;This variable is totally different from, and independent of, the outer a variable.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;This variable is only available within the if block, and not accessible outside this block.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In addition, you can't re-declare a let variable:&lt;/p&gt;

&lt;pre&gt;let a = 2;
let a = 3// SyntaxError, cannot re-declare the a variable
&lt;/pre&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Declaring variables with the const keyword&lt;/h3&gt;

&lt;p&gt;Variables declared with the const keyword share all the characteristics of variables declared using the let keyword, plus one important distinguishing characteristic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;They can't be reassigned&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;const a = 2;
a = 3 // Error, reassignment is not allowed
&lt;/pre&gt;

&lt;pre&gt;const a = 2;
const a = 3 // Error, re-declaration is not allowed
&lt;/pre&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Variable Mutability&lt;/h2&gt;

&lt;p&gt;Regardless of how you declare a variable, using any of the keywords we have discussed, the variable is mutable. Mutability must not be confused with reassignment. This difference is highlighted when working with arrays or objects. An example or two will clarify what this means.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Object example:&lt;/h3&gt;

&lt;pre&gt;const person = {
  name: 'Michael'
};
person.name = 'Jamie' // OK! person variable mutated, not completely re-assigned
console.log(person.name); // "Jamie"
person = "Newton" // Error, re-assignment is not allowed with const declared variables
&lt;/pre&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Array example:&lt;/h3&gt;

&lt;pre&gt;const person = [];
person[0] = 'Michael'; // OK! person variable only mutated, not completely re-assigned
console.log(person[0]) // "Michael"
person = "Newton" // Error, re-assignment is not allowed with const declared variables
&lt;/pre&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Accessing a variable before its declaration&lt;/h2&gt;

&lt;p&gt;In the section on declaring variables with let, we noted that one of the characteristics of let declared variables is that they are not accessible before they are declared. What does this mean? Let's see.&lt;/p&gt;

&lt;p&gt;Consider this piece of code:&lt;/p&gt;

&lt;pre&gt;console.log(a); // undefined, but no error raised
var a = 2;
&lt;/pre&gt;

&lt;p&gt;In the above snippet, we attempt to read the value of the a variable before its declaration. Instead of getting an error, we get undefined. Why is that? The answer is that var declared variables are moved to the top of the scope at execution.&lt;/p&gt;

&lt;p&gt;At runtime, this code is interpreted as:&lt;/p&gt;

&lt;pre&gt;var a;
console.log(a); // undefined: a is declared, but hasn't been assigned a value, hence no errors raised
a = 2;
&lt;/pre&gt;

&lt;p&gt;This phenomenon is what is referred to as hoisting.&lt;/p&gt;

&lt;p&gt;If we try to do a similar thing with a variable declared using let or const, we will get a reference error thrown.&lt;/p&gt;

&lt;pre&gt;console.log(a); // ReferenceError
let a = 2;
&lt;/pre&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Concluding thoughts&lt;/h2&gt;

&lt;p&gt;Mastering scope in JavaScript can seem tricky, and may take some time to getting used to.  But with practice the various ways of declaring variables in JavaScript and how these affect scope become second nature.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Further reading&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let" rel="external"&gt;MDN let&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/mbeaudru/modern-js-cheatsheet#variable-declaration-var-const-let" rel="external"&gt;Modern Javascript Cheatsheet&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>scope</category>
      <category>variables</category>
    </item>
    <item>
      <title>Introduction to JavaScript Functions</title>
      <dc:creator>Michael Orji</dc:creator>
      <pubDate>Thu, 26 Oct 2017 11:38:01 +0000</pubDate>
      <link>https://dev.to/simplymichael/introduction-to-javascript-functions-dbe</link>
      <guid>https://dev.to/simplymichael/introduction-to-javascript-functions-dbe</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;From its use as a front-end scripting language for web development, Javascript has evolved, and is now being deployed across several stacks and application environments, notable among which is its use in backend development in Node.JS. As a consequence, more and more developers and teams are leveraging on it to build applications of all types from the simplest to the most complex.&lt;/p&gt;

&lt;p&gt;Mastery of JavaScript is therefore considered among the most important in the skillset of a (web) developer. As reported by Computer Weekly, a recent survey by software analysis firm - Cast - found that a greater percentage of developers (56%) believe that JavaScript (alongside Java) is among  "the most important languages to master in the next five years". This is in comparison with C++ (38%), Python (35%) and SQL (30%).&lt;/p&gt;

&lt;p&gt;Among the most powerful, yet sometimes underplayed, features of JavaScript is its use for functions. JavaScript is primarily a functional language, and functions occupy a fundamental place in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Function
&lt;/h2&gt;

&lt;p&gt;A function, in simple terms, is a (named) group of code that can be invoked. In other words, a function is a way to group together some code, give this group a name, and later invoke the code using the given name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why functions
&lt;/h2&gt;

&lt;p&gt;The use of functions provide several benefits to developers. Some of these are highlighted below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Encapsulation and code reuse&lt;/strong&gt; - Functions promote code reuse by encapsulating repetitive tasks or routines, potentially saving you more typing time. This also helps to avoid potential (typing) errors that could arise from having to type the group of code the function encapsulates whenever you need to execute the action(s) they perform.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better code organization&lt;/strong&gt; - Functions help with organizing and managing code.  Once you write the function and test that it works, you can save it and call it whenever you need it. Also, if you ever need to make a change to the code you only need to do it in one place, rather than looking for every instance where you typed the code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concise and self-documenting code&lt;/strong&gt; - They make our code shorter, and allows for cleaner syntax. Appropriately named functions make code more self-documenting.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to define and use functions
&lt;/h2&gt;

&lt;p&gt;Supposing you have a sequence of code statements that compute the sum of two numbers, and print the result to the browser, you could do 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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, several lines of code later, you change either or both of the values stored in the a and b variables, and again set sum to equal the addition of a and b, and finally print the sum with document.write(sum);. Depending on how many times you have to execute the group of statements, it can become a nightmare, especially if the code in question is doing something non-trivial. This presents a good case of where functions can come in handy. Let's see how.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining a function
&lt;/h3&gt;

&lt;p&gt;To convert our above code to a function: we follow these steps:&lt;/p&gt;

&lt;p&gt;-&lt;strong&gt;collect our code sequence together&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-&lt;strong&gt;Wrap the set of code between curly braces&lt;/strong&gt;, creating a block of 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="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&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;By enclosing our code within the opening and closing braces {}, we have effectively turned them into a single unit of execution (known as a code block), that can either be executed as one.&lt;br&gt;
-&lt;strong&gt;Lastly, give the unit a name&lt;/strong&gt;, preceded by the function keyword, and followed by a set of parentheses ():&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;sumAndPrint&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&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;With that final step, we have successfully defined our function, ready to be summoned whenever we need to sum,  and print the result of, the numbers 5 and 7.&lt;br&gt;
  The block of code in between the opening and closing curly brace is called the function body.&lt;/p&gt;
&lt;h3&gt;
  
  
  Calling (invoking) our function
&lt;/h3&gt;

&lt;p&gt;So, we have finished defining our function, by encapsulating it within a block of code, and giving it a fancy name. But how do we use this function?&lt;br&gt;
To use our newly defined function, we simply call it by its name, followed by the pair of parentheses (excluding the function keyword, and the function body):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sumAndPrint();&lt;/code&gt; //prints 12&lt;/p&gt;

&lt;p&gt;That is how simple it is to define and use a function in JavaScript.&lt;br&gt;
  Another term for calling a function is to "invoke" the function.&lt;/p&gt;
&lt;h2&gt;
  
  
  Improving upon our function
&lt;/h2&gt;

&lt;p&gt;The function we have defined above is an improvement over always having to manually type the block of code it encloses everytime we need to perform the action of adding two numbers and output the result. Nonetheless, it suffers from a few drawbacks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Firstly, the function , in its current form, can only sum the numbers 5 and 7, not any other pair of numbers. This means that if we need to sum any other set of numbers, we will have to write a different function for every new pair of numbers. This is not particularly desirable since it will be breaking the DRY (Don't Repeat Yourself) principle, and taking us back to the reason for writing functions in the first place: avoiding unnecessary or avoidable repetition.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Another problem our function - as it currently stands - suffers from is that it is doing too many (actually just two) things at a time:&lt;br&gt;
It caluclates the sum of the given numbers.&lt;br&gt;
It prints the calculated sum.&lt;br&gt;
A good rule of thumb when creating functions is that a function should only do one thing; after all, functions are supposed to help us build modularity into our code. But our function, in its present state, breaks that rule.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A third problem with our function is that there's no way currently for us to get hold of the resulting sum of the numbers after the function has finished executing. This is because upon exiting the function, the a, b, and sum variables go out of scope, and can no longer be accessed outside of the function. So, if we needed to do something with the computed value after the function completes execution, we couldn't.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see how we can improve upon our function to take care of each of the issues we have raised.&lt;/p&gt;
&lt;h2&gt;
  
  
  Function parameters and return values
&lt;/h2&gt;

&lt;p&gt;The first issue - being able to sum only the numbers 5 and 7, thus breaking the DRY principle - can be fixed by what are known as function parameters.&lt;br&gt;
Function parameters are what we give a function to enable it carry out its task.&lt;/p&gt;

&lt;p&gt;As an analogy, imagine sending a child on an errand, the errand could be to go help deliver a message to Mr. A. In this instance, you are not expecting the child to give anything tangible to Mr. A, just merely to convey a (verbal) message. That's the case with our function as it currently stands. You don't give it anything external to perform its task. Instead, the function contains within itself everything it needs to accomplish its job.&lt;/p&gt;

&lt;p&gt;Now imagine sending the child on an errand to go help purchase some items from the local store. In this case, you would need to give the child some means of exchange, for example money, in order for the child to be able to get you the requested items. Otherwise, there'd be no way for them to get you the items you desire. In programming speak, we call such enablers (in the case of the child on errand, money) of actions "parameters". Parameters are passed to functions by including them between the opening and closing parentheses in the function definition. Multiple parameters are separated using commas.&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;exampleFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;param2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;param3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;//function code goes here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to our &lt;strong&gt;sumAndPrint&lt;/strong&gt; function from earlier on, what it requires to carry out its task of summation are two numbers - currently 5 and 7, respectively stored in the a and b variables. However, what we'd like to do is for the function to be able to sum any arbitrary pair of numbers, not just 5 and 7.&lt;br&gt;
To achieve this functionality, we need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Move the declaration of the a and b variables out of the function body.
Pass them to the function as parameters, between the opening and closing parentheses.&lt;/li&gt;
&lt;li&gt;When invoking the function, pass actual arguments to it, which will get assigned to the parameters in the order in which they were passed.
Here's what our code looks like after applying these changes:
&lt;/li&gt;
&lt;/ul&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;sumAndPrint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&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;The a and b in the function's parameter list now act as placeholders for the actual values that we will pass in when we invoke the function. With this brief change in the function definition, our function can accept and sum any two numbers we pass to it.&lt;br&gt;
To use our updated function, we call it as usual, but this time, we pass in the numbers we wish to sum as arguments:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sumAndPrint(3, 6);&lt;/code&gt; //9.&lt;/p&gt;

&lt;p&gt;Internally, the number 3 will be assigned to the a variable, and the number 6 to the b variable.&lt;br&gt;
This solves the first issue, now let's move on to fixing the second issue we raised earlier.&lt;/p&gt;

&lt;p&gt;In this update, we want to make our function do only one thing. This can be achieved by moving the code that does the printing of the calculated sum outside of the function. When we do that, however, we'd no longer be be able to print out the result of the computation carried out by our function, since as noted earlier, there's no way to access the sum variable outside of the function.&lt;br&gt;
This brings us to the third issue noted above: how to make the result from the function accessible from outside of the function, so that we can work with it, for example to use it in another calculation or output it to the browser, as we are doing here.&lt;br&gt;
  It is also a good idea to rename our function - after moving the printing code out - so that it is clear it is no longer doing two things - summation and printing - but one (summation). A good name for the updated function will be "sum".&lt;/p&gt;

&lt;p&gt;To get access to the result of the function, and hence fix the third issue identified above, we need to get our function to give us its resultant value.&lt;br&gt;
Again, using the analogy of the child on errand, after getting the items from the local store, the child has to "return" the items bought to the sender. In the same way, after performing its calculation (or whatever task it is written to perform), a function can be instructed to return the results of its calculation to the caller. This is done by means of a "return" statement. The "return" statement is made up of the keyword return followed by the value we want the function to return to the calling code.&lt;/p&gt;

&lt;p&gt;The return keyword basically says to our function, "when you are done executing, send this value back to your caller".&lt;br&gt;
Applying these changes - moving out the code that prints to the browser, renaming our function, and adding the return statement - to our function, it should now 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;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sum&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;In fact, we can eliminate the intermediate sum variable altogether, and just directly return the result of the expression a + b:&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;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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 can store the result of calling this function in another variable and use it however we wish:&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see how the use of functions has made our code more succinct, cleaner, and more self-documenting, traits that are highly desirable as programmers. The function name "sum" speaks for itself wherever it is used in our code or by client code.&lt;/p&gt;

&lt;h2&gt;
  
  
  More on JavaScript Parameters
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Default Parameters
&lt;/h3&gt;

&lt;p&gt;The most recent version of the JavaScript specification (ES6), supports the concept of optional parameters when defining functions. In simple terms, optional parameters mean that our function parameters are assigned default values when the function is created, such that if the user does not pass any corresponding arguments when they call the function, the default values are used.&lt;br&gt;
For example, we could define our sum function as follows:&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;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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;This would allow the function to be invoked while passing: two, one, or no arguments at all.&lt;br&gt;
&lt;code&gt;sum(3, 6);&lt;/code&gt;Here, 3 is assigned to the a variable, and 6 to b.&lt;br&gt;
&lt;code&gt;sum(3);&lt;/code&gt; In this instance, 3 will be assigned to the a variable, and since we didn't supply a second argument, the default value of 7 will be assigned to the b variable, giving a result of 10.&lt;br&gt;
&lt;code&gt;sum();&lt;/code&gt; In this call, we pass no arguments at all, so the default values of 5 and 7 are used, resulting in a value of 12.&lt;/p&gt;
&lt;h3&gt;
  
  
  Dealing with earlier versions of JavaScript
&lt;/h3&gt;

&lt;p&gt;Versions of JavaScript prior to ES6 do not directly support the concept of default parameters; but we can achieve the same functionality by modifying our code slightly, while making use of a feature of JavaScript functions:&lt;br&gt;
JavaScript allows us to call functions without passing any arguments, even if the function definition includes parameters.&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;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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;What's happening here? We are using the typeof operator to determine the data type of the arguments (or none) passed to the function when it is invoked.&lt;br&gt;
For each parameter, if no corresponding argument is passed, the typeof will return the string "undefined" for that parameter. For example, if we call the function without supplying an argument or passing null for the a parameter, typeof a will return "undefined", so the test typeof a !== "number" will evaluate to true, and a will be set to the default value of 5. Else, the test will fail and we use the supplied number.&lt;br&gt;
  We could have tested for if &lt;code&gt;typeof a === "undefined"&lt;/code&gt;, but by testing against "number", we ensure that even if the user passed our function a string such as "hello", the default numeric values will be used for the variables. This way, our function is made more robust by being able to detect, and work around, unreasonable input.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary and concluding remarks
&lt;/h2&gt;

&lt;p&gt;To recap, here are some of the highlights of what we did in this write-up.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We started out by stating that functions are one of the most important concepts to master in JavaScript programming.&lt;/li&gt;
&lt;li&gt;We then looked at some of the benefits that the use of JavaScript functions offer us in terms of code organization and reuse.&lt;/li&gt;
&lt;li&gt;We proceeded to see how to define and use functions. Specifically, we saw that a function is made up of 4 parts:

&lt;ul&gt;
&lt;li&gt;The function keyword&lt;/li&gt;
&lt;li&gt;A function name&lt;/li&gt;
&lt;li&gt;A comma-separated list of (optional) parameters&lt;/li&gt;
&lt;li&gt;The function body - between curly braces { }&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;We saw that a function can return a value to its caller using the return statement.&lt;/li&gt;
&lt;li&gt;We learnt that a function is invoked by using its name followed by the parentheses ( ). Arguments to the function, if any, are passed inside the parentheses.&lt;/li&gt;
&lt;li&gt;We learnt that it is considered good practice to create functions that perform a single, specialized task.&lt;/li&gt;
&lt;li&gt;Finally, we learnt that it is possible to define functions with default parameter values in ES6 compliant browsers. We provided an alternate implementation for older JavaScript versions by a clever use of the &lt;code&gt;typeof&lt;/code&gt; operator with conditional statements.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In conclusion, mastering JavaScript functions is an important part of becoming a seasoned JavaScript developer. This write up is but an introduction to functions in JavaScript. I encourage you to explore JavaScript functions in greater detail. Your ability to stand out as a JavaScript developer depends on it. Your future self will thank you for the effort.&lt;/p&gt;

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