<?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: Lamina Busayo</title>
    <description>The latest articles on DEV Community by Lamina Busayo (@busayo2605).</description>
    <link>https://dev.to/busayo2605</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%2F1094057%2F41614d0b-60ad-4b76-b832-0049418b10ab.png</url>
      <title>DEV Community: Lamina Busayo</title>
      <link>https://dev.to/busayo2605</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/busayo2605"/>
    <language>en</language>
    <item>
      <title>Loops in JavaScript</title>
      <dc:creator>Lamina Busayo</dc:creator>
      <pubDate>Fri, 14 Jul 2023 17:51:42 +0000</pubDate>
      <link>https://dev.to/busayo2605/loops-in-javascript-56pl</link>
      <guid>https://dev.to/busayo2605/loops-in-javascript-56pl</guid>
      <description>&lt;p&gt;loops are used in javaScript to perform repeated task based on condition. conditioons typically returns true or false. A loop will continue running until the defined condition returns false.&lt;br&gt;
This concept will explianed using an array of strings&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const people = ['Sam', 'John', 'Sylvia', 'Steve']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To iterate over an Array means to loop through an Array’s contents. There are majorly 7 types of loops in javaScript. To get the most out of this article, you will need to understand concepts like Array, String, and Function. If you don’t, you will need to take a step back to have an understanding on the aformentioned concecpts. If you do, let’s get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;For Loop&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let’s start with the most common and the oldest loop, the For Loop. This is what it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; people.length; i++) {
    console.log(people[i])
}

//output
    //Sam
    //John
    //Sylvia
    //Steve

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

&lt;/div&gt;



&lt;p&gt;You can see in the above that between the brackets there are three required attributes, these are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let i = 0; – This creates a new variable that we increment on every loop. “i” represents the Index and we use this variable to access the people in the array.&lt;/li&gt;
&lt;li&gt;i &amp;lt; people.length; – This is a conditional statement. This code tells the engine to keep looping as long as “i” is less than the Array size.&lt;/li&gt;
&lt;li&gt;i++;  – This last attribute is shorthand to increment i. It is the same as writing i = i+1. It will add 1 to “i” on every loop.
In our example, the above loop will loop through each emoji and then print the people to the console:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This basic For loop will run anywhere you can execute JavaScript. You can always rely on this to be available in any browser.&lt;/p&gt;

&lt;p&gt;Sometimes you need to loop until you find an element in the Array. To do this we can use a Break statement.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Break&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Break will stop the For loop from running. This is useful when you are trying to find a value in a large Array. For example, say that we were not sure where the string Sylvia was in our Array and we wanted to loop until we found it.&lt;br&gt;
Well, we could write code 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;for (let i = 0; i &amp;lt; people.length; i++) {
    if (people[i] === 'Sylvia'){
        break
    }   
    console.log(people[i])
}

//output
    //Sam
    //John
    //Sylvia


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

&lt;/div&gt;



&lt;p&gt;As soon as we match the the string Sylvia the loop stops. &lt;/p&gt;

&lt;p&gt;What if we wanted to perform an operation on all the values in the Array except one? In this case, we could use Continue.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Continue&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Continue is like a Break statement but instead of stopping the loop it can skip or “jump over” an item in the array.&lt;/p&gt;

&lt;p&gt;This time we are going to write the code so that when we find the string Sylvia, we skip it and we won’t print it to the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    for (let i = 0; i &amp;lt; people.length; i++) {
        if (people[i] === 'Sylvia'){
            break
        }   
        console.log(people[i])
    }

    //output
        //Sam
        //John
        //Steve


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

&lt;/div&gt;



&lt;p&gt;As you can see, the code is almost the same as the prior example, except that this time we have replaced Break with Continue. The outcome is different:&lt;br&gt;
The string Sylvia has not been printed as expected. You can use Continue to filter an Array and only perform an operation on some of the items.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;For..In&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We have looked at traditional For loops, but there are shorter versions. We can use a For..in loop to loop through an Array 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;    for (const i in people) {
        console.log(people[i])
    }

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

&lt;/div&gt;



&lt;p&gt;This is shorter code and there is no need to increment the index using the i++ code. So why not use this loop all the time?&lt;br&gt;
There is a problem with For..in.&lt;br&gt;
You see, it is possible to extend a built-in object like Array. For example, I can create a custom operation on an Array, 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;Array.prototype.foo = 1
for (const i in people) {
      console.log(people[i])
}

//output
    //Sam
    //John
    //Sylvia
    //Steve
    //foo

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

&lt;/div&gt;



&lt;p&gt;Now when you run the code this is the output:&lt;br&gt;
The &lt;code&gt;foo&lt;/code&gt; function is part of the loop! For this reason, you should never use For..in in your code.&lt;/p&gt;

&lt;p&gt;We have discussed the basic For loops—next, we will look at While loops.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;While Loop&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A While Loop will continue to loop while a condition is true.&lt;/p&gt;

&lt;p&gt;For example, the below code will loop while “i” is less than the Array size:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i = 0 
while (i &amp;lt; people.length){
    console.log(people[i])
    i++
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are doing the same thing as we were in the For loop:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;i = 0 – Creating an index variable and setting it to 0.&lt;/li&gt;
&lt;li&gt;i &amp;lt; emojis.length – Looping while i is less than emojis.length&lt;/li&gt;
&lt;li&gt;console.log(emojis[i]) – printing each emoji String to the console.&lt;/li&gt;
&lt;li&gt;i++ – Incrementing the index by 1 on each loop.
This code will print all the people.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why would you use a While loop over a For loop?&lt;/p&gt;

&lt;p&gt;Although they are interchangeable, you will not see While loops very often in JavaScript code. But, they can be useful. For example, say we are not sure what position the string John is in. We want to loop until we see a John. This is what the code will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let i = 0
let noJohn = true 
while (noJohn){
    console.log(people[i])
    i++
    if (people[i] === 'John') {

        noJohn = false
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For each loop, we check the condition (noJohn) and if it is ‘true,’ then the loop continues. As soon as ‘noJohn’ is set to false the loop will stop. This is like the break statement For loop we used above.&lt;/p&gt;

&lt;p&gt;What if you wanted to check the conditional after the loop? Well, in that case, you need a Do/While loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Do/While Loop&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This loop is almost identical to the while loop, except the condition check is at the end. This is what the code will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let i = 0 
do {
    console.log(people[i])
    i++
} while (i &amp;lt; people.length)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code will run in this flow:&lt;/p&gt;

&lt;p&gt;Set the index to 0&lt;br&gt;
Print the first element in the Array using console.log&lt;br&gt;
Add 1 to the index&lt;br&gt;
Then check is index is still less than the size of the Array&lt;/p&gt;
&lt;h1&gt;
  
  
  &lt;em&gt;ES5&lt;/em&gt;
&lt;/h1&gt;

&lt;p&gt;All the loops we have discussed so far are available in any JavaScript environment. In front-end development, we need to think about browser support. Over the last few years, there have been many advances in the JavaScript language. There are new loops available.&lt;/p&gt;

&lt;p&gt;The first changes came in ES5, which means version 5 of JavaScript. The new loops are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;forEach&lt;/li&gt;
&lt;li&gt;&lt;p&gt;map&lt;br&gt;
For both of these are operations, you can call on an Array. For the people Array, it would look like this:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;people.forEach()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;people.map()&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s look at these more in depth.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;forEach&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;forEach is a function added to the Array type. The function takes a callback function, which is a block of code, 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;people.forEach(person =&amp;gt; console.log(person))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For every value inside the Array, it will run the callback block you pass inside the brackets. This is a nice way to do For loops in ES5.&lt;/p&gt;

&lt;p&gt;If forEach is available, then this is the recommended way to do a loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Map&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You can loop using a Map function the same way as forEach by passing a callback. The map function then calls this callback for every item in the Array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;people.map(person =&amp;gt; console.log(person))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference with Map is that it returns an Array. This function can convert the contents of one Array to another. For example, if we wanted to create a new Array with all the people as John, we could run this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const johns = people.map(person =&amp;gt; 'John')

console.log(johns)

//output
    //['John', 'John', 'John', 'John']

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

&lt;/div&gt;



&lt;p&gt;Use this Loop when you need to create a new array from an existing one. This can be a very powerful loop when combined with reduce.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;em&gt;ES6&lt;/em&gt;
&lt;/h1&gt;

&lt;p&gt;The last loop we are going to look at is part of ES6.&lt;/p&gt;

&lt;p&gt;This is the For..of loop.&lt;/p&gt;

&lt;p&gt;This loop addresses the shortcomings of “For..In” and has similar code syntax.&lt;/p&gt;

&lt;p&gt;The browser support for ES6 is getting better yet IE is a bit behind. This means you may not be able to use For..of if you need to support the IE browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;For..of&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Like the For..of loop the code 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;for (person of people) {
    console.log(person)
}

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

&lt;/div&gt;



&lt;p&gt;This does not have the same problem as the For..in loop. Anything added to the Array will not appear in the loop. If you are in an ES6 environment, then you can and should use this loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We have looked at the major loops that you can use in Javascript.&lt;/p&gt;

&lt;p&gt;If you do not have ES5 or ES6, then you can use the traditional For loop or a While loop.&lt;/p&gt;

&lt;p&gt;If you have ES5, then you can use either forEach or Map. If you are only supporting the latest browsers and have ES6, then you can look at using For..of.&lt;/p&gt;

&lt;p&gt;Whatever you do, don’t use For..in!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>HOW TO MAKE HTTP REQUEST</title>
      <dc:creator>Lamina Busayo</dc:creator>
      <pubDate>Sat, 08 Jul 2023 04:40:03 +0000</pubDate>
      <link>https://dev.to/busayo2605/how-to-make-http-request-4jlb</link>
      <guid>https://dev.to/busayo2605/how-to-make-http-request-4jlb</guid>
      <description>&lt;p&gt;JavaScript has great modules and methods to make HTTP requests that can be used to send or receive data from a server side resource. There are few popular ways to make HTTP requests in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Ajax&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Ajax is the traditional way to make an asynchronous HTTP request. Data can be sent using the HTTP POST method and receiving using the HTTP GET method. Let’s take look and make a &lt;code&gt;GET&lt;/code&gt; request. I’ll be using JSON Placeholder, a free online REST API for developers that return random data in JSON format.&lt;br&gt;
To make an HTTP call in Ajax, you need to initialize a new  &lt;code&gt;XMLHHttpRequest()&lt;/code&gt; method, specify the URL endpoint and HTTP method (in this case GET). Finally we use the &lt;code&gt;open()&lt;/code&gt; method to tie the HTTP method and URL endpoint together and call the &lt;code&gt;send()&lt;/code&gt; method to fire off the request.&lt;br&gt;
We log the HTTP response to the console by using the &lt;code&gt;XMLHTTPRequest.onreadystatechange&lt;/code&gt; property which contains the event handler to be called when the &lt;code&gt;readystatechanged&lt;/code&gt; event is called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Http = XMLHttpRequest();
const url = ‘https://jsonplaceholder.typicode.com/posts’;
Http.open(“GET”, url);
Http.send();
Http.onreadystatechange = (e) =&amp;gt; { console.log(Http.responseText)}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If viewed on the browser console, it will return an array of data in JSON format. But how would we know if the request is done?&lt;br&gt;
The &lt;code&gt;onreadystatechange&lt;/code&gt; property has 2 methods, &lt;code&gt;readyState&lt;/code&gt; and &lt;code&gt;status&lt;/code&gt;which allow us to check the state of our request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Http = XMLHttpRequest();
const url = ‘https://jsonplaceholder.typicode.com/posts’;
Http.open(“GET”, url);
Http.send();
Http.onreadystatechange = (e) =&amp;gt; { 
   If (this.readyState === 4 &amp;amp;&amp;amp; this.status === 200){
   console.log(Http.responseText)
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;fetch&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;fetch&lt;/code&gt; is a new powerful web API that let us make asynchronous requests. In fact, &lt;code&gt;fetch&lt;/code&gt; is one the best and my favorite way to make an HTTP request. It returns a “Promise” which is one of the great features of ES6. Promises allow us to handle the asynchronous request in a smarter way. Let’s look at how &lt;code&gt;fetch&lt;/code&gt; technically works.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const url = ‘https://jsonplaceholder.typicode.com/posts’;
fetch(url)
.then(data =&amp;gt; data.json())
.then(res =&amp;gt; console.log(res))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;fetch&lt;/code&gt; function takes one required parameter: the &lt;code&gt;endpoint&lt;/code&gt; URL. It also has other optional parameters as in the example below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const url = ‘https://jsonplaceholder.typicode.com/posts’;
const data = {
   name: “sam”,
   id:12
}
const otherParam = {
    headers: {
        “content-type”:”application/json; charaset=UTF-8”
    }
    body: data,
    method:”POST”
} 
fetch(url, otherParam)
.then(data =&amp;gt; data.json())
.then(res =&amp;gt; console.log(res))
.catch(err=&amp;gt; console.log(err))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, &lt;code&gt;fetch&lt;/code&gt; has many advantages for making HTTP requests. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Axios&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Axios is an open source library for making HTTP requests and provides many great features. Let’s have a look at how it works.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Useage&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;First, you’d have to include Axios. There are 2 ways to include Axios in your project.&lt;br&gt;
First, you can use npm:&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 axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you’d need to import it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from ‘axios’
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;secondly, you can include axios using a CDN&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src=”https://unpkg.com/axios/dist/axios.min.js”&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Useage&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;With Axios you can use &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; to retrieve and post data from the saver.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const url = ‘https://jsonplaceholder.typicode.com/posts’;
axios.get(url)
.then(data =&amp;gt; console.log(data))
.catch(err =&amp;gt; console.log(err))

const url = ‘https://jsonplaceholder.typicode.com/posts’;
const data = {
name: “sam”,
id:12
}
const param = {
    name: “sam”,
    id: 34
} 
Axios.get(url, param)
.then(data =&amp;gt; data.json())
.catch(err=&amp;gt; console.log(err))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;axios&lt;/code&gt; takes one required parameter, and can take a second optional parameter too.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const url = ‘https://jsonplaceholder.typicode.com/posts’;
const user = {
name: “sam”,
id:12
}

axios({
method: “post”,
url: url’
data: {
    user
}
})
.then(data =&amp;gt; console.log(data))
.catch(err =&amp;gt; console.log(err))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>OBJECT ORIENTED PROGRAMMING</title>
      <dc:creator>Lamina Busayo</dc:creator>
      <pubDate>Sun, 18 Jun 2023 05:08:38 +0000</pubDate>
      <link>https://dev.to/busayo2605/object-oriented-programming-30kb</link>
      <guid>https://dev.to/busayo2605/object-oriented-programming-30kb</guid>
      <description>&lt;p&gt;Object-oriented Programming treats data as a crucial element in program development and doesn't allow it to flow freely around the system. It ties data more securely to the function that operates on it and protects it from accidental modification from an outside function. OOP is a programming paradigm constructed around objects. Individual objects can be modified without affecting other program aspects, so it is easier to update and modify programs written in object-oriented languages. JavaScript is one of those languages that OOP is deeply rooted in.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Features of an Object-Oriented Language&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There are four rules or main pillars of Object-oriented programming language. This defines how the data and actions associated with the data; are organized using code.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Object&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;An object is a data structure containing properties and methods. Consider a student. A student will have characteristics like name, roll number, and class, and he will perform an action, let's say, giving an exam. In object-oriented programming, these characteristics are called data variables. These actions are called data methods.&lt;/p&gt;

&lt;p&gt;We create a class called Student and then create instances of this class. These instances are called Objects. Hence, an object is a real instance of a class. Class is nothing but a prototype/blueprint.&lt;/p&gt;

&lt;p&gt;Example: John Smith is an instance or object created using the class - Student.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Classes&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We know that objects hold the data and the functions to manipulate the data. However, the two can be bound together in a user-defined data type with the help of classes. Any number of objects can be created in a class. Each object is associated with the data of type class. A class is therefore a collection of objects of similar types.&lt;/p&gt;

&lt;p&gt;For example, consider the class “Fruits”. We can create multiple objects for this class &lt;/p&gt;

&lt;p&gt;Fruit Mango;&lt;/p&gt;

&lt;p&gt;This will create an object mango belonging to the class fruit. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Encapsulation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Encapsulation is the wrapping up/binding of data and function into a single unit called class. Data encapsulation is the most prominent feature of a class wherein the data is not accessible to the outside world, and only those functions wrapped inside the class can access it. These functions serve as the interface between the object’s data and the program.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Inheritance&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The phenomenon where objects of one class acquire the properties of objects of another class is called Inheritance. It supports the concept of hierarchical classification. Consider the object “car” that falls in the class “Vehicles” and “Light Weight Vehicles”.&lt;/p&gt;

&lt;p&gt;In OOP, the concept of inheritance ensures reusability. This means that additional features can be added to an existing class without modifying it. This is made possible by deriving a new class from the existing one.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Creating Objects in JavaScript&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We can create an object using the string literal in JavaScript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const student = {
        name: "Chris",
        age: 21,
        studies: "Computer Science",
    };

console.log(`${student.name} of the age ${student.age} studies ${student.studies})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OUTPUT: ‘Chris of the age 21 studies Computer Science’&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Creating objects using the new keyword.&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const student = new Object();
    student.name = "Chris",
    student.age=21,

    student.studies = "Computer Science";

console.log(`${student.name} of the age ${student.age} studies ${student.studies})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OUTPUT: ‘Chris of the age 21 studies Computer Science’&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Creating an object using the object constructor.&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function stud(name, age, studies){
        this.name = name;
        this.age = age;
        this.studies = studies;
    }
    const student = stud("Chris", 21, "Computer Science");

  console.log(`${student.name} of the age ${student.age} studies ${student.studies})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OUTPUT: ‘Chris of the age 21 studies Computer Science’&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Class Implementation in JavaScript&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript uses the ES6 standard to define classes. Consider the following example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Cars {
    constructor(name, maker, price) {
      this.name = name;
      this.maker =  maker;
      this.price = price;
    }
    getDetails(){
        return (`The name of the car is ${this.name}.`)
    }
  }
  let car1 = new Cars('Rolls Royce Ghost', 'Rolls Royce', '$315K');
  let car2 = new Cars('Mercedes AMG One', 'Mercedes', '$2700K');
  console.log(car1.name);    
  console.log(car2.maker);  
  console.log(car1.getDetails());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OUTPUT&lt;br&gt;
‘Rolls Royce Ghost’&lt;br&gt;
'Mercedes'&lt;br&gt;
‘The name of the car is Rolls Royce Ghost.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;JavaScript Encapsulation&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Encapsulation puts the data variables and the data functions together inside a box. Encapsulation ensures that data can only be accessed using the data functions defined inside the class, and abstraction ensures not anyone outside this encapsulated box can access it.&lt;/p&gt;

&lt;p&gt;But, we already saw that there is no actual concept of classes in JavaScript. So, JavaScript implements encapsulation using two ways: Function Scope and Closures.&lt;/p&gt;

&lt;p&gt;Let's discuss both of them with examples.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Function Scope&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When we define a variable or a function inside the function, we can only access it from inside and not from outside the function. This can be one way of implementing abstraction. Look at the example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function messageFunc(){
    const message= "Hey there!"
    console.log("Message from  inside: ",message);
}

messageFunc();

console.log("Message from  outside: ",message);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output for this code snippet says the right message when accessed from inside but throws an error when accessed from outside. Hence, the message is encapsulated.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Closures&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We can create a function inside a function, and the inner function will be able to access the local variable defined inside the outer function. This is called closure.&lt;/p&gt;

&lt;p&gt;Note: A closure is created every time a function is declared.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function messageFunc(){
    const message = "Hey there!"
    const displayMessage = function(){
        console.log(message);
    }
    displayMessage();
}

messageFunc();

console.log("Message from outside: ",message);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, the output says that creating a closure helps encapsulate and restrict the data access only to inside the function.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>DESTRUCTURING IN JAVASCRIPT</title>
      <dc:creator>Lamina Busayo</dc:creator>
      <pubDate>Tue, 13 Jun 2023 10:53:55 +0000</pubDate>
      <link>https://dev.to/busayo2605/destructuring-in-javascript-ha5</link>
      <guid>https://dev.to/busayo2605/destructuring-in-javascript-ha5</guid>
      <description>&lt;p&gt;Destructuring in JavaScript is unpacking data structures, array and objects to easily access the values inside them. Destructuring help to extract values from arrays and objects into distinct variable so we don’t have to navigate through large arrays or objects to get the values that are needed. JavaScript allows us to destructure arrays, objects and function parameters.&lt;br&gt;
JavaScript allows for 3 different modes of destructuring which includes;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Array destructuring&lt;/li&gt;
&lt;li&gt;Object destructuring&lt;/li&gt;
&lt;li&gt;Function parameter destructuring&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;ARRAY DESTRUCTURING&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Array in JavaScript is a collection of elements a numerical index can access. The element in an array can be of any data type Including numbers, strings and even array of objects. Arrays are created using square brackets and individual elements can be accessed using their index which starts at 0.&lt;br&gt;
The basic syntax for array destructuring uses square brackets on the left-hand side. For example, to extract the first element of an array and assign it to a variable. The following code can be used&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Const&lt;/span&gt; &lt;span class="nx"&gt;Persons&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Sam&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Faith&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;John&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Gift&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;Const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;firstPerson&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Persons&lt;/span&gt;
&lt;span class="nx"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstPerson&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;RESULT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Sam&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Multiple elements can also be extracted from an array and assigned to multiple variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Const&lt;/span&gt; &lt;span class="nx"&gt;Persons&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Sam&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Faith&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;John&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Gift&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;Const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;firstPerson&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondPerson&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;thirdPerson&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Persons&lt;/span&gt;
&lt;span class="nx"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstPerson&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondPerson&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;thirdPerson&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;RESULT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Sam&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Faith&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;John&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;OBJECT DESTRUCTURING&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Objects in JavaScript are collections of key-value pairs. Objects are created using curly braces.&lt;br&gt;
The basic syntax for object destructuring uses curly braces on the left-hand side of an assignment statement and the object on the right-hand side. For example, to extract a property called “name” from an object and assign it to  a variable, the following code can be used&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Const&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;John&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Apple&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;Const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; 
&lt;span class="nx"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;RESULT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;John&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code creates an object called Person that has 2 properties (ie name and brand), we use object destructuring to extract the name property of the object and assign it to a variable called ‘name’&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;FUNCTION PARAMETER DESTRUCTURING&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript allows us to extract values from an object or array passed as parameter to a function. Destructuring parameters is quite straight forward as you only need to use the destructuring syntax inside the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;. You are &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years)
} 
greet({name: “Peter”, age:25})
RESULT: Hello, Peter. You are 25 years
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;BENEFITS OF DESTRUCTURING&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Improved performance&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When values are extracted from an array or object, new variable is created that reference the value. This is particularly useful when working with large data, as it allows you to work with a smaller portion of the data, which can be more efficient.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Concise and readable code&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Destructuring allows you to write more concise and readable code. When working with large data, destructuring allows you to focus on the essential parts of the data rather than getting lost in details.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Easier handling of nested properties&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;With destructuring, you can extract values with a single line of code, making it easier to work with nested properties.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Better handling of function parameters&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Destructuring can be used to extract values from function parameters. This can be particularly useful when working with complex data structures, as it allows you to focus on the specific values you need without navigating through the entire data structure.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;CONCLUSION&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In conclusion, destructuring in JavaScript makes it easy for developer to navigate through large data.&lt;/p&gt;

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