<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mgbeji Uche </title>
    <description>The latest articles on Forem by Mgbeji Uche  (@mgbejxi).</description>
    <link>https://forem.com/mgbejxi</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%2F1075065%2F98162baa-36d1-491b-a95d-13bc54dd5223.jpg</url>
      <title>Forem: Mgbeji Uche </title>
      <link>https://forem.com/mgbejxi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mgbejxi"/>
    <language>en</language>
    <item>
      <title>Understanding JavaScript Scope: Global and Local</title>
      <dc:creator>Mgbeji Uche </dc:creator>
      <pubDate>Mon, 22 Jan 2024 21:25:16 +0000</pubDate>
      <link>https://forem.com/mgbejxi/understanding-javascript-scope-global-and-local-2jp0</link>
      <guid>https://forem.com/mgbejxi/understanding-javascript-scope-global-and-local-2jp0</guid>
      <description>&lt;p&gt;JavaScript, a versatile and widely-used programming language, employs a concept called "scope" to manage the accessibility of variables within a program. Scope determines where a variable can be accessed and modified. In JavaScript, there are two main types of scope: global scope and local scope. Let's delve into these scopes and explore how they function with relevant examples.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;1. Global Scope:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Global scope refers to the outermost level of a JavaScript program. Variables declared outside any function or block have global scope and are accessible throughout the entire codebase. Global variables can be risky because any part of the program can modify them, potentially leading to unintended consequences.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&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="c1"&gt;// Global scope variable&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;globalVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am global&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;exampleFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Accessing global variable&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;globalVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;exampleFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: "I am global"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;globalVar&lt;/code&gt; is declared outside any function, making it accessible globally. The &lt;code&gt;exampleFunction&lt;/code&gt; can access and print the global variable.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;2. Local Scope:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Local scope, on the other hand, is confined to a specific function or block. Variables declared within a function or block have local scope and are only accessible within that particular context. This helps in encapsulating variables, preventing unintended interference from other parts of the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&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;function&lt;/span&gt; &lt;span class="nf"&gt;exampleFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Local scope variable&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;localVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am local&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Accessing local variable&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;localVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;exampleFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: "I am local"&lt;/span&gt;

&lt;span class="c1"&gt;// Attempting to access localVar outside its scope would result in an error&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;localVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: localVar is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;localVar&lt;/code&gt; is declared within the &lt;code&gt;exampleFunction&lt;/code&gt;, making it accessible only within that function. Attempting to access it outside the function results in a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;3. Lexical Scope (Closure):&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Lexical scope, often associated with closures, involves the ability of a function to access variables from its outer (enclosing) scope, even after that scope has finished executing. This behavior is a fundamental aspect of JavaScript and is a powerful mechanism for managing state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&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;function&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&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;outerVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am outer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Accessing variable from outer scope&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;outerVar&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;innerFunction&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;closureExample&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;closureExample&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: "I am outer"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;innerFunction&lt;/code&gt; is defined inside &lt;code&gt;outerFunction&lt;/code&gt;, forming a closure. When &lt;code&gt;outerFunction&lt;/code&gt; is invoked, it returns &lt;code&gt;innerFunction&lt;/code&gt;, which can still access &lt;code&gt;outerVar&lt;/code&gt; even though &lt;code&gt;outerFunction&lt;/code&gt; has completed execution.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;4. Block Scope (Introduced with ES6):&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Before ECMAScript 6 (ES6), JavaScript only had function scope. With the introduction of the &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; keywords in ES6, block scope was introduced. Variables declared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are limited to the block in which they are defined, such as loops or conditional statements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Block scope variable&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;blockVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am block-scoped&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Accessing block-scoped variable&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blockVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Attempting to access blockVar outside its scope would result in an error&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blockVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: blockVar is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;blockVar&lt;/code&gt; is declared within the &lt;code&gt;if&lt;/code&gt; block, making it accessible only within that block. Attempting to access it outside the block results in a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Conclusion:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Understanding scope is crucial for writing maintainable and error-free JavaScript code. Global scope allows variables to be accessed throughout the entire program, while local scope confines variables to specific functions or blocks. Lexical scope, through closures, enables functions to access variables from their outer scopes. With the introduction of ES6, block scope provides more fine-grained control over variable visibility.&lt;/p&gt;

&lt;p&gt;By leveraging these different types of scope, developers can write more modular and secure code, minimizing the risk of unintended variable modifications and improving overall code quality.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Array Methods in JavaScript</title>
      <dc:creator>Mgbeji Uche </dc:creator>
      <pubDate>Sat, 13 Jan 2024 12:13:20 +0000</pubDate>
      <link>https://forem.com/mgbejxi/arry-methods-in-javascript-385j</link>
      <guid>https://forem.com/mgbejxi/arry-methods-in-javascript-385j</guid>
      <description>&lt;p&gt;An array is a special kind of object which contains lists of elements that store multiple values in one place. &lt;br&gt;
Array Methods are built in functions used for processing or manipulating different types of elements or data values stored in an array. &lt;/p&gt;

&lt;p&gt;Example of an array method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nums = [1, 2, 3, 4, 5];
const doubled = nums.map(value =&amp;gt; value * 2);
console.log(doubled); // [2, 4, 6, 8, 10]

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

&lt;/div&gt;



&lt;p&gt;Now that you have an idea of what an array method is, lets dive deeper and explore the various kinds of array methods available in JavaScript. There are many kinds of array methods in JavaScript and we shall checkout the most commonly used of them all in this tutorial. Also, we shall group them together according to the type of functions they perform for ease of understanding.&lt;/p&gt;

&lt;h3&gt;
  
  
  Array method for evaluating length properties.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;length property&lt;/strong&gt;: this method is used for checking the number or length of elements in an array, when called the arr.length() method returns a number value equal to the number of elements/items in the array. &lt;/p&gt;

&lt;p&gt;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 oddNums = [2, 4, 5, 11, 12];
console.log(oddNums.length); // 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Array Methods for adding or removing elements.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;push()&lt;/strong&gt;: this array method adds one or more elements to the end of an array and returns the new length of the array. The arr.push() method modifies the original array as can be seen below.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let verbs = ["come", "go", "laugh"];
let added = verbs.push("run"); 
console.log(added); // 4
console.log(verbs); // ["come", "go", "laugh", "run"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;pop()&lt;/strong&gt;: this array method removes an element from the end of an array and returns the it, this method modifies the original array.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let nouns = ["house", "cars", "pen", "sun"];
console.log(nouns.pop()); // sun
console.log(nouns); // ["house", "cars", "pen"]

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;unshift()&lt;/strong&gt;: this method adds one or more elements to the beginning of an array and returns its new length. The arr.shift() method modifies the original array.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
let someColors = ["white", "yellow", "purple"];
console.log(someColors.unshift("pink")); // 4 
console.log(someColors); // ["pink", "white", "yellow", "purple"]

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;shift()&lt;/strong&gt;: this array method removes the first element from an array and returns it. Also, this method changes the original array as can be seen below.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let colors = ["red", "blue", "green", "black"];
console.log(colors.shift()); // red
console.log(colors); // ["blue", "green", "black"]

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;splice()&lt;/strong&gt;: this method is used for deleting, inserting, and replacing elements in an array. It accepts two or more arguments.&lt;br&gt;
This syntax arr.splice(start, end) deletes one element starting from index 1 and returns it.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Delete 1 item, starting from index 1
 Let items = ["pen", "book", "phone", "cup", "cap"];
 console.log(items.splice(1,1)); // ["book"]
 console.log(items); // ["pen", "phone", "cup", "cap"]

&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;let removed = items.splice(0, 2); // removes the first two items starting from index 0 and returns them
 console.log(removed); // ["pen", "phone"]
 console.log(items); // ["cup", "cap"]
 console.log(items.length); //2

&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;// insert items
let add = ["pen", "book", "phone", "phone", "peg"];
 console.log(add.splice(2, 0, "pot", "spoon")); 
 console.log(add); // ["pen", "book", "pot", "spoon", "phone", "phone", "peg"]
 console.log(add.length); // 7
 console.log(add.splice(2, 2, "ball", "glasses")); // ["pot", "spoon"]
 console.log(add); //  ["pen", "book", "ball", "glasses", "phone", "phone", "peg"]


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

&lt;/div&gt;



&lt;p&gt;The above examples show the different ways in which new elements can be deleted, inserted or replaced in an array using the splice() method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;slice()&lt;/strong&gt;: The slice() method takes two arguments like this arr.slice([start], [end]) and returns a new array with a copy of all elements from the start index to the end, without including the end index itself.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let pureItems = ["pen", "book", "phone", "cup", "cap"];
console.log( pureItems.slice(1, 3)); // ["book", "phone"]

let itemNums = [1, 2, 3, 4, 5]
console.log(itemNums.slice(-3)); // [3, 4, 5]


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

&lt;/div&gt;



&lt;p&gt;As can be seen from the above example, passing a negative value as argument counts from the ending of the array(from right to left).&lt;/p&gt;

&lt;h3&gt;
  
  
  Array methods for finding elements.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;indexOf()&lt;/strong&gt;: this array method returns the first index of a specified value or -1 if the value is not found in the array.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let str = "Good Morning Uche";
let index = str.indexOf('Uche');
console.log(index); //13
console.log(str.indexOf('he'); //15

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

&lt;/div&gt;



&lt;p&gt;Please note that spaces are counted as well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;includes()&lt;/strong&gt;: this array method checks if an element is included in an array and returns a Boolean value of true if found, otherwise it returns false.&lt;/p&gt;

&lt;p&gt;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 numArray = [1, 2, 3, 4, 5];
console.log(numArray.includes(5)); // true
console.log(numArray.includes(8)); // false

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;find()&lt;/strong&gt;: this method searches for an element with a matching value in an array and returns the value if found, otherwise, it returns undefined.&lt;/p&gt;

&lt;p&gt;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 fewNums = [1, 2, 3, 4]
console.log(fewNums.find( num =&amp;gt; num == 3 )); // 3

console.log(fewNums.find( num =&amp;gt; num == 20 )); // undefined

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;findIndex()&lt;/strong&gt;: this method looks for an element and returns the index where the element was found instead of the element itself. The value of -1 is returned if nothing is found. &lt;/p&gt;

&lt;p&gt;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 nums = [9, 5, 4, 6, 8, 9, 12];

let numsIndex = nums.findIndex( num =&amp;gt; num === 4);
console.log(numsIndex); // 2

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  High-order methods
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;map()&lt;/strong&gt;: this array method transforms array elements by performing an assigned function on each element of the array then returns a new modified array.&lt;/p&gt;

&lt;p&gt;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 numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(value =&amp;gt; value * 2);
console.log(doubled); // [2, 4, 6, 8, 10]

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;filter()&lt;/strong&gt;: this method returns an array of all matching elements in an array. In order words, it filters off all other elements which did not  pass the test and returns a new array of the filtered elements.&lt;/p&gt;

&lt;p&gt;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 array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
console.log(array.filter(num =&amp;gt; num &amp;gt; 3 &amp;amp;&amp;amp; num &amp;lt;= 8)); // [4, 5, 6, 7, 8]

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;reduce()&lt;/strong&gt;: this array method reduces elements of an array to a single value and returns the value. It accepts the accumulator and current value as arguments.&lt;/p&gt;

&lt;p&gt;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 gameLevels = [1, 2, 3, 4];
console.log(gameLevels.reduce((acc, curr) =&amp;gt; acc + curr)); // 10

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;some()&lt;/strong&gt;: this array method checks if at least one element in an array meets a requirement. This method returns a Boolean value of true when found and false when not found. &lt;/p&gt;

&lt;p&gt;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 prices = [30, 210, 180, 20];
console.log(prices.some(price =&amp;gt; price &amp;gt; 500)); // false
console.log(prices.some(price =&amp;gt; price &amp;gt; 100)); // true

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;sort()&lt;/strong&gt;: this method sorts elements of an array and returns a modified array of sorted elements in an ascending or descending order. In order to return a correctly sorted array, an arrow function like this should be passed to it: (a,b) =&amp;gt; a – b;// for sorting in ascending order, or this: (a,b) =&amp;gt; b – a; for sorting in descending order. This way it returns a correctly sorted array. &lt;br&gt;
The reason for this twist is because the elements of an array is first converted to strings for comparison and then sorted out using lexicographic ordering, this makes a number like 3, 4, 5 etc, greater than 12, 22, 10, because the first numbers of each digit are compared and returned. As a result of this, the output differs from the expected output hence, we employ the services of the above arrow function to correct this. &lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let evenNums = [2, 8, 16, 4, 24, 6];
console.log(evenNums.sort()); // [16, 2, 24, 4, 6, 8]
console.log(evenNums.sort((a,b) =&amp;gt; a - b)); // [2, 4, 6, 8, 16, 24]
console.log(evenNums.sort((a,b) =&amp;gt; b - a)); // [24, 16, 8, 6, 4, 2]

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;every()&lt;/strong&gt;: method checks if every element in an array passes a test or satisfies a given requirement.&lt;/p&gt;

&lt;p&gt;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 ages = [3, 10, 14, 16];
console.log(ages.every(age =&amp;gt; age &amp;gt; 2)); // true
console.log(ages.every(age =&amp;gt; age &amp;gt; 40)); // false

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;forEach()&lt;/strong&gt;: this method loops through array elements and performs any assigned functions on each item or element in the array.&lt;/p&gt;

&lt;p&gt;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 primeNums = [1, 2, 3, 5];
primeNums.forEach((item) =&amp;gt; console.log(item)); // prints each array items to the console

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Concat Method
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;concat()&lt;/strong&gt;: this method takes any number of arrays as arguments and returns a new array with all the elements in the previous arrays, then merges them all together.  The syntax is like this: &lt;br&gt;
arr.concat(arg1, arg2, arg3...)&lt;/p&gt;

&lt;p&gt;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 alpha = ['a', 'b', 'c'];
const numeric = [1, 2, 3];
const alphaNumeric = alpha.concat(numeric);
console.log(alphaNumeric); // ['a', 'b', 'c', 1, 2, 3]

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Reverse Method
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;reverse()&lt;/strong&gt;: This method reverses the order of elements in an array.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add = ["pen", "book", "phone", "phone", "peg"];
 console.log(add.reverse()); // ["peg", "phone", "phone", "book", "pen"]

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Methods for flattening arrays
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;flat()&lt;/strong&gt;: this array method flattens an array recursively up to a specified depth.&lt;/p&gt;

&lt;p&gt;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 freshNums = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]]
console.log(freshNums.flat(1)); // [1, 2, 3, 4, [5, 6, [7, 8, [9, 10]]]]
console.log(freshNums.flat(2)); // [1, 2, 3, 4, 5, 6, [7, 8, [9, 10]]]
console.log(freshNums.flat(4)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;flatMap()&lt;/strong&gt;: this method executes a mapping function on every element in an array and creates a new flat array without changing the original array. &lt;/p&gt;

&lt;p&gt;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 arrays = [[1], [2], [3], [4], [5]];
const a = arrays.flatMap(arr =&amp;gt; arr * 5);
const b = arrays.flat().map(arr =&amp;gt; arr * 10);
console.log(a); // [5, 10, 15, 20, 25]
console.log(b); // [10, 20, 30, 40, 50]

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Array to String Method
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;join()&lt;/strong&gt;: this method concatenate all elements of an array into a string separated by a separator and returns an array as a string. This method does not change the original array.&lt;/p&gt;

&lt;p&gt;Examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let letters = ["a", "b", "c"];
console.log(letters.join(',')); // a,b,c
console.log(letters.join('-')); // a-b-c
console.log(letters.join('')); // abc

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;toString()&lt;/strong&gt;: this method converts each element of the array into a string and concatenates them together, separated by a comma.&lt;/p&gt;

&lt;p&gt;Examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let favNums = [1, 2, 3, 4, 5, 8, 13, 22 ];
let convertedNums = favNums.toString();
console.log(convertedNums); // 1,2,3,4,5,8,13,22
console.log(typeof(convertedNums)); // string

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;concatenation(+)&lt;/strong&gt;: this method converts an array to a string through a method called implicit coercion. In order words, JavaScript implicitly coerces the array to a string using the concatenation operator +.&lt;/p&gt;

&lt;p&gt;Examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let got = "" + favNums
console.log(got); // 1,2,3,4,5,8,13,22
console.log(typeof(got)); // string

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;string()&lt;/strong&gt;: an array can also be explicitly coerced to string using a String constructor.&lt;/p&gt;

&lt;p&gt;Examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let bigNums = [58, 96, 68, 87];
let convertedBigNums = String(bigNums);
console.log(convertedBigNums); // 58,96,68,87
console.log(typeof(convertedBigNums)); // string

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  In Conclusion:
&lt;/h3&gt;

&lt;p&gt;Now that you have learnt about the various methods of accessing, modifying and manipulating elements stored in arrays, you now understand why array methods are so important in JavaScript. It is equally important to understand and consider best practices when choosing an appropriate method for solving a particular problem while working with arrays.&lt;br&gt;
Hope you enjoyed reading this far, thanks for reading.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Unlocking the World of JavaScript Basics: Embracing Variables and Data Types</title>
      <dc:creator>Mgbeji Uche </dc:creator>
      <pubDate>Mon, 04 Dec 2023 09:06:13 +0000</pubDate>
      <link>https://forem.com/mgbejxi/unlocking-the-world-of-javascript-basics-embracing-variables-and-data-types-4p2n</link>
      <guid>https://forem.com/mgbejxi/unlocking-the-world-of-javascript-basics-embracing-variables-and-data-types-4p2n</guid>
      <description>&lt;p&gt;If you're venturing into the captivating realm of web development, understanding the fundamentals of JavaScript is your gateway to success. In this journey, two essential building blocks demand your attention: variables and data types.&lt;/p&gt;

&lt;p&gt;Let's explore further to see what they really are. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Variables:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aRO25juQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j6dpknwf7c11314o0oga.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aRO25juQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j6dpknwf7c11314o0oga.jpg" alt="Image description" width="720" height="710"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine variables as containers holding valuable information. These dynamic entities allow you to store and manipulate data within your scripts. Think of them as labeled boxes – you assign a name (or label) to a box, drop something inside, and later retrieve or modify its contents. This way, it makes for easy retrieval and use in future references. &lt;/p&gt;

&lt;p&gt;Example: &lt;/p&gt;

&lt;p&gt;let message = "Hello, World!";&lt;/p&gt;

&lt;p&gt;Here, 'message' is our box, and it contains the greeting. The 'let' keyword declares the variable, while '=' assigns the value.&lt;/p&gt;

&lt;p&gt;Variables can be declared using any of this syntax; let, var or const. &lt;br&gt;
If a variable is declared using  'const', it simply means that it cannot be changed or redeclared later. However, if a variable is declared using either the 'let' or 'var' keyword, it can be reassigned later if one wants to. &lt;/p&gt;

&lt;p&gt;Variables bring flexibility to your code, enabling dynamic interactions and adaptability. Whether it's a number, a string of text, or a complex object, variables empower your script with the ability to evolve and respond to changing conditions.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Data Types: The DNA of Your Code&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jphFjygC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7pvsbluegmybslj6a1ua.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jphFjygC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7pvsbluegmybslj6a1ua.jpg" alt="Image description" width="720" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JavaScript embraces various data types, each serving a unique purpose. Let's explore some fundamental ones:&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Strings:&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Strings represent text and are enclosed in single or double quotes.&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;let&lt;/span&gt; &lt;span class="nx"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, 'Hello, World!' is a string, encapsulated within the variable 'greeting'.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Numbers:&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Numeric values, both integers and decimals, fall under the number data type.&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;let&lt;/span&gt; &lt;span class="nx"&gt;quantity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.14&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this snippet, 'quantity' holds an integer, while 'price' embraces a decimal number.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Booleans:&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Booleans express truth or falsehood. They are the binary backbone of decision-making in programming.&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;let&lt;/span&gt; &lt;span class="nx"&gt;isProgrammingFun&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isJavaScriptHard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this instance, 'isProgrammingFun' holds a true value, while 'isJavaScriptHard' is false.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Arrays:&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Arrays group multiple values into a single, ordered structure.&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;let&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;apple&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;banana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orange&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;'fruits' here is an array containing strings of different fruits.&lt;/p&gt;

&lt;p&gt;Understanding these data types equips you with the power to manipulate information effectively, making your scripts robust and versatile.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why It Matters:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Embracing variables and data types is not just a technical necessity; it's the art of crafting dynamic, responsive code. By mastering these basics, you lay the foundation for intricate projects, setting the stage for a seamless coding experience. &lt;/p&gt;

&lt;p&gt;So, whether you're a coding novice or a seasoned developer, remember: Variables and data types are not mere lines of code; they are the essence that breathes life into your JavaScript creations. Dive in, experiment, and let your code tell stories of innovation and creativity. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;In Conclusion:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;All great things start with small steps and JavaScript is no exception. Every little piece of information gained while learning this unique language will go a long way in helping you become better at programming. Never give up! &lt;br&gt;
Happy coding!&lt;/p&gt;

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