<?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: Pedro Marungo</title>
    <description>The latest articles on DEV Community by Pedro Marungo (@pedromarungo).</description>
    <link>https://dev.to/pedromarungo</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%2F674520%2F4e1b73b1-64ab-45c3-aca2-1f775a3e3380.jpg</url>
      <title>DEV Community: Pedro Marungo</title>
      <link>https://dev.to/pedromarungo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pedromarungo"/>
    <language>en</language>
    <item>
      <title>React testing with Jest</title>
      <dc:creator>Pedro Marungo</dc:creator>
      <pubDate>Tue, 16 May 2023 06:29:44 +0000</pubDate>
      <link>https://dev.to/pedromarungo/react-testing-with-jest-491k</link>
      <guid>https://dev.to/pedromarungo/react-testing-with-jest-491k</guid>
      <description>&lt;h2&gt;
  
  
  Running test with &lt;strong&gt;Jest&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There is a number of javascript test frameworks. To test vanilla javascript applications, a popular choice is &lt;strong&gt;Mocha&lt;/strong&gt;. Jest is another popular choice for react developers. Jest was developed by facebook like react and is an open source project. Jest comes preinstalled when you generate a react project using the&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create-react-app //command line tool.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you generated a react project using the command above all you have to do to run the test is use the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm test 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Several things will occur when npm test is executed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Jest is executed:&lt;/strong&gt; create-react-app by default uses Jest as a testing framework. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Test files are located:&lt;/strong&gt; Jest will automatically search for test files in the project directory. It searches for files with the .test.js or .spec.js extension in the src directory and its subdirectories,this is a common convention used to indicate that the file contains test code for the corresponding JavaScript file..&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Test are executed:&lt;/strong&gt; Jest will execute all test files &amp;amp; runs the test cases defined within those files. It will provide a test runner that displays results in the console, showing which test pass &amp;amp; which have fail. It will also provide failing assertions or errors during the test execution. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Test watch mode:&lt;/strong&gt; Jest runs &lt;strong&gt;watch mode&lt;/strong&gt; by default when you run &lt;strong&gt;npm test&lt;/strong&gt;, but it is optional. Watch mode means that Jest will keep running &amp;amp; watches for changes in your test file or source code. When it detects any modifications it re-runs the affected tests automatically. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Code coverage:&lt;/strong&gt; By default create react app is configured to generate code coverage reports. When npm test is executed, Jest will also collect information about how much your code is covered by the tests. It will generate a coverage report that provides insights into areas of your code that are tested &amp;amp; those that are not tested. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The npm test command will also execute the test script in the package.json file in your project directory. &lt;strong&gt;create-react-app&lt;/strong&gt; sets up a default configuration where the test script is associated with the command to run the test suite using Jest. This configuration includes a predefined command that is associated with the test script in the project's package.json file. The test script is like a shortcut or a predefined command that you can run using &lt;strong&gt;npm test&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The default test script in &lt;strong&gt;package.json&lt;/strong&gt; of a Create React App project typically looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//json

"scripts": {
  "test": "react-scripts test"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This script, in turn, runs the command associated with it, which is typically &lt;strong&gt;react-scripts test&lt;/strong&gt;. Behind the scenes, &lt;strong&gt;npm test&lt;/strong&gt; typically invokes &lt;strong&gt;react-scripts test&lt;/strong&gt; to execute the test scripts defined in the project. So both npm test and react-scripts test can be used interchangeably to run tests in a Create React App project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example of Jest test
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test("displays the text 'hello world'", () =&amp;gt; {
  render(&amp;lt;Article /&amp;gt;);

  expect(screen.queryByText("hello world")).toBeInTheDocument();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The test example above is doing the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;render&lt;/strong&gt;: The render method is used to render a React component inside the testing environment or virtual DOM. Since the Jest test don't run in a actual browser but instead run inside &lt;strong&gt;JSDOM&lt;/strong&gt; which mimics the behavior of a browser's DOM. JSDOM allows Jest to simulate a browser like environment within node. This enable test to interact with &amp;amp; manipulate the DOM. You can also use DOM related APIs such as &lt;strong&gt;document, window, querySelector&lt;/strong&gt; in your test script. By default Jest sets up JSDOM for each test file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;expect&lt;/strong&gt;: The expect method used to define assertions and make comparisons between values. You can use various &lt;strong&gt;matchers&lt;/strong&gt; with the expect method to check if a certain condition is met. For example in the code above we are using the custom JSDOM matcher &lt;strong&gt;toBeInTheDocument&lt;/strong&gt; which checks if the element is present in the DOM. You can use this matcher to verify if an element if rendered &amp;amp; exist in the current document.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;screen&lt;/strong&gt;: The screen object is used to interact with the rendered components. It also provides helper methods to query the DOM for elements we expect to have been rendered. screen allows convenient way to access elements without having to rely on the document object directly. In the above example the line &lt;code&gt;screen.queryByText('hello world')&lt;/code&gt; searches the virtual DOM for some element that has the text content "hello world". If no element is found, it will return &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, React testing with &lt;strong&gt;Jest&lt;/strong&gt; provides a powerful and efficient way to ensure the quality of your application. With Jest's comprehensive testing framework and Reacts testing utilities, you can easily write and execute tests to validate the behavior and functionality of your React components. - &lt;em&gt;Happy testing!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>testing</category>
      <category>javascript</category>
      <category>jest</category>
    </item>
    <item>
      <title>Introduction to NPM</title>
      <dc:creator>Pedro Marungo</dc:creator>
      <pubDate>Sun, 14 May 2023 19:10:06 +0000</pubDate>
      <link>https://dev.to/pedromarungo/introduction-to-npm-117</link>
      <guid>https://dev.to/pedromarungo/introduction-to-npm-117</guid>
      <description>&lt;p&gt;NPM stands for &lt;strong&gt;Node Package Manager.&lt;/strong&gt; It is a tool used to manage and share reusable packages of code for Node.js. It is a command-line utility that installs, updates and uninstalls packages.&lt;/p&gt;

&lt;p&gt;NPM is installed automatically with Node.js. It is used to install packages globally or locally in a particular folder.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjquqko4hyetg4vtoljys.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjquqko4hyetg4vtoljys.gif" alt="Image description" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Installing packages with npm
&lt;/h2&gt;

&lt;p&gt;Notes on installing packages with npm:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;During the project development process, there may be a need to add specific packages.&lt;/li&gt;
&lt;li&gt;To install packages, navigate to the project directory and run the command &lt;code&gt;npm install &amp;lt;package_name&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This command will add the package as a dependency in the project's package.json file.&lt;/li&gt;
&lt;li&gt;It is essential to have a correctly structured package.json file for the installation to work.&lt;/li&gt;
&lt;li&gt;If the package.json file is missing or incorrectly structured, the installation will fail.&lt;/li&gt;
&lt;li&gt;npm provides a built-in command, &lt;code&gt;npm init&lt;/code&gt;, which can be used to create the package.json file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm init&lt;/code&gt; allows you to initialize a new project and generate a package.json file with the required structure.&lt;/li&gt;
&lt;li&gt;With the generated package.json file, you can then proceed to install additional packages using &lt;code&gt;npm install&lt;/code&gt;.
Remember to always maintain a properly structured package.json file and use the appropriate npm commands to install and manage packages within your project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To install a package globally, you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g {package name}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To install a package locally, you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install {package name}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Package.json
&lt;/h2&gt;

&lt;p&gt;NPM also allows you to manage dependencies for a project. You can create a &lt;code&gt;package.json&lt;/code&gt; file which contains the list of dependencies for your project. To create this file, you can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will prompt you to enter the details of your project such as the name, version, description, and author. Once you have entered the details, it will create a &lt;code&gt;package.json&lt;/code&gt; file for you.&lt;/p&gt;

&lt;p&gt;To install all the dependencies listed in your &lt;code&gt;package.json&lt;/code&gt; file, you can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notes on package.json and npm:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The package.json file is used to specify the required packages for a JavaScript application.&lt;/li&gt;
&lt;li&gt;It lists out each package's name and version number.&lt;/li&gt;
&lt;li&gt;When the command &lt;code&gt;npm install&lt;/code&gt; is run in a directory with a package.json file, npm reads the dependencies listed in package.json.&lt;/li&gt;
&lt;li&gt;It downloads the packages from &lt;a href="http://npmjs.com/" rel="noopener noreferrer"&gt;npmjs.com&lt;/a&gt;, the hosting platform for npm packages.&lt;/li&gt;
&lt;li&gt;The installation process also involves resolving dependencies for each package.&lt;/li&gt;
&lt;li&gt;Each package may have its own package.json file with its specific dependencies, forming a dependency tree.&lt;/li&gt;
&lt;li&gt;npm ensures that all dependencies, including nested ones, are fetched and installed.&lt;/li&gt;
&lt;li&gt;In a local environment, running &lt;code&gt;npm install&lt;/code&gt; creates a folder called "node_modules."&lt;/li&gt;
&lt;li&gt;The node_modules folder contains all the downloaded packages necessary for the application.
Remember to regularly update your package.json file to manage and track the required packages and their versions accurately.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installing, updating, removing packages
&lt;/h2&gt;

&lt;p&gt;NPM also allows you to update and uninstall packages. To update a package, you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm update {package name}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To uninstall a package, you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm uninstall {package name}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In conclusion, NPM is a powerful tool that allows you to manage and share packages of code for Node.js. It simplifies the process of installing, updating and uninstalling packages and managing dependencies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Publishing packages with npm
&lt;/h2&gt;

&lt;p&gt;Another important feature of NPM is that it allows you to publish your own packages to the NPM registry, which can be used by other developers. To publish a package, you need to first create a &lt;code&gt;package.json&lt;/code&gt; file, which includes the details of the package such as name, version, and dependencies. You can then use the following command to publish the package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm publish
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;NPM also provides a way to manage multiple versions of the same package, which is useful when you need to maintain compatibility with different versions of Node.js or other dependencies. You can specify the version of a package when you install it by using the &lt;code&gt;@&lt;/code&gt; symbol followed by the version number, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install {package name}@{version}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>npm</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>Mastering array iteration</title>
      <dc:creator>Pedro Marungo</dc:creator>
      <pubDate>Sun, 14 May 2023 08:19:36 +0000</pubDate>
      <link>https://dev.to/pedromarungo/mastering-array-iteration-50cl</link>
      <guid>https://dev.to/pedromarungo/mastering-array-iteration-50cl</guid>
      <description>&lt;p&gt;&lt;em&gt;Introduction:&lt;/em&gt;&lt;br&gt;
JavaScript's array iteration methods are useful for working with arrays. They offer simple and efficient ways to manipulate arrays, making your code cleaner, easier to understand, and faster. Learning these methods is important for effectively manipulating arrays in JavaScript. Let's explore these methods and learn how to unleash the full potential of your array operations!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. forEach():&lt;/strong&gt;&lt;br&gt;
The forEach() method in JavaScript allows you to perform actions on each item in an array. It simplifies iterating over an array and performing tasks on each element.&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];
numbers.forEach((number) =&amp;gt; {
  console.log(number);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. map():&lt;/strong&gt;&lt;br&gt;
The map() method in JavaScript creates a new array by applying a function to each element of the original array. It's useful for transforming and extracting specific values from arrays.&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 doubledNumbers = numbers.map((number) =&amp;gt; number * 2);
console.log(doubledNumbers); // Output: [2, 4, 6, 8, 10]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. filter():&lt;/strong&gt;&lt;br&gt;
The filter() method in JavaScript creates a new array by selecting elements that meet certain conditions. It helps in refining and organizing data by picking specific elements.&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 evenNumbers = numbers.filter((number) =&amp;gt; number % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. reduce():&lt;/strong&gt;&lt;br&gt;
The reduce() method in JavaScript combines all elements of an array into a single value. It's useful for performing calculations or aggregating data.&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 sum = numbers.reduce((accumulator, current) =&amp;gt; accumulator + current, 0);
console.log(sum); // Output: 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. find():&lt;/strong&gt;&lt;br&gt;
The find() method in JavaScript searches an array and returns the first element that matches a specific condition. It helps in finding specific elements in an array.&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 foundNumber = numbers.find((number) =&amp;gt; number &amp;gt; 3);
console.log(foundNumber); // Output: 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. some() and every():&lt;/strong&gt;&lt;br&gt;
The some() and every() methods in JavaScript evaluate elements in an array based on a given condition. They help in determining if elements match specific criteria or if all elements satisfy a certain condition.&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 hasEvenNumber = numbers.some((number) =&amp;gt; number % 2 === 0);
console.log(hasEvenNumber); // Output: true

const allNumbersPositive = numbers.every((number) =&amp;gt; number &amp;gt; 0);
console.log(allNumbersPositive); // Output: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Conclusion:&lt;/em&gt;&lt;br&gt;
JavaScript's array iteration methods provide efficient ways to manipulate arrays. They improve code readability and performance. Mastering these methods is crucial for effective array manipulation in JavaScript. &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript Arrays</title>
      <dc:creator>Pedro Marungo</dc:creator>
      <pubDate>Fri, 28 Apr 2023 08:42:42 +0000</pubDate>
      <link>https://dev.to/pedromarungo/javascript-arrays-3505</link>
      <guid>https://dev.to/pedromarungo/javascript-arrays-3505</guid>
      <description>&lt;h2&gt;
  
  
  Creating Arrays (data-list)
&lt;/h2&gt;

&lt;p&gt;An array in JavaScript is basically an order list. Every item inside the array list is place in a position known as an index.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzag3mh7d6705c6mx6p1n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzag3mh7d6705c6mx6p1n.png" alt="Image description" width="800" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Arrays begin at index 0 for the first item and then the following index position 1 for the next item in the array,and so on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating an Array literal
&lt;/h2&gt;

&lt;p&gt;One way to create an array in javascript is to use an array &lt;em&gt;literal&lt;/em&gt; (wrapping items inside of square brackets [&lt;code&gt;'item1', 'item2']&lt;/code&gt;. Each item inside the array is known as an individual &lt;em&gt;element&lt;/em&gt;. Each element is wrapped in quotes single or double if the data type is string or backticks if you are using template literals for data interpolation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft3com7vzylm9e85sarmb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft3com7vzylm9e85sarmb.png" alt="Image description" width="800" height="236"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm0aq1cifdtio55n8r8x4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm0aq1cifdtio55n8r8x4.png" alt="Image description" width="800" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Arrays data types
&lt;/h2&gt;

&lt;p&gt;Arrays can store any data types. All primitive data types from &lt;em&gt;&lt;strong&gt;Boolean, Null, Undefined, Number, String, Symbol&lt;/strong&gt;&lt;/em&gt;. Though it is not recommended to use Null and Undefined because they can cause some quirks.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxqq8ni6vp944n9d4nuvq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxqq8ni6vp944n9d4nuvq.gif" alt="Image description" width="500" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Example of quirks storing &lt;code&gt;undefined&lt;/code&gt; inside an array literal. In the example below we can see that we are console logging some of the array elements by using bracket notation and passing in the index value we want to log to the terminal. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7pxq9gxntbl5pcasvrk5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7pxq9gxntbl5pcasvrk5.png" alt="Image description" width="800" height="259"&gt;&lt;/a&gt;&lt;br&gt;
Towards the end of the array we try to log an element at index 5, but there is no element at index 5 so the output returns undefined. So basically undefined is use to display values that are yet not defined. So it is best to follow good practice and not define variables with a value of undefined or store an array element in the same way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples of Array data types
&lt;/h2&gt;

&lt;p&gt;As mentioned above arrays can hold any data types as elements they are place inside an array and are separated by a comma ,. Here is a example of an array with different data types store as elements &lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx9lq9q9u3cezbvrv0z5p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx9lq9q9u3cezbvrv0z5p.png" alt="Image description" width="800" height="209"&gt;&lt;/a&gt;&lt;br&gt;
The example above has some different data types including &lt;code&gt;string, number, boolean, object&lt;/code&gt;. We even see another empty array inside our array. (arrays are known and consider to be a special kind object in javascript. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ilxjvnqhmp5ojcmoids.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ilxjvnqhmp5ojcmoids.gif" alt="Image description" width="480" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessing Array elements
&lt;/h2&gt;

&lt;p&gt;Each element stored inside the array has a numbered position known as an index. Remember array indexes start at 0 not 1. We can access stored elements inside the array using their corresponding index. &lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fye191gd7joa9moh634ry.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fye191gd7joa9moh634ry.png" alt="Image description" width="800" height="289"&gt;&lt;/a&gt;&lt;br&gt;
In the example above we retrieved the 2nd element index at spot 2 inside our array. We do this by referencing the array name or better known as an identifier, followed by bracket notation &lt;code&gt;[2]&lt;/code&gt;. The console.log returns the value &lt;em&gt;mango&lt;/em&gt; which is stored in index 2 inside the fruits array. &lt;/p&gt;

&lt;h2&gt;
  
  
  Storing array elements to variables
&lt;/h2&gt;

&lt;p&gt;If we wanted to store individual element values that are stored inside our array to variable we are able to do that &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw5in6sd05d9nd1dkenle.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw5in6sd05d9nd1dkenle.png" alt="Image description" width="800" height="162"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If an array is store in a const variable we are still able to modify elements inside that const variable array. This is known as &lt;code&gt;mutable&lt;/code&gt;. Meaning we can change the elements inside that array even if it was defined with a const variable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foqjr21v9jywxcrwwedat.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foqjr21v9jywxcrwwedat.gif" alt="Image description" width="486" height="274"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating or changing elements
&lt;/h2&gt;

&lt;p&gt;If for some reason we wanted to change an element of our array we can do so by using bracket notation as mentioned above and the index of the element we want to modify, followed by the assignment operator &lt;code&gt;=&lt;/code&gt; and the new value. Again can be any data type.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpxdgt5krjbsie1me769o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpxdgt5krjbsie1me769o.png" alt="Image description" width="800" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Returning the number of elements in an array
&lt;/h2&gt;

&lt;p&gt;If we want to know how many elements are in our array or any given array we can use a built in array property &lt;code&gt;length&lt;/code&gt;. The length array property returns the number of elements in an array. We access the length property with dot notation &lt;code&gt;array.length&lt;/code&gt; .&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyrqg2gdkh12qfpsfybod.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyrqg2gdkh12qfpsfybod.png" alt="Image description" width="800" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding elements to an array
&lt;/h2&gt;

&lt;p&gt;If we wanted to add new elements to an array dynamically using a built in JavaScript method we could do so with the &lt;code&gt;.push()&lt;/code&gt; built in method. &lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjmpicfvcryjagil7qvxw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjmpicfvcryjagil7qvxw.png" alt="Image description" width="800" height="226"&gt;&lt;/a&gt;&lt;br&gt;
We use the push method by using dot notation and then the method name, followed by invocation operators (). The push method can take as many arguments as you like but a large numbers of arguments can cause performance and memory usage issues for your application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0lo6cu30w7bqdcrmgedq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0lo6cu30w7bqdcrmgedq.gif" alt="Image description" width="448" height="250"&gt;&lt;/a&gt;&lt;br&gt;
An Important thing to know is that the .push() method is known in JavaScript as an &lt;em&gt;destructive array method&lt;/em&gt; that will change and modify the original initial array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing the last element in an array
&lt;/h2&gt;

&lt;p&gt;Just like the .push() method that allows us to add elements to the end of an array. The .pop() method removes the last item in an array. The .pop() method does not take any arguments and is also known as a &lt;em&gt;destructive array method&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftqocfx6o104fuy10m721.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftqocfx6o104fuy10m721.png" alt="Image description" width="800" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The .pop() method also has a return value that we can capture with a variable for later use, the .pop() method returns the last element in the array that was removed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcc8mlekwnphjwlo9c9jh.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcc8mlekwnphjwlo9c9jh.gif" alt="Image description" width="498" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing the first element in an array
&lt;/h2&gt;

&lt;p&gt;If we wanted to remove the first element in an array we can use yet another built in JavaScript method &lt;code&gt;.shift()&lt;/code&gt;. The .shift() method does not take any arguments and is also known as a &lt;em&gt;destructive array method&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyr45jcm3ewpesuf0ycud.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyr45jcm3ewpesuf0ycud.png" alt="Image description" width="800" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding an element at the start of an array
&lt;/h2&gt;

&lt;p&gt;Let's say we wanted to add a new element to the start of the array. Well you guess it! there is a built in array method for that as well. The &lt;code&gt;.unshift()&lt;/code&gt;method takes any amount of arguments we would like to add as elements to the start of the array and is also known as a &lt;em&gt;destructive array method&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F99r6ds3gskctalt75dt7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F99r6ds3gskctalt75dt7.png" alt="Image description" width="800" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Extracting a portion of elements of an array
&lt;/h2&gt;

&lt;p&gt;If we want to remove a portion of elements from a given array we can use another built in method &lt;code&gt;.slice()&lt;/code&gt;. The slice method takes 2 arguments the starting index (where to start the extraction) and the ending index (where to end the extraction), if the ending index is not given .slice() method will extract starting from the starting given index to the end of the array. The .slice() method is a &lt;em&gt;non-destructive array method&lt;/em&gt; it instead will extract the elements and return a new array. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxujks4jm9g246r9rjob.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxujks4jm9g246r9rjob.png" alt="Image description" width="800" height="273"&gt;&lt;/a&gt;&lt;br&gt;
We must save the .slice() method statement in a variable to be able to see the new array with the extracted elements. If we console.log() the new array it will only output the arrays extracted from start to ending index. If we console.log the original array it will output all values, as it was not modified.&lt;/p&gt;

&lt;h2&gt;
  
  
  Search for a particular element index in an array
&lt;/h2&gt;

&lt;p&gt;If we wanted to search for a particular index of an element, we can use the &lt;code&gt;.indexOf()&lt;/code&gt; method. The .indexOf() method takes in 1 argument the element we are looking for and an optional second argument the starting index of where to start the search. The .indexOf() method is a &lt;em&gt;non-destructive array method&lt;/em&gt; it instead will extract the element and return a new array which we save to a variable. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4rk66b28wr13i2gjl2z5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4rk66b28wr13i2gjl2z5.png" alt="Image description" width="800" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we provide an element that does not exist in the array or if we start at an index that is passing the the index of the element we want, the .indexOf() method will return -1 for both of these occurrences.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Variable Scope Pollution</title>
      <dc:creator>Pedro Marungo</dc:creator>
      <pubDate>Fri, 28 Apr 2023 01:55:02 +0000</pubDate>
      <link>https://dev.to/pedromarungo/variable-scope-pollution-7j1</link>
      <guid>https://dev.to/pedromarungo/variable-scope-pollution-7j1</guid>
      <description>&lt;p&gt;When writing &lt;strong&gt;JavaScript&lt;/strong&gt; programs as a beginner we tend to define lots of variables in our program. Now we can freely create as many variables as we like using &lt;code&gt;var, let, const.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fujjdty8adxgsh2lqasxt.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fujjdty8adxgsh2lqasxt.gif" alt="Image description" width="476" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I think by now we all know that using var is not recommended unless we are maintaining some legacy program code. We also know that using var can we lead some weird quirks like variable hoisting and redeclaration.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6onoty9fdacqznrvunzm.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6onoty9fdacqznrvunzm.gif" alt="Image description" width="480" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Overall though this article is not about var or its weird legacy quirks instead this article is here to bring forth another quirk that not only affects variables declared with var but also let. &lt;/p&gt;

&lt;h2&gt;
  
  
  Global vs local scope (Variables)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi65ccamdv9b2xrxd5xe5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi65ccamdv9b2xrxd5xe5.gif" alt="Image description" width="498" height="337"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we define variables in JavaScript we usually define them in the global scope of our programs. For example review the screenshot below &lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwcymmkbc5si9ohpsz7un.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwcymmkbc5si9ohpsz7un.png" alt="Image description" width="800" height="611"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is fine for simple programs or if we are just sandboxing some code. But in more complex and professional programs we might want to actually avoid defining variables in the global scope. You might be wondering why? &lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fehyyv4u88sb03r27ukjo.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fehyyv4u88sb03r27ukjo.gif" alt="Image description" width="500" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well if we define variables in the global &lt;strong&gt;scope&lt;/strong&gt; we allow access to those variables from anywhere in our JavaScript code and now those variables can be reassigned (let &amp;amp; var) and redeclared specially if we use the var keyword to create those variables. This can lead to unexpected results and hard to track bugs in a larger program. &lt;/p&gt;

&lt;p&gt;For example review the snapshot below which inside the function we reassigned the global variable value. In the global scope the variable has a value of 2003 and inside the function we reassigned the value to 2004. When we &lt;strong&gt;&lt;code&gt;console.log&lt;/code&gt;&lt;/strong&gt; the value of the variable inside the function and then invoke the function call we see the result of 2004 inside the text editor terminal console. We also see if we console log the previously global scope variable &lt;em&gt;mySpaceFounded&lt;/em&gt; it will also output the value 2004, which means that the global variable value was altered and changed throughout the whole JavaScript program. &lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7388mtngx9z8wib1qgob.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7388mtngx9z8wib1qgob.png" alt="Image description" width="554" height="857"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  So what is the solution or best practice?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F429v38nz4nxe0fcfqwhy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F429v38nz4nxe0fcfqwhy.gif" alt="Image description" width="540" height="304"&gt;&lt;/a&gt;&lt;br&gt;
We can avoid defining global variables unless we are certain that we want to allow all of our code access to those global variables. So instead we can define our variables only inside block statements like the example below. &lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhx07bstnyg6u8wtas2bj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhx07bstnyg6u8wtas2bj.png" alt="Image description" width="800" height="279"&gt;&lt;/a&gt;&lt;br&gt;
This practice of defining variables inside block statements is known as local scope or local context for variables. This makes sense since we don't want the random number variable in the global scope and later on we make the mistake of changing the variables value and affect other parts of our program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scope Pollution
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqw1c5fnp3k1uyact2xtj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqw1c5fnp3k1uyact2xtj.gif" alt="Image description" width="480" height="360"&gt;&lt;/a&gt;&lt;br&gt;
The term scope pollution may not seem that popular for beginners but it should be a concept that is taught because it will lead to bad practices amongst beginner developers. Scope pollution is the idea that too many variables or functions are defined in the global scope of our programs.&lt;/p&gt;

&lt;p&gt;This may seem not too concerning at all but when working with 3rd party or open source libraries we can run into issues, specially if those libraries or outside code have their own global variables hidden in different parts of the program, again this can lead to confusing and hard to track down bugs which can cause both time and money for a company. Defining our functions or variables in global scope will place our globally defined variables and functions into the global namespace object which is is available to us and any outside code we pull inside our programs. &lt;/p&gt;

&lt;h2&gt;
  
  
  Free up memory
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2fki5kb3ziirwbp2rqci.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2fki5kb3ziirwbp2rqci.gif" alt="Image description" width="500" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Defining variables in the global scope will make our JavaScript program retain used up memory for those variables or functions. The memory will not be release until our program crashes or comes to a complete end or by our web page being closed or refreshed. On the other the hand if you define your variables inside local scope. Well when you program runs and those block statement get call or executed a new execution context is created, which will include a local environment. This environment will include all the variables and functions defined inside that function or block statement including any arguments passed into the function. When the the function or block statement finishes executing, its execution context is destroyed, At this point, the JavaScript engine may choose to free the memory used by the local variables.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>coding</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Self Taught Developer Journey 2023</title>
      <dc:creator>Pedro Marungo</dc:creator>
      <pubDate>Sun, 05 Mar 2023 05:40:25 +0000</pubDate>
      <link>https://dev.to/pedromarungo/self-taught-developer-journey-2023-4mpp</link>
      <guid>https://dev.to/pedromarungo/self-taught-developer-journey-2023-4mpp</guid>
      <description>&lt;h2&gt;
  
  
  The start of my Journey
&lt;/h2&gt;

&lt;p&gt;In late 2011 I remember glancing over at my TV and a movie was playing called &lt;em&gt;&lt;strong&gt;The Social Network&lt;/strong&gt;&lt;/em&gt;. During this time, &lt;strong&gt;Facebook&lt;/strong&gt; was at the top as the most used and beloved social network on the market. But sadly for me, I had been a &lt;em&gt;&lt;strong&gt;MySpace&lt;/strong&gt;&lt;/em&gt; user. After watching a piece of the,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Social Network &lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft2pigg9wxiyh25p44u1i.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft2pigg9wxiyh25p44u1i.gif" alt="Image description" width="500" height="280"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I've decided why not try to create the next myspace social network version.? With this idea in mind I decide to ask my tech knowledgeable brother on how to go about this new project idea I had. He soon explain I needed to learn how to write programming code and it would take a long time but it was possible!.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgynzx12tceq3vb4xh750.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgynzx12tceq3vb4xh750.gif" alt="Image description" width="400" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The learning challenge
&lt;/h2&gt;

&lt;p&gt;Now if you are new to this coding/programming world. Well then you don't know how lucky you are, back then if you wanted to learn how to program/code you kinda had to figure it out on your own. Yes you could go to &lt;strong&gt;stack-overflow&lt;/strong&gt; or some coding forum but the people in there weren't so nice and would tell you to find your own learning resources. Years after knowing how long and difficult it was for me to scatter the web and piece together resources. I understood the tough love I receive from these programmers online. Learning how to program/code is not easy, it takes time and dedication.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7hthv3sppbjr3hizpqbx.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7hthv3sppbjr3hizpqbx.gif" alt="Image description" width="500" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Following my dreams...
&lt;/h2&gt;

&lt;p&gt;Since 2012, I have wrote code for a small time &lt;strong&gt;MMORPG&lt;/strong&gt; in &lt;strong&gt;&lt;em&gt;C#&lt;/em&gt;&lt;/strong&gt; and converter their &lt;strong&gt;XHTML&lt;/strong&gt; to &lt;strong&gt;HTML5&lt;/strong&gt;. This gave me some serious insight into what it takes to learn and create projects. Soon after in 2016 I launch my first &lt;strong&gt;Android&lt;/strong&gt; application written in &lt;strong&gt;Java&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://play.google.com/store/apps/details?id=rottenkittenstudio.com.buddhatheawakenedone&amp;amp;hl=en&amp;amp;gl=US&amp;amp;pli=1" rel="noopener noreferrer"&gt;https://play.google.com/store/apps/details?id=rottenkittenstudio.com.buddhatheawakenedone&amp;amp;hl=en&amp;amp;gl=US&amp;amp;pli=1&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zw8vpixy1ob0evfviu5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zw8vpixy1ob0evfviu5.png" alt="Image description" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well thanks again to brother who taught and help me learn some Java code. My brother who wrote and created his first android application in 2014 that got over 100k downloads and made a hefty $5K a month. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7afeir5viejegichtoab.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7afeir5viejegichtoab.gif" alt="Image description" width="498" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Though seeing the success of my brother's application downloads and hefty cash deposits. I was not in it for the money, to me creating something bigger was my goal. I still had the itch for creating a social network and platforms where people can interact and connect. Not to say that money was not needed!. But it was not the driving force for my learning and coding journey.&lt;/p&gt;

&lt;h2&gt;
  
  
  Early struggles and current situation
&lt;/h2&gt;

&lt;p&gt;Though I don't like to share much of my personal life with strangers. From 2010 to about 2015, me and my brother did not have a place to call home and experience homelessness. Coding from broken down computers and stealing wifi from neighbors, we eventually use some of the earnings my brother made to help our living situation. I only share this story cause it prevent me from having a steady learning process in my journey to &lt;strong&gt;self taught developer&lt;/strong&gt;, but also taught me to be humble patient and resilient. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgtb4o7nthael77oib8n7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgtb4o7nthael77oib8n7.gif" alt="Image description" width="500" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I am now currently enroll in &lt;strong&gt;Flatiron School - Online Bootcamp&lt;/strong&gt; to complete and receive a certification for &lt;strong&gt;full-stack engineer.&lt;/strong&gt; I would love to work and help companies grow and create good meaningful software, while still investing and creating personal projects in my free time to achieve my dream of launching a tech startup in &lt;em&gt;&lt;strong&gt;Chicago IL&lt;/strong&gt;&lt;/em&gt; and putting a dent in the universe. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvgv8b035gby70lfxj0ue.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvgv8b035gby70lfxj0ue.jpeg" alt="Image description" width="645" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
