<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Ankit Kumar]]></title><description><![CDATA[🚀 Backend Developer | Python 🐍 | Django | Node.js | writes code with one hand and drinks tea with the other.]]></description><link>https://blog.ankitdevelops.in</link><generator>RSS for Node</generator><lastBuildDate>Fri, 10 Apr 2026 13:32:07 GMT</lastBuildDate><atom:link href="https://blog.ankitdevelops.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Vertical vs Horizontal Scaling]]></title><description><![CDATA[As our application grows, we need to scale our system to handle increasing traffic and load. Two popular strategies to achieve this are vertical scaling and horizontal scaling.
Vertical Scaling

Vertical Scaling is also referred to as scale up. In th...]]></description><link>https://blog.ankitdevelops.in/vertical-vs-horizontal-scaling</link><guid isPermaLink="true">https://blog.ankitdevelops.in/vertical-vs-horizontal-scaling</guid><category><![CDATA[#TechInterviews #InterviewPreparation #DBMS #OperatingSystems #ComputerNetworks #Algorithms #DataStructures #ProblemSolving #SoftwareDevelopment #ProgrammingLanguages #SystemDesign #OOP #DatabaseManagement #WebDevelopment #MobileDevelopment #BackendDevelopment #FrontendDevelopment #SoftwareEngineering #CareerDevelopment #JobInterviews #InterviewTips]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Tue, 22 Oct 2024 17:31:20 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/PSpf_XgOM5w/upload/f59c459329aa2845ef380a695b62255d.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As our application grows, we need to scale our system to handle increasing traffic and load. Two popular strategies to achieve this are <strong>vertical scaling</strong> and <strong>horizontal scaling</strong>.</p>
<h2 id="heading-vertical-scaling">Vertical Scaling</h2>
<ul>
<li><p>Vertical Scaling is also referred to as <code>scale up</code>. In this, we add more resources to the existing machine like adding more RAM, faster CPU, etc.</p>
</li>
<li><p>It's good for simple architecture because there is a limit, we can't add unlimit amount of RAM or CPU to a single machine.</p>
</li>
<li><p>It does not have a failover if it does down our complete application goes down.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729617939570/05347e92-0a1d-438e-84c0-40561eb9a78f.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-horizontal-scaling"><strong>Horizontal Scaling</strong></h2>
<ul>
<li><p>Horizontal scaling is also referred to as <code>scale out</code>. In this, we add more machines to spread the load across multiple machines.</p>
</li>
<li><p>It's considered effective for large-scale systems.</p>
</li>
<li><p>It does have a failover if a single machine goes down, It will not affect our application much because it can be distributed across other machines.</p>
</li>
<li><p>We will be required to add a load balancer to distribute the load across different machines.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729617961374/6c529f21-5b9c-4162-b524-bdbbe47b332f.png" alt class="image--center mx-auto" /></p>
<p>Choosing between vertical and horizontal scaling depends on the needs of your application. Vertical scaling is easy but limited, while horizontal scaling offers more flexibility and resilience for large-scale systems.</p>
]]></content:encoded></item><item><title><![CDATA[How Python code is executed.]]></title><description><![CDATA[When you run a Python script, several steps take place behind the scenes to execute your code. Here’s a brief overview of how it works:

Compilation to Bytecode

When we execute a Python script, the Python interpreter compiles the source code into by...]]></description><link>https://blog.ankitdevelops.in/how-python-code-is-executed</link><guid isPermaLink="true">https://blog.ankitdevelops.in/how-python-code-is-executed</guid><category><![CDATA[Python]]></category><category><![CDATA[Python 3]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Mon, 21 Oct 2024 16:40:35 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1729648834432/ab3198c1-e951-473d-9371-c3c24cf66290.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When you run a Python script, several steps take place behind the scenes to execute your code. Here’s a brief overview of how it works:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729528662407/345cb71d-d2ba-4230-afc4-2bbae45d7528.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-compilation-to-bytecode">Compilation to Bytecode</h3>
<ul>
<li><p>When we execute a Python script, the Python interpreter compiles the source code into bytecode.</p>
</li>
<li><p>Bytecode is a lower-level representation of the code, stored in a <code>.pyc</code> file within the <code>__pycache__</code> directory.</p>
</li>
<li><p>During this process, all syntax errors are checked.</p>
</li>
<li><p>Bytecode instructions are platform-independent and remain the same across different operating systems.</p>
</li>
<li><p>We can manually generate the bytecode of file Python file using this command. <code>python3 -m py_compile</code> <a target="_blank" href="http://main.py"><code>main.py</code></a></p>
</li>
<li><p>We can also execute the generated bytecode.`python3 __pycache__/main.cpython-310.pyc`</p>
</li>
</ul>
<h3 id="heading-execution-by-python-virtual-machine-pvm">Execution by Python Virtual Machine (PVM)</h3>
<ul>
<li><p>Once the source code is compiled into bytecode, the Python Virtual Machine (PVM) interprets these bytecode instructions and performs the corresponding actions.</p>
</li>
<li><p>PVM is a part of Python Interpreter.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Django authenticate and login methods]]></title><description><![CDATA[Django comes with a robust authentication system that handles both authentication and authorization. In this post let's talk about two of its methods authenticate() and login().
authenticate()
The authenticate method is used to verify the user's cred...]]></description><link>https://blog.ankitdevelops.in/django-authenticate-and-login-methods</link><guid isPermaLink="true">https://blog.ankitdevelops.in/django-authenticate-and-login-methods</guid><category><![CDATA[Python]]></category><category><![CDATA[Django]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Fri, 20 Oct 2023 15:10:59 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1697814595751/2982b236-5ff1-4e9e-af61-304f77b47718.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Django comes with a robust authentication system that handles both authentication and authorization. In this post let's talk about two of its methods <code>authenticate()</code> and <code>login()</code>.</p>
<p><code>authenticate()</code></p>
<p>The authenticate method is used to verify the user's credentials against the authentication backends defined in the project. it takes the user's credentials as arguments and returns user object if the credentials are valid and if the credentials are invalid then it returns <code>None</code></p>
<p><code>login()</code></p>
<p>The <code>login()</code> function is used to create user sessions and log them in. It takes a <code>HttpRequest</code> object and a User object saves the user's ID in the session using Django’s session framework and sets a session cookie in the user's browser, allowing them to remain authenticated across different pages and requests.</p>
<p>Example to create a login_view using both methods</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.contrib.auth <span class="hljs-keyword">import</span> authenticate, login
<span class="hljs-keyword">from</span> django.http <span class="hljs-keyword">import</span> HttpResponse
<span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render
<span class="hljs-keyword">from</span> django.contrib.auth.models <span class="hljs-keyword">import</span> User

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">login_view</span>(<span class="hljs-params">request</span>):</span>
    <span class="hljs-keyword">if</span> request.method == <span class="hljs-string">'POST'</span>:
        username = request.POST[<span class="hljs-string">'username'</span>]
        password = request.POST[<span class="hljs-string">'password'</span>]
        user = authenticate(request, username=username, password=password)
        <span class="hljs-keyword">if</span> user <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
            login(request, user)
            <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">"Logged in successfully"</span>)
        <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">"Authentication failed. Please try again."</span>)

    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">'login.html'</span>)
</code></pre>
]]></content:encoded></item><item><title><![CDATA[Working with Python Dictionary]]></title><description><![CDATA[A dictionary is one of the many built-in datatypes of Python, A dictionary allows us to store data in key-value pairs, where the key must be unique. it is also known as “associative memories” or “associative arrays” in some other programming language...]]></description><link>https://blog.ankitdevelops.in/working-with-python-dictionary</link><guid isPermaLink="true">https://blog.ankitdevelops.in/working-with-python-dictionary</guid><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Sun, 15 Oct 2023 06:03:30 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1696950837680/1ce21164-200a-4626-9b29-e26cbdb6ae81.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>A dictionary is one of the many built-in datatypes of Python, A dictionary allows us to store data in key-value pairs, where the key must be unique. it is also known as “associative memories” or “associative arrays” in some other programming languages. A dictionary contains comma-separated <code>key:value</code> pairs enclosed within {}. In a dictionary, the different keys can have the same values but the key must be unique.</p>
<h2 id="heading-creating-dictionary">Creating Dictionary</h2>
<p><strong>Using Curly Braces</strong> <code>{}</code><strong>:</strong></p>
<p>To create a dictionary in Python we use a pair of curly brackets <code>{}</code> and store comma-separated key-value pairs in it.</p>
<pre><code class="lang-python"><span class="hljs-comment"># empty dictionary</span>
my_dict = {}
print(type(my_dict)) <span class="hljs-comment"># &lt;class 'dict'&gt;</span>

<span class="hljs-comment"># storing value in dictionary</span>
my_dict = {<span class="hljs-string">'key1'</span>: <span class="hljs-string">'value1'</span>, <span class="hljs-string">'key2'</span>: <span class="hljs-string">'value2'</span>, <span class="hljs-string">'key3'</span>: <span class="hljs-string">'value3'</span>}
print(my_dict) <span class="hljs-comment"># my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}</span>
</code></pre>
<p><strong>Note:</strong></p>
<p>Dictionaries in Python must have unique keys. When the same key is provided multiple times, the last one overwrites the previous one.</p>
<pre><code class="lang-python">user = {
    <span class="hljs-string">'name'</span>: <span class="hljs-string">'John'</span>,
    <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>,
    <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>,
    <span class="hljs-string">"name"</span>: <span class="hljs-string">"James"</span> <span class="hljs-comment"># using the same key</span>
}
print(user) <span class="hljs-comment"># {'name': 'James', 'age': 25, 'email': 'john.doe@example.com'}</span>
</code></pre>
<p><strong>Using</strong> <code>dict()</code> <strong>Constructor:</strong></p>
<p>We can also use <code>dict()</code> constructor to create a dictionary, and to store some data in it. We can pass key-value pairs as arguments or as a list of tuples. Take a look at the examples below.</p>
<pre><code class="lang-python">my_dict = dict()
print(type(my_dict)) <span class="hljs-comment"># &lt;class 'dict'&gt;</span>

<span class="hljs-comment"># stroing data as arguments</span>
my_dict = dict(key1=<span class="hljs-string">'value1'</span>, key2=<span class="hljs-string">'value2'</span>, key3=<span class="hljs-string">'value3'</span>)
print(my_dict) <span class="hljs-comment"># {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}</span>

<span class="hljs-comment"># stroring data as list of tuples</span>
my_dict = dict([(<span class="hljs-string">'key1'</span>, <span class="hljs-string">'value1'</span>), (<span class="hljs-string">'key2'</span>, <span class="hljs-string">'value2'</span>), (<span class="hljs-string">'key3'</span>, <span class="hljs-string">'value3'</span>)])
print(my_dict) <span class="hljs-comment"># {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}</span>
</code></pre>
<h2 id="heading-accessing-data-from-dictionary">Accessing Data from Dictionary</h2>
<h3 id="heading-using-square-brackets"><strong>Using</strong> Square <strong>Brackets:</strong></h3>
<p>We can use square brackets with keys to access its corresponding values. If the key is not present in the dictionary a <code>KeyError</code> will be raised.</p>
<pre><code class="lang-python">user = {<span class="hljs-string">'name'</span>: <span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>, <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>}

print(user[<span class="hljs-string">'name'</span>]) <span class="hljs-comment"># John Doe</span>
print(user[<span class="hljs-string">'age'</span>]) <span class="hljs-comment"># 25</span>
print(user[<span class="hljs-string">'email'</span>]) <span class="hljs-comment"># john.doe@example.com</span>

print(user[<span class="hljs-string">'phone'</span>]) <span class="hljs-comment"># KeyError</span>
</code></pre>
<h3 id="heading-using-get-method"><strong>Using</strong> <code>get()</code> <strong>method:</strong></h3>
<p>The <code>get()</code> method allows us to retrieve the value associated with the key, if the key is not present in the dictionary then it returns <code>None</code> or the <code>default_value</code> if it is specified.</p>
<pre><code class="lang-python">user = {<span class="hljs-string">'name'</span>: <span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>, <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>}

print(user.get(<span class="hljs-string">'name'</span>)) <span class="hljs-comment"># John Doe</span>
print(user.get(<span class="hljs-string">'age'</span>)) <span class="hljs-comment"># 25</span>
print(user.get(<span class="hljs-string">'email'</span>)) <span class="hljs-comment"># john.doe@example.com</span>

print(user.get(<span class="hljs-string">'phone'</span>)) <span class="hljs-comment"># None</span>
print(user.get(<span class="hljs-string">'phone'</span>,<span class="hljs-string">'No Phone Number'</span>)) <span class="hljs-comment"># No Phone Number</span>
</code></pre>
<h3 id="heading-accessing-keys-of-the-dictionary"><strong>Accessing keys of the dictionary:</strong></h3>
<p>To get all the keys of a dictionary we use <code>dict.keys()</code> method. It returns a view object that contains all the keys of the dictionary. This view object is a dynamic view, meaning it reflects any changes made to the dictionary, If we add or remove an item to the dictionary, the view will automatically update. we can also use <code>dict.keys()</code> method to iterate over the keys of a dictionary using a for loop or we can use it to test the membership of any key in the dictionary. Take a look at the example below to learn about different actions we can perform using the <code>dict.keys()</code> method.</p>
<pre><code class="lang-python">user = {<span class="hljs-string">'name'</span>: <span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>, <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>}
print(user.keys()) <span class="hljs-comment"># dict_keys(['name', 'age', 'email'])</span>

<span class="hljs-comment"># adding a new key to the dictionary</span>
user[<span class="hljs-string">'phone'</span>] = <span class="hljs-string">'9898989898'</span>
print(user.keys())  <span class="hljs-comment"># dict_keys(['name', 'age', 'email', 'phone'])</span>

<span class="hljs-comment"># Iterating over the keys of a dictionary</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> user.keys():
  print(i)

<span class="hljs-comment"># Testing membership</span>
<span class="hljs-keyword">if</span> <span class="hljs-string">'phone'</span> <span class="hljs-keyword">in</span> user.keys():
  print(<span class="hljs-string">"True"</span>)
<span class="hljs-keyword">else</span>:
  print(<span class="hljs-string">"False"</span>)

<span class="hljs-comment"># converting the view object to a static list</span>
static_list = list(user.keys())
print(static_list)  <span class="hljs-comment"># ['name', 'age', 'email', 'phone']</span>
</code></pre>
<h3 id="heading-accessing-values-of-the-dictionary">Accessing Values of the dictionary.</h3>
<p>To access all the values of the dictionary we use the <code>dict.values()</code> method. It returns a view object that contains all the values of the dictionary. This view object is a dynamic view, meaning it reflects any changes made to the dictionary, If we add or remove an item to the dictionary, the view will automatically update. The view object can be iterated and it also supports membership testing, which means we can use <code>dict.values()</code> the method to iterate over the values of the dictionary or to test the membership of some value in the dictionary. Take a look at the example below.</p>
<pre><code class="lang-python">user = {<span class="hljs-string">'name'</span>: <span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>, <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>}
print(user.values())  <span class="hljs-comment"># dict_values(['John Doe', 25, 'john.doe@example.com'])</span>

<span class="hljs-comment"># adding a new item to the dictionary</span>
user[<span class="hljs-string">'phone'</span>] = <span class="hljs-string">'9898989898'</span>
print(user.values())  <span class="hljs-comment"># dict_values(['John Doe', 25, 'john.doe@example.com', '9898989898'])</span>

<span class="hljs-comment"># Iterating over the keys of a dictionary</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> user.values():
  print(i)

<span class="hljs-comment"># Testing membership</span>
<span class="hljs-keyword">if</span> <span class="hljs-number">25</span> <span class="hljs-keyword">in</span> user.values():
  print(<span class="hljs-string">"True"</span>)
<span class="hljs-keyword">else</span>:
  print(<span class="hljs-string">"False"</span>)

<span class="hljs-comment"># converting the view object to a static list</span>
static_list = list(user.values())
print(static_list)  <span class="hljs-comment"># ['name', 'age', 'email', 'phone']</span>
</code></pre>
<h3 id="heading-accessing-items-from-the-dictionary">Accessing items from the dictionary</h3>
<p>we use <code>dict.items</code> method to access all the items from the dictionary. It returns a view object that contains a list of tuples with key-value pairs of the dictionary. The view object is the same as we learned in <code>dict.keys() or dict.values()</code> methods. Take a look at the example below.</p>
<pre><code class="lang-python">user = {<span class="hljs-string">'name'</span>: <span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>, <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>}
print(user.items())  <span class="hljs-comment"># dict_items([('name', 'John Doe'), ('age', 25), ('email', 'john.doe@example.com')])</span>

<span class="hljs-comment"># adding a new item to the dictionary</span>
user[<span class="hljs-string">'phone'</span>] = <span class="hljs-string">'9898989898'</span>
print(user.items())  <span class="hljs-comment"># dict_items([('name', 'John Doe'), ('age', 25), ('email', 'john.doe@example.com'), ('phone', '9898989898')])</span>

<span class="hljs-comment"># Iterating over the keys of a dictionary</span>
<span class="hljs-keyword">for</span> key, value <span class="hljs-keyword">in</span> user.items():
  print(key, value)

<span class="hljs-comment"># Testing membership</span>
<span class="hljs-keyword">if</span> (<span class="hljs-string">'age'</span>, <span class="hljs-number">25</span>) <span class="hljs-keyword">in</span> user.items():
  print(<span class="hljs-string">"True"</span>)
<span class="hljs-keyword">else</span>:
  print(<span class="hljs-string">"False"</span>)

<span class="hljs-comment"># conveting the object view to static list</span>
static_list = list(user.items())
print(static_list)  <span class="hljs-comment"># [('name', 'John Doe'), ('age', 25), ('email', 'john.doe@example.com'), ('phone', '9898989898')]</span>
</code></pre>
<h2 id="heading-modifying-dictionary">Modifying Dictionary</h2>
<p>While working with a dictionary, we will need to add new data, delete existing data, or update the existing data from the dictionary, in this section of the article we will learn about this.</p>
<h3 id="heading-adding-new-data">Adding new data</h3>
<p>We can add new key-value pairs to the dictionary either by using square brackets or by using <code>dict.update()</code> method let's take a look at them one by one. Any new <code>key:value</code> pair added to the dictionary will be added at the end of the dictionary since the dictionary maintains the order of its items.</p>
<p><strong>Using Square Brackets:</strong></p>
<blockquote>
<p>Syntax :-</p>
<p>dict[key] = value</p>
</blockquote>
<pre><code class="lang-python">user = {<span class="hljs-string">'name'</span>: <span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>, <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>}

<span class="hljs-comment"># adding new key value pair</span>
user[<span class="hljs-string">'phone'</span>] =<span class="hljs-string">'9898989898'</span>
user[<span class="hljs-string">'username'</span>] = <span class="hljs-string">'john'</span>

print(user) <span class="hljs-comment"># {'name': 'John Doe', 'age': 25, 'email': 'john.doe@example.com', 'phone': '9898989898', 'username': 'john'}</span>
</code></pre>
<p><strong>Using the update method:</strong></p>
<p>The <code>update()</code> method is used to update a dictionary with elements from another dictionary or from an iterable of key-value pairs (as tuples or other iterable of length two) or keyword arguments. This method modifies the dictionary in place, adding new key-value pairs or if the key already exits it updates the value of it.</p>
<pre><code class="lang-python">user = {<span class="hljs-string">'name'</span>: <span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>, <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>}

<span class="hljs-comment"># updating with another dictionary </span>
extra_data = {<span class="hljs-string">"phone"</span>: <span class="hljs-string">"989898989"</span>, <span class="hljs-string">"username"</span>: <span class="hljs-string">"john"</span>}
user.update(extra_data)
print(user)  <span class="hljs-comment"># {'name': 'John Doe', 'age': 25, 'email': 'john.doe@example.com', 'phone': '989898989', 'username': 'john'}</span>

<span class="hljs-comment"># updating with list of tuples</span>
extra_data = [(<span class="hljs-string">"phone"</span>, <span class="hljs-string">"989898989"</span>), (<span class="hljs-string">"username"</span>, <span class="hljs-string">"john"</span>)]
user.update(extra_data)
print(user)  <span class="hljs-comment"># {'name': 'John Doe', 'age': 25, 'email': 'john.doe@example.com', 'phone': '989898989', 'username': 'john'}</span>

<span class="hljs-comment"># updating with keyword arguments</span>
user.update(phone=<span class="hljs-string">"989898989"</span>, username=<span class="hljs-string">"john"</span>)
print(user)  <span class="hljs-comment"># {'name': 'John Doe', 'age': 25, 'email': 'john.doe@example.com', 'phone': '989898989', 'username': 'john'}</span>

<span class="hljs-comment"># updating values of a key </span>
user.update(phone=<span class="hljs-string">"989898989"</span>, username=<span class="hljs-string">"john"</span>,age=<span class="hljs-number">30</span>)
print(user)  <span class="hljs-comment"># {'name': 'John Doe', 'age': 30, 'email': 'john.doe@example.com', 'phone': '989898989', 'username': 'john'}</span>
</code></pre>
<h3 id="heading-removing-data-from-the-dictionary">Removing data from the dictionary</h3>
<p><strong>Using del statement</strong></p>
<p><code>del</code> statement is used to remove key-value pairs from the dictionary. It takes a key and removes the key and its corresponding value from the list. if the key is not present in the dictionary it raises <code>KeyError</code>. We can even delete the complete dictionary using the <code>del</code> statement.</p>
<pre><code class="lang-python">user = {<span class="hljs-string">'name'</span>: <span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>, <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>}

<span class="hljs-comment"># removing item with del statement</span>
<span class="hljs-keyword">del</span> user[<span class="hljs-string">'name'</span>]
print(user) <span class="hljs-comment"># {'age': 25, 'email': 'john.doe@example.com'}</span>

<span class="hljs-comment"># trying to delete item that doesn't exists in the dictionary</span>
<span class="hljs-keyword">del</span> user[<span class="hljs-string">'phone'</span>] <span class="hljs-comment"># KeyError: 'phone'</span>

<span class="hljs-comment"># removing the complete dictionary</span>
<span class="hljs-keyword">del</span> user
</code></pre>
<p><strong>Using the</strong> <code>pop()</code> <strong>method:</strong></p>
<p><code>pop()</code> methods accept a key and optional default value, it removes the specified key from the dictionary and returns its value. If the key is not in the dictionary it returns the default value if it is specified else it raises a key error.</p>
<blockquote>
<p>Syntax:</p>
<p>dict.pop(key,default_value)</p>
<ul>
<li><p><code>key</code>: The key whose associated value is to be retrieved and removed.</p>
</li>
<li><p><code>default</code> (optional): If the key is not found, the default value is returned (if provided). If not provided and the key is not found, a <code>KeyError</code> is raised.</p>
</li>
</ul>
</blockquote>
<pre><code class="lang-python">user = {<span class="hljs-string">'name'</span>: <span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>, <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>}

<span class="hljs-comment"># using pop method to remove item from the list</span>
returned_value = user.pop(<span class="hljs-string">'name'</span>)
print(returned_value) <span class="hljs-comment"># John Doe</span>

<span class="hljs-comment"># using default value</span>
returned_value = user.pop(<span class="hljs-string">'phone'</span>,<span class="hljs-string">"Invalid key"</span>)
print(returned_value) <span class="hljs-comment"># invalid key</span>

<span class="hljs-comment"># using invalid key without default value</span>
returned_value = user.pop(<span class="hljs-string">'username'</span>)
print(returned_value) <span class="hljs-comment"># KeyError</span>

print(user) <span class="hljs-comment"># {'age': 25, 'email': 'john.doe@example.com'}</span>
</code></pre>
<p><strong>Using</strong> <code>dict.clear()</code> <strong>method:</strong></p>
<p><code>dict.clear()</code> method is used to remove all the key-value pairs from the dictionary and returns <code>None</code>.</p>
<pre><code class="lang-python">user = {<span class="hljs-string">'name'</span>: <span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>, <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>}

<span class="hljs-comment"># using clear method</span>
returned_value = user.clear()
print(returned_value) <span class="hljs-comment"># None</span>
print(user) <span class="hljs-comment"># {}</span>
</code></pre>
<h2 id="heading-using-loops-with-dictionary">Using Loops with Dictionary</h2>
<p><strong>Note:</strong></p>
<p>The dictionary preserves the insertion order, which means the order in which items are added to a dictionary will be maintained, when we iterate through the items of a dictionary, they will be in the same order in which they were inserted. However, we can't access the items in the dictionary using their position because items in the dictionary are key-indexed not position-indexed.</p>
<p><strong>Using for loop with dictionary:</strong></p>
<p>When we loop over the dictionary it implicitly loops over its keys.</p>
<pre><code class="lang-python">user = {<span class="hljs-string">'name'</span>: <span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>, <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>}

<span class="hljs-keyword">for</span> val <span class="hljs-keyword">in</span> user:
  print(val)

<span class="hljs-comment"># Output</span>
<span class="hljs-string">'''
name
age
email
'''</span>
</code></pre>
<p>but we can use these keys to access their corresponding values.</p>
<pre><code class="lang-python">user = {<span class="hljs-string">'name'</span>: <span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>, <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>}

<span class="hljs-keyword">for</span> val <span class="hljs-keyword">in</span> user:
  print(user[val])

<span class="hljs-comment"># Output</span>
<span class="hljs-string">'''
John Doe
25
john.doe@example.com
'''</span>
</code></pre>
<p><strong>Looping through keys or values.</strong></p>
<p>As we have seen in this article earlier we can user <code>dict.values()</code> to get values of the list and <code>dict.keys()</code> to get list of keys.</p>
<pre><code class="lang-python">user = {<span class="hljs-string">'name'</span>: <span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>, <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>}

<span class="hljs-comment"># looping through keys</span>
<span class="hljs-keyword">for</span> key <span class="hljs-keyword">in</span> user.keys():
  print(key)

<span class="hljs-comment"># looping through values</span>
<span class="hljs-keyword">for</span> val <span class="hljs-keyword">in</span> user.values():
  print(val)
</code></pre>
<p><strong>Looping through key-value pairs</strong></p>
<p>We can use <code>dict.items()</code> to loop through the key-value pairs of the dictionary. <code>dict.items()</code> method returns tuples of key-value pairs of a dictionary.</p>
<pre><code class="lang-python">user = {<span class="hljs-string">'name'</span>: <span class="hljs-string">'John Doe'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">25</span>, <span class="hljs-string">'email'</span>: <span class="hljs-string">'john.doe@example.com'</span>}

<span class="hljs-comment"># looping through key-value pairs</span>
<span class="hljs-keyword">for</span> key,value <span class="hljs-keyword">in</span> user.items():
  print(<span class="hljs-string">f"Key: <span class="hljs-subst">{key}</span>, Value: <span class="hljs-subst">{value}</span>"</span>)
</code></pre>
<h2 id="heading-dictionary-comprehension">Dictionary Comprehension</h2>
<p><strong>Comprehensions:</strong></p>
<p>Comprehension in Python is syntactic sugar, it allows us compact and shorter syntax to create new sequences (set, list, dictionary). Take a look at a simple example below.</p>
<blockquote>
<p>Syntax:</p>
<p>dict = {key: value for i in iterable}</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-comment"># Create a dictionary with squares of numbers from 1 to 5</span>
my_dict = {num: num**<span class="hljs-number">2</span> <span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> range(<span class="hljs-number">1</span>, <span class="hljs-number">6</span>)}
print(my_dict) <span class="hljs-comment"># {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}</span>
</code></pre>
<h2 id="heading-choosing-the-correct-key">Choosing the correct key</h2>
<p>We know that keys in the dictionary must be unique, but what are the possible candidates for keys?</p>
<p>Any immutable type can serve as a key, such as a number, string, or tuple. If you use a tuple as a key, it must only contain immutable types such as a number, string, or another tuple.</p>
<p>As another way to put it, keys need to be hashable to be used in dictionaries. Hashable means that the value should have a fixed hash value that doesn't change during its lifetime.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Using numbers as keys</span>
number_dict = {<span class="hljs-number">1</span>: <span class="hljs-string">'One'</span>, <span class="hljs-number">2</span>: <span class="hljs-string">'Two'</span>, <span class="hljs-number">3</span>: <span class="hljs-string">'Three'</span>}

<span class="hljs-comment"># Using strings as keys</span>
string_dict = {<span class="hljs-string">'apple'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'banana'</span>: <span class="hljs-number">2</span>, <span class="hljs-string">'orange'</span>: <span class="hljs-number">3</span>}

<span class="hljs-comment"># Using a tuple with immutable elements as a key</span>
tuple_dict = {(<span class="hljs-string">'John'</span>, <span class="hljs-number">25</span>): <span class="hljs-string">'Student'</span>, (<span class="hljs-string">'Alice'</span>, <span class="hljs-number">30</span>): <span class="hljs-string">'Teacher'</span>, (<span class="hljs-string">'Bob'</span>, <span class="hljs-number">22</span>): <span class="hljs-string">'Engineer'</span>}
</code></pre>
<h2 id="heading-dictionary-methods">Dictionary methods</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Method</strong></td><td><strong>Description</strong></td></tr>
</thead>
<tbody>
<tr>
<td><code>dict.keys()</code></td><td>Returns keys view</td></tr>
<tr>
<td><code>dict.values()</code></td><td>Returns values view</td></tr>
<tr>
<td><code>dict.items()</code></td><td>Returns key-value pairs view</td></tr>
<tr>
<td><code>dict.get(key, default)</code></td><td>Returns value for key, or default if key not found</td></tr>
<tr>
<td><code>dict.pop(key, default)</code></td><td>Removes and returns value for key</td></tr>
<tr>
<td><code>dict.popitem()</code></td><td>Removes and returns the last key-value pair</td></tr>
<tr>
<td><code>dict.update(other_dict)</code></td><td>Updates the dictionary with another dictionary</td></tr>
<tr>
<td><code>dict.clear()</code></td><td>Removes all items from the dictionary</td></tr>
</tbody>
</table>
</div><h2 id="heading-summary">Summary:-</h2>
<ul>
<li><p>Python dictionaries are collections that store data in key-value pairs, enclosed in curly braces <code>{}</code>.</p>
</li>
<li><p>Keys in a dictionary must be unique, and dictionaries are mutable (modifiable).</p>
</li>
<li><p>Creating dictionaries can be done using curly braces <code>{}</code> or the <code>dict()</code> constructor.</p>
</li>
<li><p>Accessing data in a dictionary is possible through square brackets or the <code>get()</code> method.</p>
</li>
<li><p>Modifying dictionaries involves adding, removing, or updating key-value pairs.</p>
</li>
<li><p>Keys in dictionaries must be hashable, and any immutable type can be used as a key.</p>
</li>
<li><p>Loops can be used with dictionaries, implicitly iterating over keys or explicitly using methods like <code>keys()</code>, <code>values()</code>, or <code>items()</code>.</p>
</li>
<li><p>Dictionary comprehension provides a concise syntax for creating dictionaries in a single line.</p>
</li>
<li><p>The dictionary preserves the insertion order, which means the order in which items are added to a dictionary will be maintained, when we iterate through the items of a dictionary, they will be in the same order in which they were inserted. However, we can't access the items in the dictionary using their position because items in the dictionary are key-indexed not position-indexed.</p>
</li>
<li><p>using <code>len(dict)</code> function with a dictionary will return the number of <code>key:value</code> pairs in the dictionary.</p>
</li>
<li><p>using <code>sorted(dict)</code> function will return a sorted list of keys.</p>
</li>
<li><p>using <code>max(dict)</code> function will return the maximum key in the dictionary.</p>
</li>
<li><p>using <code>min(dict)</code> function will return the minimum key in the dictionary.</p>
</li>
<li><p>using <code>sum(dict)</code> function will return the sum of all the keys if the keys are numbers.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Getting started with Express: Building your first app]]></title><description><![CDATA[In this article, we are going to create our first express app, to get started with this article make sure the following tools are installed in your system.

NodeJs -

To verify whether nodejs is installed or not, open your terminal and run the comman...]]></description><link>https://blog.ankitdevelops.in/getting-started-with-express-building-your-first-app</link><guid isPermaLink="true">https://blog.ankitdevelops.in/getting-started-with-express-building-your-first-app</guid><category><![CDATA[iwritecode]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Express]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Sun, 02 Apr 2023 07:51:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1680421769352/a4c281f6-4a35-4173-8097-726d0375fd1a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, we are going to create our first express app, to get started with this article make sure the following tools are installed in your system.</p>
<ul>
<li><p>NodeJs -</p>
<ul>
<li>To verify whether nodejs is installed or not, open your terminal and run the command <code>node -v</code> and <code>npm -v</code> this should give any version number.</li>
</ul>
</li>
<li><p>Vscode</p>
</li>
<li><p>postman</p>
</li>
</ul>
<h2 id="heading-step-1-basic-setup">Step 1: Basic Setup</h2>
<p>First, we will create a project folder, we can do that using the GUI, but let's try to create that by using the terminal.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1680416205176/2b2f38bd-b647-434d-8b64-3718f161b44f.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p><code>mkdir helloExpress</code> - <code>mkdir</code> command is used to create a new directory. so this command will create a new directory <code>helloExpress</code></p>
</li>
<li><p><code>cd helloExpress/</code> - <code>cd</code> is used to change the directory. So this command will change the current directory to the <code>helloExpress</code> directory.</p>
</li>
<li><p><code>code .</code> this command will open vscode in the current directory.</p>
</li>
</ul>
<p>Now the next step is to create our <code>package.json</code> file. <code>package.json</code> file is a simple JSON file located in the project root directory. This file contains important information about the project like name, version, dependencies, and other configuration options. Every nodejs project contains a p<code>ackage.json</code> file.</p>
<p>To create the <code>package.json</code> file open the in-built terminal of vs code, you can use <code>ctrl + ` </code> . Now we are going to write the command <code>npm init</code> . This will initialize a new node js project and ask you to fill in a bunch of information about the project. You can give that information or keep pressing enter to either keep them empty or default, and if you don't want to keep pressing enter then you can use <code>npm init -y</code> this will skip all the question prompts and set them to default.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1680417121036/737efead-f875-4148-9023-3e7a2d975c9c.png" alt class="image--center mx-auto" /></p>
<p>This is how our <code>package.json</code> file looks currently.</p>
<pre><code class="lang-javascript">{
  <span class="hljs-string">"name"</span>: <span class="hljs-string">"helloexpress"</span>,
  <span class="hljs-string">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-string">"description"</span>: <span class="hljs-string">""</span>,
  <span class="hljs-string">"main"</span>: <span class="hljs-string">"index.js"</span>,
  <span class="hljs-string">"scripts"</span>: {
    <span class="hljs-string">"test"</span>: <span class="hljs-string">"echo \"Error: no test specified\" &amp;&amp; exit 1"</span>
  },
  <span class="hljs-string">"author"</span>: <span class="hljs-string">""</span>,
  <span class="hljs-string">"license"</span>: <span class="hljs-string">"ISC"</span>
}
</code></pre>
<p>Now the next step is to create an entry point for our application, we will create a new <code>index.js</code> file in the project root directory.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// index.js file</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello Express"</span>);
</code></pre>
<p>Currently, We only have a simple console message in our <code>index.js</code> file.</p>
<p>Now we will add <code>script</code> in our <code>package.json</code> file.</p>
<pre><code class="lang-javascript">{
  <span class="hljs-string">"name"</span>: <span class="hljs-string">"helloexpress"</span>,
  <span class="hljs-string">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-string">"description"</span>: <span class="hljs-string">""</span>,
  <span class="hljs-string">"main"</span>: <span class="hljs-string">"index.js"</span>,
  <span class="hljs-string">"scripts"</span>: {
    <span class="hljs-string">"start"</span>: <span class="hljs-string">"node index.js"</span>
  },
  <span class="hljs-string">"author"</span>: <span class="hljs-string">""</span>,
  <span class="hljs-string">"license"</span>: <span class="hljs-string">"ISC"</span>
}
</code></pre>
<p>Now when we run the command <code>npm start</code> then it should display <code>Hello Express</code> message in the console.</p>
<h2 id="heading-step-2-express-and-nodemon-installation">Step 2: Express and nodemon Installation.</h2>
<p>In this section of the article, we are going to install Express and nodemon and do a basic setup of express and nodemon.</p>
<p><strong><em><mark>installing Express</mark></em></strong></p>
<blockquote>
<p>Express is a popular and widely-used web application framework for Node.js. It provides a set of features and tools for building web applications and APIs.</p>
</blockquote>
<p>To install Express run command <code>npm install express</code> in the vscode in-built terminal.</p>
<p><strong><em><mark>Installing nodemon</mark></em></strong></p>
<blockquote>
<p>Nodemon is a command-line tool that monitosr for any changes in your source and automatically restart your server.</p>
</blockquote>
<p>To install nodemon run the command <code>npm i nodemon -D</code> . nodemon is used for development and testing purposes, that's why we install it as <code>devDependencies</code> for installing any package as <code>devDependencies</code> we use <code>-D</code> flag.</p>
<p>Now <code>nodemon</code> is installed, let's modify the script in <code>package.json</code> file.</p>
<pre><code class="lang-javascript">{
  <span class="hljs-string">"name"</span>: <span class="hljs-string">"helloexpress"</span>,
  <span class="hljs-string">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-string">"description"</span>: <span class="hljs-string">""</span>,
  <span class="hljs-string">"main"</span>: <span class="hljs-string">"index.js"</span>,
  <span class="hljs-string">"scripts"</span>: {
    <span class="hljs-string">"start"</span>: <span class="hljs-string">"nodemon index.js"</span>
  },
  <span class="hljs-string">"author"</span>: <span class="hljs-string">""</span>,
  <span class="hljs-string">"license"</span>: <span class="hljs-string">"ISC"</span>,
  <span class="hljs-string">"dependencies"</span>: {
    <span class="hljs-string">"express"</span>: <span class="hljs-string">"^4.18.2"</span>
  },
  <span class="hljs-string">"devDependencies"</span>: {
    <span class="hljs-string">"nodemon"</span>: <span class="hljs-string">"^2.0.22"</span>
  }
}
</code></pre>
<p>Now try doing any changes in <code>index.js</code> file and save it, and your server will start automatically.</p>
<h2 id="heading-step-3-express-setup">Step 3: Express Setup</h2>
<p>Let's move to <code>index.js</code> file and start setting up our app. Let's add the following code to your <code>index.js</code> file and then I will try to explain you each and every line.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">"express"</span>);
<span class="hljs-keyword">const</span> app = express();
<span class="hljs-keyword">const</span> port = <span class="hljs-number">3000</span>;

app.get(<span class="hljs-string">"/"</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> {
  res.send(<span class="hljs-string">"Hello Express"</span>);
});

app.listen(port, <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`listening on port <span class="hljs-subst">${port}</span>`</span>);
});
</code></pre>
<p><code>const express = require("express");</code></p>
<p>This line imports the Express module and assigns it to <code>express</code> variable. This allows us to use the functionality provided by the Express framework.</p>
<p><code>const app = express();</code></p>
<p>This creates an instance of the Express application and assigns it to <code>app</code> variable. This is the main object that we will use to configure and interact with our application.</p>
<p><code>const port = 3000;</code></p>
<p>This line sets our port to 3000, our server will listen for any incoming request on this port only.</p>
<pre><code class="lang-javascript">app.get(<span class="hljs-string">"/"</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> {
  res.send(<span class="hljs-string">"Hello Express"</span>);
});
</code></pre>
<p>The above line sets up a GET route on the root URL ("/") of our server. It accepts two arguments <code>PATH</code> and a <code>Handler function</code>. When a user makes a request to the root URL, the function passed as the second argument will be executed. In this case, the function sends the string "Hello World!" as the response.</p>
<pre><code class="lang-javascript">app.listen(port, <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`listening on port <span class="hljs-subst">${port}</span>`</span>);
});
</code></pre>
<p>This line starts the server and listens for incoming requests on the specified port. When the server starts listening for any incoming request, the function passed as the second argument will be executed, which logs a message to the console indicating that the server is listening on the specified port.</p>
<p>Now if you open <code>localhost:3000</code> in your browser, it will show you <code>Hello Express</code> message.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1680420191993/0bf3b0a8-ca86-4caf-b6c3-41c8a9f2a318.png" alt class="image--center mx-auto" /></p>
<p>While working with Express we usually don't send this type of one-line message usually we send JSON data which might be coming from the database, but currently, we don't have that type of knowledge so let's create some static JSON data and try to send that as a response. This is the data we are going to send as response.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> data = [
  {
    <span class="hljs-attr">id</span>: <span class="hljs-number">1</span>,
    <span class="hljs-attr">first_name</span>: <span class="hljs-string">"Leonid"</span>,
    <span class="hljs-attr">last_name</span>: <span class="hljs-string">"Bardsley"</span>,
    <span class="hljs-attr">email</span>: <span class="hljs-string">"lbardsley0@yahoo.co.jp"</span>,
    <span class="hljs-attr">gender</span>: <span class="hljs-string">"Male"</span>,
  },
  {
    <span class="hljs-attr">id</span>: <span class="hljs-number">2</span>,
    <span class="hljs-attr">first_name</span>: <span class="hljs-string">"Tybalt"</span>,
    <span class="hljs-attr">last_name</span>: <span class="hljs-string">"Antushev"</span>,
    <span class="hljs-attr">email</span>: <span class="hljs-string">"tantushev1@storify.com"</span>,
    <span class="hljs-attr">gender</span>: <span class="hljs-string">"Male"</span>,
  },
  {
    <span class="hljs-attr">id</span>: <span class="hljs-number">3</span>,
    <span class="hljs-attr">first_name</span>: <span class="hljs-string">"Bethena"</span>,
    <span class="hljs-attr">last_name</span>: <span class="hljs-string">"Worgen"</span>,
    <span class="hljs-attr">email</span>: <span class="hljs-string">"bworgen2@virginia.edu"</span>,
    <span class="hljs-attr">gender</span>: <span class="hljs-string">"Female"</span>,
  },
  {
    <span class="hljs-attr">id</span>: <span class="hljs-number">4</span>,
    <span class="hljs-attr">first_name</span>: <span class="hljs-string">"Bartram"</span>,
    <span class="hljs-attr">last_name</span>: <span class="hljs-string">"Matts"</span>,
    <span class="hljs-attr">email</span>: <span class="hljs-string">"bmatts3@myspace.com"</span>,
    <span class="hljs-attr">gender</span>: <span class="hljs-string">"Non-binary"</span>,
  },
  {
    <span class="hljs-attr">id</span>: <span class="hljs-number">5</span>,
    <span class="hljs-attr">first_name</span>: <span class="hljs-string">"Marice"</span>,
    <span class="hljs-attr">last_name</span>: <span class="hljs-string">"Pavlenkov"</span>,
    <span class="hljs-attr">email</span>: <span class="hljs-string">"mpavlenkov4@tmall.com"</span>,
    <span class="hljs-attr">gender</span>: <span class="hljs-string">"Bigender"</span>,
  },
];
</code></pre>
<p>Next, we need to add a new GET Route and add the following line.</p>
<pre><code class="lang-javascript">app.get(<span class="hljs-string">"/users"</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> {
  res.status(<span class="hljs-number">200</span>).json(data);
});
</code></pre>
<p>The above line sets up a new GET route on the root URL ("/users") of our server. It accepts two arguments <code>PATH</code> and a <code>Handler function</code>. When a user makes a request to the URL, the function passed as the second argument will be executed. In this case, the function sends a JSON string as the response. The <code>status()</code> method sets the HTTP status code to 200, indicating that the request was successful, and the <code>json()</code> method converts the <code>data</code> variable into a JSON string and sends it as the response body.</p>
<p>Now if you go to `<a target="_blank" href="http://localhost:3000/users">localhost:3000/users</a>` in your browser or postman you can see the data as result and in postman, you can also see the status code.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1680421403820/a603fb15-1ba3-4ebf-bdf6-7256bfeda09c.png" alt class="image--center mx-auto" /></p>
<p>That's it you have successfully created your first express app.</p>
<p><a target="_blank" href="https://github.com/ankitdevelops/first-express-app">Github Repo</a></p>
]]></content:encoded></item><item><title><![CDATA[Hoisting In JavaScript?]]></title><description><![CDATA[Hosting is a concept in JavaScript in which you can use variables and functions before they are declared. Before moving ahead with this article I would suggest you read this article. In this article, I talked about how javascript code is executed.

I...]]></description><link>https://blog.ankitdevelops.in/hoisting-in-javascript</link><guid isPermaLink="true">https://blog.ankitdevelops.in/hoisting-in-javascript</guid><category><![CDATA[iwritecode]]></category><category><![CDATA[ineuron]]></category><category><![CDATA[@hiteshchoudharylco]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Mon, 20 Feb 2023 12:31:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1675944272720/37c388f8-3d28-41b7-bb1b-edc445ea8ba0.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hosting is a concept in JavaScript in which you can use variables and functions before they are declared. Before moving ahead with this article I would suggest you read <a target="_blank" href="https://ankitdevelops.hashnode.dev/how-javascript-works">this</a> article. In this article, I talked about how javascript code is executed.</p>
<blockquote>
<p>In Short:</p>
<p>When a script is loaded a <strong>Global Execution Context</strong> is created and every time a function is invoked a <strong>Function Execution Context</strong> is created and it's deleted after the function is executed. When we run the any program, javascript will create a new execution context. In the memory-creation phase, it will start allocating memory to variables and functions. In the case of variables a special value <code>undefined</code> will be stored and for the function, the whole function code will be stored.</p>
</blockquote>
<h2 id="heading-hoisting-of-variables">Hoisting of Variables.</h2>
<p>Take a look at the example below.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> name = <span class="hljs-string">"Ankit"</span>;
<span class="hljs-built_in">console</span>.log(name);
</code></pre>
<p>We all know how the above program is going to execute, in line 1 value <code>Ankit</code> will be assigned to a variable <code>name</code> and in line 2 the value stored in the variable name will be displayed in the console. But what if we change the order of the line, take a look at the example below.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(name);
<span class="hljs-keyword">var</span> name = <span class="hljs-string">"Ankit"</span>;

<span class="hljs-comment">// output in the console</span>
<span class="hljs-literal">undefined</span>
</code></pre>
<p>This time it logs <code>undefined</code> to the console, but why?</p>
<p>As I have talked about in <a target="_blank" href="https://ankitdevelops.hashnode.dev/how-javascript-works">this</a> article or above, javascript allocates memory to variables and functions before executing them, when it allocates memory to variables it stores the special keyword <code>undefined</code> to it. That's why it showing undefined.</p>
<p>Take a look at the below example</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(name);
<span class="hljs-comment">// var name = "Ankit";</span>
</code></pre>
<p>In the above example, we are trying to console.log the name without declaring it, this will give an error. Notice the <code>name is not defined</code> error, <code>name is not defined</code> error and <code>undefined</code> error is two different things. We get this error because the name variable has not been declared, and no memory has been allocated to the name variable.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675942164547/654ce968-eec2-438d-b673-f1d7b6e1acdc.png" alt class="image--center mx-auto" /></p>
<p>Please note that the variable declared with only <code>var</code> keyword is hoisted, a variable declared with <code>let</code> and <code>const</code> are not hoisted.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675942448009/d4c382cd-ca11-4321-9428-1343eecd1b8b.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675942468684/f70d61fd-cc0f-417b-815a-a2a75aad99a1.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-hoisting-of-functions">Hoisting of Functions</h2>
<p>Take a look at the below code example.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello <span class="hljs-subst">${name}</span>`</span>);
}
sayHello(<span class="hljs-string">"Rahul"</span>); <span class="hljs-comment">// Hello Rahul</span>
</code></pre>
<p>This is how we declare a function in any programming language when we call the <code>sayHello</code> function with a name argument it's going to display the Hello message, what if we call the function before declaring it? Let's see</p>
<pre><code class="lang-javascript">sayHello(<span class="hljs-string">"Rahul"</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello <span class="hljs-subst">${name}</span>`</span>);
}
</code></pre>
<p>When we execute the above code it also displays the <code>Hello Rahul</code> message in the console, but why and How? Let's understand this.</p>
<p>As we have talked javascript allocates memory to variables and functions before executing them, in the case of variables it stores the special keyword <code>undefined</code> , and in the case of functions, it stores a copy of the function. so the function is already in the memory that's why we can invoke the function before declaring it. This doesn't mean that you can invoke a function without declaring it.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675943332644/c01f7a3f-f372-427e-a9a3-1e1a010f68f9.png" alt class="image--center mx-auto" /></p>
<p>Please note that only the function declaration is hoisted, not the function expression.</p>
<blockquote>
<p>In Javascript we can also define functions as values and store them in a variable, this is known as a function expression.</p>
</blockquote>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675943516933/5061b40e-c522-4d27-a03d-2e5b8a5e5428.png" alt class="image--center mx-auto" /></p>
<p>Arrow functions are also not hoisted.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675943594788/e22d44e1-6e74-471a-927c-07d8edd8e421.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-conclusion">Conclusion 🙋‍♀️🙋‍♂️</h2>
<p>Thanks for taking the time to read this tutorial. I hope you found it helpful and informative. Please share it with your friends and followers on social media if you enjoyed reading it.</p>
<p>Always keep this two line in mind.</p>
<ul>
<li><p>function declaration are scanned and made available</p>
</li>
<li><p>variable declaration are scanned and made undefined.</p>
</li>
</ul>
<p><strong>Read</strong> <a target="_blank" href="https://ankitdevelops.hashnode.dev/how-javascript-works">this</a> <strong>before reading this article.</strong></p>
]]></content:encoded></item><item><title><![CDATA[How JavaScript Works]]></title><description><![CDATA[JavaScript is a synchronous, single-threaded language, it means javascript can run only one command at a time and in a specific order. For a javascript code to be executed, we need something called a javascript engine, so what is this javascript engi...]]></description><link>https://blog.ankitdevelops.in/how-javascript-works</link><guid isPermaLink="true">https://blog.ankitdevelops.in/how-javascript-works</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[iwritecode]]></category><category><![CDATA[learning]]></category><category><![CDATA[ineuron]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Tue, 07 Feb 2023 07:35:12 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1675754825231/1c2d9b24-2be1-45c3-a5e8-78cc01eadb0d.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>JavaScript is a synchronous, single-threaded language, it means javascript can run only one command at a time and in a specific order. For a javascript code to be executed, we need something called a javascript engine, so what is this javascript engine <code>A JavaScript engine is the component of a web browser that executes JavaScript code.</code> Every Web browser has its own javascript engine e.g chrome has its v8 engine, Firefox has SpiderMonkey, Safari has Javascript Core Webkit, etc.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> name = <span class="hljs-string">"Ankit"</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">displayMessage</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-keyword">var</span> message = <span class="hljs-string">"Hello "</span> + name;
  <span class="hljs-keyword">return</span> message;
}

<span class="hljs-keyword">var</span> message1 = displayMessage(name);
<span class="hljs-keyword">var</span> message2 = displayMessage(<span class="hljs-string">"Rahul"</span>);

<span class="hljs-built_in">console</span>.log(message1);
<span class="hljs-built_in">console</span>.log(message2);
</code></pre>
<p>Take a look at the above code, we all know the working of the above code, we call the <code>displayMessage</code> function with an argument and it will return a message and later the message will be displayed in the console, but let's try to understand how JavaScript will execute this code.</p>
<h2 id="heading-execution-context">Execution Context.</h2>
<p>You might be thinking what the heck is Execution Context? let me tell you one thing <mark>everything in javascript happens inside the execution context. </mark> Execution context refers to an environment in which javascript code is executed. There are two types of Execution Context in javascript.</p>
<ul>
<li><p><strong>Global Execution Context:-</strong> It is the default execution context When a script file is loaded in the javascript engine then the Global Execution Context is created.</p>
</li>
<li><p>Function Execution Context:- Whenever a function is invoked in javascript a new function execution context is created.</p>
</li>
</ul>
<p>Execution Context is created in two phases.</p>
<ul>
<li><p><strong>Memory Creation Phase:-</strong> in this phase memory is allocated variables and functions.</p>
</li>
<li><p><strong>Code Execution Phase:- In</strong> this phase, the JavaScript engine starts executing the code from top to bottom. Variables and functions declared in the creation phase are now accessible and can be used to execute the code.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675750509159/aa098d72-7bf7-4b45-bf9d-9da8fe3a2f29.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-how-code-is-executed-in-javascript">How code is Executed in JavaScript.</h2>
<p>Now we know about the execution context, Take a look at the below code and try to understand how the code will be executed by javascript.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> name = <span class="hljs-string">"Ankit"</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">displayMessage</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-keyword">var</span> message = <span class="hljs-string">"Hello "</span> + name;
  <span class="hljs-keyword">return</span> message;
}

<span class="hljs-keyword">var</span> message1 = displayMessage(name);
<span class="hljs-keyword">var</span> message2 = displayMessage(<span class="hljs-string">"Rahul"</span>);

<span class="hljs-built_in">console</span>.log(message1);
<span class="hljs-built_in">console</span>.log(message2);
</code></pre>
<p><strong>Phase1: Memory Creation Phase</strong></p>
<p>When we run the above program, javascript will create a new execution context. In the memory-creation phase, it will start allocating memory to variables and functions. In the case of variables a special value <code>undefined</code> will be stored and for the function, the whole function code will be stored.</p>
<p>This is how the execution context will look in the memory creation phase.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675750934031/c75ce9ae-037f-4e21-b80c-aa24805f292d.png" alt class="image--center mx-auto" /></p>
<p><strong>Phase2: Code Execution Phase</strong></p>
<p>In the code Execution phase, javascript will again go through the code line by line and start executing. So it starts executing and it encounters <code>name="Ankit"</code> now it replaces <code>undefined</code> with <code>Ankit</code> and moves to the next line. The next line is the function definition, a function is only executed when it is invoked, so nothing to execute here. Moving to the next line we have <code>var message1 = displayMessage(name);</code> a function is being invoked, so a new function execution context will be created. In this new execution context, there will be two phases. The first one of memory creation phase and the second is the code execution phase, In the memory execution phase memory will be allocated to variables and functions.</p>
<p>Notice that in the <code>displayMessage</code> function we have a <code>name</code> parameter, this parameter will also be treated as a variable. so in the memory creation phase, this is how our execution context looks.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675752348211/066ee46f-e274-48ce-96f6-08080ce52436.png" alt class="image--center mx-auto" /></p>
<p>Code Execution Phase</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675752950908/fe1e5186-fedb-4f97-ad08-49dbfdae7192.png" alt class="image--center mx-auto" /></p>
<p>Again in the code execution phase <code>undefined</code> in name will be replaced with the argument passed and <code>undefined</code> in the message will be replaced by <code>Hello Ankit</code>. After that, it encounters <code>return</code> keyword, and when js encounters <code>return message</code> it moves the control back to the place where the function was invoked with the returned value. In our case, the control will move back to <code>message</code> the variable and this function execution contested will be deleted, and undefined in the message1 will be replaced by the returned value.</p>
<p>Representing Deletion of the function execution context.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675753068925/2722c861-31e1-4250-b818-d05dedb3e85e.png" alt class="image--center mx-auto" /></p>
<p>Now our control will move to the next line i.e <code>message2</code> here also <code>displayMessage</code> function is being invoked so the same process will be repeated again, and a new function execution context will be created, and the memory creation phase and code execution phase will be repeated.</p>
<p><strong>Memory Creation Phase</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675754687667/e127cf3d-bc9f-41af-a229-99b4a7884202.png" alt class="image--center mx-auto" /></p>
<p><strong>Code Execution Phase</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675753664344/1730e730-9724-4c1b-9e16-3b80a3fd27ce.png" alt class="image--center mx-auto" /></p>
<p>After the code execution phase, the control is moved to the <code>message2</code> execution context is also deleted. <code>undefined</code> in <code>message2</code> is replaced by returned value. After that js encounters <code>console.log</code> statements and it displays both messages to the console.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675754204225/4f162e0c-9e13-48b3-80cc-aaaf9c2484d4.png" alt class="image--center mx-auto" /></p>
<p>Once the whole program is executed the global execution context is also deleted.</p>
<h2 id="heading-call-stack">Call Stack</h2>
<p>As you might have noticed that there are lots of execution contexts need to be created and deleted, so to keep track of execution contexts javascript uses Call Stack. It operates on the Last In First Out (LIFO) principle, meaning that the last function called is the first one to be executed. When Ever a new execution context is created it is pushed into the call stack and when the execution context is deleted it's popped out of the stack. It also maintains the order of execution contexts. It is also known as an 'Execution Context Stack', 'Runtime Stack', or 'Machine Stack'.</p>
<h2 id="heading-conclusion">Conclusion 🙋‍♀️🙋‍♂️</h2>
<p>Thanks for taking the time to read this tutorial. I hope you found it helpful and informative. Please share it with your friends and followers on social media if you enjoyed reading it.</p>
]]></content:encoded></item><item><title><![CDATA[Higher-Order Functions in JavaScript?]]></title><description><![CDATA[We know that functions in javascript are First Class Citizens, so we can assign functions to variables as values, pass a function as an argument to another function and return a function from another function.
// function as values
const var1 = funct...]]></description><link>https://blog.ankitdevelops.in/higher-order-functions-in-javascript</link><guid isPermaLink="true">https://blog.ankitdevelops.in/higher-order-functions-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[iwritecode]]></category><category><![CDATA[ineuron]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Mon, 30 Jan 2023 09:54:31 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1675072283463/471c23a5-3138-4661-911a-43268686f327.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>We know that functions in javascript are First Class Citizens, so we can <strong><em>assign functions to variables as values,</em> <em>pass a function as an argument to another function</em></strong> and <strong><em>return a function from another function.</em></strong></p>
<pre><code class="lang-javascript"><span class="hljs-comment">// function as values</span>
<span class="hljs-keyword">const</span> var1 = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello There"</span>);
};

var1(); <span class="hljs-comment">// -&gt; Hello There</span>

<span class="hljs-comment">// returning function from function</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">returnFunction</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"I am being return as function"</span>);
  };
}

<span class="hljs-keyword">let</span> var2 = returnFunction(); <span class="hljs-comment">//stores the returned function</span>
var2(); <span class="hljs-comment">// I am being return as function</span>

<span class="hljs-comment">// function as argument</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">square</span>(<span class="hljs-params">num, callback</span>) </span>{
  <span class="hljs-keyword">var</span> result = num * num;
  callback(result);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">IAmCallback</span>(<span class="hljs-params">result</span>) </span>{
  <span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// 25</span>
}

<span class="hljs-comment">// passing function as an argument</span>
square(<span class="hljs-number">5</span>, IAmCallback); <span class="hljs-comment">// -&gt; 25</span>
</code></pre>
<p>You might be thinking this article is about higher-order functions then why are we reading this, here is the answer.</p>
<blockquote>
<p>Higher-order functions are function which takes function as an argument or returns a function. It is called "higher-order" because it operates on functions, rather than just values. This allows for greater flexibility and abstraction in coding.</p>
</blockquote>
<h3 id="heading-example-1">Example 1:</h3>
<p>Let's under the Higher-order function with an Example. Take a look at the code example below.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>];

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">square</span>(<span class="hljs-params">numbers</span>) </span>{
  <span class="hljs-keyword">let</span> squaredNumbers = [];
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; numbers.length; i++) {
    squaredNumbers.push(numbers[i] * numbers[i]);
  }
  <span class="hljs-keyword">return</span> squaredNumbers;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">cube</span>(<span class="hljs-params">numbers</span>) </span>{
  <span class="hljs-keyword">let</span> cubedNumbers = [];
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; numbers.length; i++) {
    cubedNumbers.push(numbers[i] * numbers[i] * numbers[i]);
  }
  <span class="hljs-keyword">return</span> cubedNumbers;
}

<span class="hljs-built_in">console</span>.log(square(numbers)); <span class="hljs-comment">// -&gt; [4, 16, 25, 36, 49, 64, 81, 100];</span>

<span class="hljs-built_in">console</span>.log(cube(numbers)); <span class="hljs-comment">// -&gt; [ 8,   64, 125, 216,  343, 512, 729, 1000 8,   64, 125, 216,  343, 512, 729, 1000 ]</span>
</code></pre>
<p>In the above example, there are two functions <code>square</code> and <code>cube</code> which accepts a list of numbers and returns a new array with squared and cubed values. as you can see in the above example we are repeating code a lot. let's fix this with a higher-order function.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>];

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">square</span>(<span class="hljs-params">number</span>) </span>{
  <span class="hljs-keyword">return</span> number * number;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">cube</span>(<span class="hljs-params">number</span>) </span>{
  <span class="hljs-keyword">return</span> number * number;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">calculate</span>(<span class="hljs-params">numbers, operation</span>) </span>{
  <span class="hljs-keyword">const</span> output = [];
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; numbers.length; i++) {
    output.push(operation(numbers[i]));
  }
  <span class="hljs-keyword">return</span> output;
}
<span class="hljs-comment">// calling the calculate function with square operation</span>
<span class="hljs-keyword">let</span> squaredNumbers = calculate(numbers, square);
<span class="hljs-built_in">console</span>.log(squaredNumbers); <span class="hljs-comment">// -&gt; [4, 16, 25, 36, 49, 64, 81, 100];</span>

<span class="hljs-comment">// calling the calcualte function with cube operation</span>
<span class="hljs-keyword">let</span> cubedNumbers = calculate(numbers, cube);
<span class="hljs-built_in">console</span>.log(cubedNumbers); <span class="hljs-comment">// -&gt; [ 8,   64, 125, 216,  343, 512, 729, 1000 8,   64, 125, 216,  343, 512, 729, 1000 ]</span>
</code></pre>
<p>In the above example, we have three different function <code>square</code> <code>cube</code> and <code>calculate</code> the <code>square</code> and <code>cube</code> the function accepts a number as an argument and returns the square and cube of that number respectively. Things to notice here are the <code>calculate</code> function, which is the higher order function, which accepts a list of numbers, and a function as arguments. It performs the operation we pass as a function on every element of the array. Now we can also create any number of new operations and pass them to calculate functions. Take a look at the example below.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">doubles</span>(<span class="hljs-params">number</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-number">2</span> * number;
}

<span class="hljs-keyword">let</span> doubledNumbers = calculate(numbers, doubles);
<span class="hljs-built_in">console</span>.log(doubledNumbers); <span class="hljs-comment">//  [4, 8, 10, 12, 14, 16, 18, 20];</span>
</code></pre>
<h3 id="heading-example-2">Example 2:</h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyBy</span>(<span class="hljs-params">factor</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">number</span>) </span>{
    <span class="hljs-keyword">return</span> number * factor;
  };
}

<span class="hljs-keyword">const</span> double = multiplyBy(<span class="hljs-number">2</span>);
<span class="hljs-built_in">console</span>.log(double(<span class="hljs-number">5</span>)); <span class="hljs-comment">// 10</span>
</code></pre>
<p>In this example, <code>multiplyBy</code> is a higher-order function that takes a factor and returns a new function. The returned function takes a number and returns the result of multiplying that number by the factor. We can then use the <code>double</code> constant, which is the result of calling <code>multiplyBy(2)</code>, to quickly double any number we pass to it.</p>
<h3 id="heading-examples-of-higher-order-function">Examples of Higher-Order Function.</h3>
<p>There are lots of in-built higher-order functions in Javascript, like <code>Array.map()</code>, <code>Array.filter()</code>, <code>Array.reduce()</code> <code>setTimeout()</code> etc. We will learn more about them later, but let's look at the example of <code>Array.map()</code>. This function takes a callback function as an argument and returns a new array containing the results of calling the callback function on each element of the original array.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>];
<span class="hljs-keyword">let</span> squaredNumbers = numbers.map(square);
<span class="hljs-built_in">console</span>.log(squaredNumbers);  <span class="hljs-comment">// -&gt;  [4, 8, 10, 12, 14, 16, 18, 20];</span>
</code></pre>
<h2 id="heading-conclusion">Conclusion 🙋‍♂️🙋‍♀️</h2>
<p>In conclusion, higher-order functions are a powerful and flexible tool in JavaScript that allows for greater abstraction and code reusability. They operate on functions, either by taking them as arguments or returning them as results and allow for dynamic and flexible behavior in code. Their use can lead to more concise and readable code, making them a valuable tool in any JavaScript programmer's toolkit.</p>
]]></content:encoded></item><item><title><![CDATA[Callback Function In JavaScript]]></title><description><![CDATA[A callback function is a function that is passed as an argument to another function and is executed after the outer function has been completed.
Why do we use the callback function?
we use the callback function because it allows for the execution of ...]]></description><link>https://blog.ankitdevelops.in/callback-function-in-javascript</link><guid isPermaLink="true">https://blog.ankitdevelops.in/callback-function-in-javascript</guid><category><![CDATA[#iNeuron #HiteshChaudhary #WebDev #Javascript #LCO #LearnCodeOnline  #LCO  #css #learncodeonline #cssselectors  @hiteshchoudharylco  code with hitesh choudhary]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[#iwritecode  #hiteshchoudhary  #LCO  #markdown #git  #github]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Sun, 29 Jan 2023 07:03:08 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1674975675450/0c57ce61-5895-4a9b-b876-7dc09182ae7b.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>A callback function is a function that is passed as an argument to another function and is executed after the outer function has been completed.</p>
<p><strong><em>Why do we use the callback function?</em></strong></p>
<p>we use the callback function because it allows for the execution of another function after the result of a previous function call has been received.</p>
<p>For example, consider a simple function that takes two arguments, a number and a callback function. The function calculates the square of the number, and then calls the callback function, passing the result as an argument.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// normal function</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">square</span>(<span class="hljs-params">num, callback</span>) </span>{
  <span class="hljs-keyword">var</span> result = num * num;
  callback(result);
}

<span class="hljs-comment">// callback function</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">iAmCallback</span>(<span class="hljs-params">result</span>) </span>{
  <span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// 25</span>
}

<span class="hljs-comment">// passing function as an argument</span>
square(<span class="hljs-number">5</span>, iAmCallback);
</code></pre>
<p>In the above example, there are two functions first one is <code>square</code> and the second is <code>iAmCallback</code> The <code>square</code> functions accept two arguments a number and a callback function, it calculates the square of the number and stores it in the result variable after that it calls another function and passes the result as an argument.</p>
<p>next, we have defined another function <code>iAmCallback</code> it accepts one argument and displays it in the console. and at last, we are calling the square function and passing number and <code>iAmCallback</code> function as an argument, here <code>iAmCallback</code> is a callback function that is being passed as an argument to another function that is <code>square</code> once the square finishes its execution it calls the callback function i.e <code>iAmCallback</code>. and the result gets displayed in the console.</p>
<h2 id="heading-example">Example:</h2>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">displayMessage</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"How are You?"</span>);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello <span class="hljs-subst">${name}</span>`</span>);
}

<span class="hljs-built_in">setTimeout</span>(displayMessage, <span class="hljs-number">2000</span>);
sayHello(<span class="hljs-string">"Ankit"</span>);
</code></pre>
<p>In the above example, we have two function <code>displayMessage</code> and <code>sayHello</code> and later we are calling both the function, but we are delaying the execution of <code>displayMessage</code> function with 2sec using <code>setTimeout</code>. so normally <code>displayMessage</code> should be executed first and then the <code>sayHello</code> should be executed, but here <code>displayMessage</code> is called after 2sec and in this meantime <code>sayHello</code> is executed, so <code>Hello Ankit</code> is displayed first in the console and after that <code>How are You</code> the message is displayed in the console. The <code>sayHello</code> the function does not wait for the <code>displayMessage</code> function to execute, but we can make <code>sayHello</code> to wait for <code>displayMessage</code> to execute using the callback function, let's look at the example below.</p>
<pre><code class="lang-javascript"> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">displayMessage</span>(<span class="hljs-params">name, callbackFunction</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"How are You?"</span>);
  callbackFunction(name);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello <span class="hljs-subst">${name}</span>`</span>);
}

<span class="hljs-built_in">setTimeout</span>(displayMessage, <span class="hljs-number">2000</span>, <span class="hljs-string">"Ankit"</span>, sayHello);
</code></pre>
<p>Now in the above example the <code>sayHello</code> function waits for the <code>displayMessage</code> function to finish its execution. So the message is displayed first in the console and then the <code>Hello Ankit</code> is displayed in the console.</p>
<h2 id="heading-conclusion">Conclusion 🙋‍♀️🙋‍♂️</h2>
<p>Callback functions are also often used in asynchronous programming, where a function is called, and the program continues to execute other code before the function has been completed. The callback function is then called when the function has finished executing, to handle the results.</p>
<p><strong><em>Here are a few examples where the callback function is used.</em></strong></p>
<ul>
<li><p>In JavaScript, callbacks are commonly used in event handling, such as when a button is clicked or a page finishes loading.</p>
</li>
<li><p>They can also be used in AJAX (Asynchronous JavaScript and XML) requests to handle the response from the server.</p>
</li>
<li><p>In database operations such as MongoDB driver for nodejs uses callback for handling the query results.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Functions in JavaScript]]></title><description><![CDATA[function in programming refers to a block of code that performs a specific task, functions allow the developer to break complex problems into smaller ones or long code into smaller manageable code. Functions can be reused, so it also reduces code dup...]]></description><link>https://blog.ankitdevelops.in/functions-in-javascript</link><guid isPermaLink="true">https://blog.ankitdevelops.in/functions-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webdev]]></category><category><![CDATA[ineuron]]></category><category><![CDATA[iwritecode]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Fri, 20 Jan 2023 13:51:59 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1675173008678/0519803b-3846-4e30-a125-b2d2628f9145.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>function in programming refers to a block of code that performs a specific task, functions allow the developer to break complex problems into smaller ones or long code into smaller manageable code. Functions can be reused, so it also reduces code duplication. Functions take input, process them and return a value, a function is only executed once they are called.</p>
<h2 id="heading-function-declaration"><strong>Function Declaration</strong></h2>
<p>In JavaScript function is defined using <code>function</code> keyword followed by the function name.</p>
<blockquote>
<p>Syntax:</p>
<p>function sayHello(){</p>
<p>// code to be executed</p>
<p>}</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello"</span>);
}
</code></pre>
<p>in the above example, we have declared a <code>sayHello</code> function, for any function to be executed we need to call that function.</p>
<p><strong><em>This is how we can call that</em></strong> <code>sayHello</code> <strong><em>function*</em></strong>.*</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// when we call the sayHello function Hello message will be displayed on the screen.</span>
sayHello(); <span class="hljs-comment">// -&gt; Hello</span>
</code></pre>
<p><strong><em><mark>Declaring another function with a parameter</mark></em></strong></p>
<p>We can also declare functions with parameters. A parameter in a function is a variable that is used to accept an argument when the function is called. It stores the arguments passed to the function so that its value can be used later. This is how we can declare a function in javascript with parameters.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// function declaration with parameter</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello <span class="hljs-subst">${name}</span>`</span>);
}

<span class="hljs-comment">// calling the function with an argument.</span>
sayHello(<span class="hljs-string">"Ankit"</span>); <span class="hljs-comment">// -&gt; Hello Ankit</span>
</code></pre>
<p>In the above example, we have used the same <code>sayHello</code> function, but this time it is declared with <code>name</code> parameter. When we call this function we need to pass an argument and the value of the argument will be stored inside the name and we can use it later. Here we are displaying a Hello message with the name passed as an argument.</p>
<blockquote>
<p><strong><em>parameter and arugment.</em></strong></p>
<p>A parameter is a variable that is declared in the function definition. It acts as a placeholder for the arguments that will be passed to the function when it is called. The parameter is used to receive the value of the argument within the function.</p>
<p>An argument, on the other hand, is the actual value passed to the function when it is called. The argument provides the input to the function, which is then stored in the corresponding parameter.</p>
<p>In the above example, while declaring the function name is the parameter and while calling the function we passed "Ankit" that is the arguments.</p>
</blockquote>
<p>A function can be declared with any number of parameters.</p>
<p><strong><em><mark>Returning a value from a function.</mark></em></strong></p>
<p>functions can also be used to return some values, <code>return</code> keyword is used to return some values from a function. when a function encounters the return statement then the execution of the function is stopped, and any code after <code>return</code> statement is not executed. Take a look at the example below.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">square</span>(<span class="hljs-params">number</span>) </span>{
  <span class="hljs-keyword">return</span> number * number;
}

<span class="hljs-comment">// calling the sqare function with 5 as argument and value function will return will be stored in the output variable.</span>
<span class="hljs-keyword">const</span> output = square(<span class="hljs-number">5</span>);
<span class="hljs-built_in">console</span>.log(output);
</code></pre>
<p>In the above example, we have declared a square function that accepts a number as an argument and returns the square of the number. next, we are calling the square function with 5 as an argument, and the value the function will return will be stored in the output variable later we are displaying the output in the console.</p>
<p><strong><em>Note:</em></strong></p>
<p>Function in javascript is hoisted, in short, it means that we can call a function before declaring them.</p>
<h2 id="heading-function-expression">Function Expression</h2>
<p>In Javascript we can also define functions as values and store them in a variable, this is known as a function expression. remember that function expression is not hoisted which means you can't use function expression before you create them.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> square = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">number</span>) </span>{
  <span class="hljs-keyword">return</span> number * number;
};

<span class="hljs-keyword">const</span> output = square(<span class="hljs-number">5</span>);
<span class="hljs-built_in">console</span>.log(output);
</code></pre>
<h2 id="heading-arrow-function">Arrow function</h2>
<p>Arrow functions were introduced in ES6. The Arrow function allows us to declare functions with shorter syntax. This is how we declare the arrow function.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> square = <span class="hljs-function">(<span class="hljs-params">number</span>) =&gt;</span> {
  <span class="hljs-keyword">return</span> number * number;
};

<span class="hljs-keyword">const</span> output = square(<span class="hljs-number">5</span>);
<span class="hljs-built_in">console</span>.log(output);
</code></pre>
<h2 id="heading-conclusion">Conclusion 🙋‍♀️🙋‍♂️</h2>
<p>That's all for this article, functions are a very important concept of any programming language, and knowledge about functions is definitely going to help you to become a better developer.</p>
]]></content:encoded></item><item><title><![CDATA[JavaScript Loops]]></title><description><![CDATA[loops are one of the fundamental concepts of any programming language, loops allow us to repeat blocks of code multiple times. When we want to repeat certain actions multiple times in our program we use loops. In JavaScript, there are several differe...]]></description><link>https://blog.ankitdevelops.in/javascript-loops</link><guid isPermaLink="true">https://blog.ankitdevelops.in/javascript-loops</guid><category><![CDATA[iwritecode]]></category><category><![CDATA[ineuron]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Wed, 18 Jan 2023 12:22:07 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1675081214661/ec8233a0-39cb-4634-98d5-8349f7848abc.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>loops are one of the fundamental concepts of any programming language, loops allow us to repeat blocks of code multiple times. When we want to repeat certain actions multiple times in our program we use loops. In JavaScript, there are several different types of loops and each is used to perform different types of action, In this article, we are going to learn about them.</p>
<h2 id="heading-for-loop">For Loop</h2>
<p>for is one of the most used loops, for loop repeats until the specified condition evaluates to false.</p>
<blockquote>
<p>Syntax:-</p>
<p>for (initialization; condition; incrementExpression) {</p>
<p>// code to be executed</p>
<p>} Initialization: The starting value for the loop counter is set.</p>
<p>Condition: The loop will continue to run as long as the condition is true.</p>
<p>incrementExpression: The counter is updated after each iteration of the loop.</p>
<p>Code: The code inside the loop will be executed as long as the condition is true.</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">5</span>; i++) {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello world"</span>);
}
</code></pre>
<p>In the above example, we have initialized <code>i=0</code> and our condition is <code>i&lt;5</code> and our increment expression <code>i++</code> it means <code>i = i+1</code>. Here loop will keep displaying <code>Hello World</code> message to the console until the value of i is less than 5 and each time it displays <code>Hello World</code> message to the console value of i is incremented by 1.</p>
<h2 id="heading-while-loop">While Loop</h2>
<blockquote>
<p>Syntax:-</p>
<p>while (condition) {</p>
<p>// code to be executed</p>
<p>}</p>
</blockquote>
<p>while loop executes the statement as long as the condition evaluates to true. It stops the execution of the code if the condition evaluates to true.</p>
<pre><code class="lang-javascript">i = <span class="hljs-number">0</span>;
<span class="hljs-keyword">while</span> (i &lt; <span class="hljs-number">5</span>) {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello World"</span>);
  i++;
}
</code></pre>
<p>In the above example, the loop will keep displaying <code>Hello World</code> message to the console, until the value of I, is less than 5, and each time it displays <code>Hello World</code> to the console the <code>i++</code> statement increases the value of I by 1.</p>
<h2 id="heading-do-while">do-while</h2>
<blockquote>
<p>Syntax:-</p>
<p>do {</p>
<p>// code to be executed</p>
<p>} while (condition);</p>
</blockquote>
<p>do...while loop also executes the statement as long as the condition evaluates to true. It stops the execution of the code if the condition evaluates to true. The main difference between <code>while</code> loop and <code>do...while</code> loop is that in <code>do... while</code> loop statement is executed once before checking the condition.</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>;
<span class="hljs-keyword">do</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello World"</span>);
  i++;
} <span class="hljs-keyword">while</span> (i &lt; <span class="hljs-number">5</span>);
</code></pre>
<h2 id="heading-for-in">for in</h2>
<blockquote>
<p>for (variable in object) {</p>
<p>// code to be executed for each property</p>
<p>}</p>
</blockquote>
<p>The "for in" loop in JavaScript is used to iterate over the properties of an object. Each iteration returns a key and we can use that key to access the values of the object.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> person = {
  <span class="hljs-attr">name</span>: <span class="hljs-string">"John Kumar"</span>,
  <span class="hljs-attr">age</span>: <span class="hljs-number">32</span>,
  <span class="hljs-attr">occupation</span>: <span class="hljs-string">"Full Stack Developer"</span>,
  <span class="hljs-attr">skills</span>: [<span class="hljs-string">"Python"</span>, <span class="hljs-string">"Django"</span>, <span class="hljs-string">"JavaScript"</span>],
};

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-keyword">in</span> person) {
  <span class="hljs-built_in">console</span>.log(key); <span class="hljs-comment">// name age occupation skills</span>
  <span class="hljs-built_in">console</span>.log(person[key]); <span class="hljs-comment">// -&gt;  John Kumar 32 Full Stack Developer ['Python', 'Django', 'JavaScript' ]</span>
}
</code></pre>
<p>in the case of an array, it returns the index of the element.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>];

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-keyword">in</span> fruits) {
  <span class="hljs-built_in">console</span>.log(key);<span class="hljs-comment">// 0 1 2</span>
  <span class="hljs-built_in">console</span>.log(fruits[key]); <span class="hljs-comment">// -&gt; apple banana cherry</span>
}
</code></pre>
<h2 id="heading-for-of">for of</h2>
<blockquote>
<p>for (varaible of iterable) {</p>
<p>// code to be executed for each value</p>
<p>}</p>
</blockquote>
<p>The "for of" loop in JavaScript is used to iterate over the values of an iterable object such as an array or a string.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>];

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> fruits) {
  <span class="hljs-built_in">console</span>.log(value); <span class="hljs-comment">// -&gt; apple banana cherry</span>
}
</code></pre>
<p>In this example, the <code>fruits</code> array is iterated over using a "for of" loop. The loop variable <code>fruit</code> holds the value of each element in turn. The result is a list of all the elements in the <code>fruits</code> array.</p>
<h2 id="heading-break-and-continue">break and continue</h2>
<p>The break and continue statements are used in JavaScript to control loop flow.</p>
<p><strong><em>break</em></strong></p>
<p>The <code>break</code> statement terminates the loop and moves the control to the next iteration.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
  <span class="hljs-keyword">if</span> (i === <span class="hljs-number">5</span>) {
    <span class="hljs-keyword">break</span>;
  }
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`count is <span class="hljs-subst">${i}</span>`</span>);
}

<span class="hljs-comment">/*
count is 0
count is 1
count is 2
count is 3
count is 4
*/</span>
</code></pre>
<p><strong><em>continue</em></strong></p>
<p><strong>continue</strong> is used to skip the current iteration of a loop and continue with the next iteration. When a <strong>continue</strong> statement is encountered inside a loop, the current iteration is skipped and the program continues with the next iteration of the loop.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
  <span class="hljs-keyword">if</span> (i === <span class="hljs-number">5</span>) {
    <span class="hljs-keyword">continue</span>;
  }
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`count is <span class="hljs-subst">${i}</span>`</span>);
}
<span class="hljs-comment">/*
count is 0
count is 1
count is 2
count is 3
count is 4
count is 6
count is 7
count is 8
count is 9
*/</span>
</code></pre>
<p>In this example, the <code>continue</code> statement causes the program to skip 5, and moves the flow to the next iteration.</p>
<h2 id="heading-conclusion">Conclusion 🙋‍♂️🙋‍♀️</h2>
<p>That's it for this article.</p>
]]></content:encoded></item><item><title><![CDATA[Conditional Statement In JavaScript]]></title><description><![CDATA[We make decisions all the time "Should I learn DSA" or "Should I learn web development", Conditional statement allows representing such decision-making in JavaScript. Conditional Statements are used to perform different actions based on different con...]]></description><link>https://blog.ankitdevelops.in/conditional-statement-in-javascript</link><guid isPermaLink="true">https://blog.ankitdevelops.in/conditional-statement-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[#iNeuron #HiteshChaudhary #WebDev #Javascript #LCO #LearnCodeOnline  #LCO  #css #learncodeonline #cssselectors  @hiteshchoudharylco  code with hitesh choudhary]]></category><category><![CDATA[#iwritecode  #hiteshchoudhary  #LCO  #markdown #git  #github]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Tue, 17 Jan 2023 12:19:58 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1673957787177/ad45c69d-b2f4-47cf-88b1-a2bfbc4d4da4.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>We make decisions all the time "Should I learn DSA" or "Should I learn web development", Conditional statement allows representing such decision-making in JavaScript. Conditional Statements are used to perform different actions based on different conditions. In this article, we are going learn about different conditional operators available in JavaScript.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673954926504/b3ba9fb1-d152-460a-ae05-ca4dd2a06b46.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-if-else">if... else</h2>
<pre><code class="lang-javascript"><span class="hljs-comment">// Syntax</span>

<span class="hljs-keyword">if</span> (condition) {
  <span class="hljs-comment">// code to be executed when the condition is evaluated to truthy value</span>
} <span class="hljs-keyword">else</span> {
  <span class="hljs-comment">// code to be executed when the condition is falsy value</span>
}
</code></pre>
<p><code>if...else</code> statement is used when we want to execute different codes based on truthy and falsy values. If the condition is a truthy value then the if block code will be executed and if the condition is a falsy value the else block code will be executed.</p>
<p><strong>Let's take a look at an example</strong></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> age = <span class="hljs-number">18</span>;

<span class="hljs-keyword">if</span> (age &gt;= <span class="hljs-number">18</span>) {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"You can vote"</span>);
} <span class="hljs-keyword">else</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"You can't vote right now."</span>);
}

<span class="hljs-comment">// Output</span>
<span class="hljs-comment">// You can vote</span>
</code></pre>
<p>In the above code example the condition i.e <code>age &gt;= 18</code> evaluates to truthy value so the if block code is executed and it displays <code>You can vote</code> message in the console.</p>
<p><strong><em>Let's take a look at another example</em></strong></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> age = <span class="hljs-number">16</span>;

<span class="hljs-keyword">if</span> (age &gt;= <span class="hljs-number">18</span>) {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"You can vote"</span>);
} <span class="hljs-keyword">else</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"You can't vote right now."</span>);
}

<span class="hljs-comment">// Output</span>
<span class="hljs-comment">// You can't vote right now.</span>
</code></pre>
<p>In the above example, I have changed the age value from 18 to 16 so the condition i.e <code>age &gt;= 18</code> evaluates to false, so the else block of code is executed and it displays the <code>You can't vote right now</code> message to the console.</p>
<blockquote>
<p><strong>Truthy and falsy value</strong></p>
<p>In JavaScript, a truthy value is a value that is considered true when evaluated in a Boolean context. Examples of truthy values include any non-zero number, any non-empty string, and any object.</p>
<p>A falsy value, on the other hand, is a value that is considered false when evaluated in a Boolean context. Examples of falsy values include 0, the empty string, null, undefined, NaN, and the special keyword 'false'.</p>
<p>It's important to note that the only falsy values in javascript are: false, 0, "", null, undefined, and NaN. All other values are truthy.</p>
</blockquote>
<h2 id="heading-else-if">else if</h2>
<p>else if statement is used when we want to test for multiple conditions. Let's take a look at the example below to understand it better.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673955746073/d352cbcf-e977-4356-a5fc-5c70fbb346b0.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> age = <span class="hljs-number">16</span>;

<span class="hljs-keyword">if</span> (age &gt;= <span class="hljs-number">18</span>) {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"You can vote"</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (age &gt;= <span class="hljs-number">16</span>) {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"You are eligible for Driving License"</span>);
} <span class="hljs-keyword">else</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"You can't vote right now."</span>);
}
</code></pre>
<p>In the above example the condition next to the if statement evaluates to a falsy value, so the if block code is not executed, then the compiler moves to <code>else if</code> the condition next to <code>else if</code> evaluates to true, so the <code>else if</code> block of code is executed.</p>
<h2 id="heading-switch-statement">Switch Statement</h2>
<p>When we have multiple if checks in our code, then we can use the Switch statement to replace multiple if checks.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Syntax</span>

<span class="hljs-keyword">switch</span> (x) {
  <span class="hljs-keyword">case</span> <span class="hljs-string">"value1"</span>:
    <span class="hljs-comment">//   code to be executed</span>
    <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">case</span> <span class="hljs-string">"value2"</span>: <span class="hljs-comment">// if (x === 'value2')</span>
    <span class="hljs-comment">//   code to be executed</span>
    <span class="hljs-keyword">break</span>;

  <span class="hljs-keyword">default</span>:
    <span class="hljs-comment">//   code to be executed</span>
    <span class="hljs-keyword">break</span>;
}
</code></pre>
<p>In the above example, the value of x is checked with the value of different cases, once the value of <code>x</code> is strictly equal to any one of the different cases, the code block of that case is executed until it encounters the next break statement. If none of the cases is matched then the default block of code is Exected. Notice the <code>break</code> keyword, <code>break</code> keyword is used so that code execution stops and no new cases are checked, the compiler moves on and starts executing any code that appears after the <code>switch</code> statement.</p>
<p><strong><em>Example.</em></strong></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> color = <span class="hljs-string">"green"</span>;

<span class="hljs-keyword">switch</span> (color) {
  <span class="hljs-keyword">case</span> <span class="hljs-string">"red"</span>:
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"The color is red."</span>);
    <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">case</span> <span class="hljs-string">"green"</span>:
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"The color is green."</span>);
    <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">case</span> <span class="hljs-string">"blue"</span>:
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"The color is blue."</span>);
    <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">default</span>:
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"The color is not red, green, or blue."</span>);
}

<span class="hljs-comment">// Output</span>

<span class="hljs-comment">// The color is green.</span>
</code></pre>
<h2 id="heading-ternary-operator">Ternary Operator</h2>
<p>Ternary Operator takes three operands and returns values based on the condition.</p>
<blockquote>
<p>Syntax:</p>
<p>condition ? expression 1 : expression2</p>
<ul>
<li><em>if the condition is true then it returns expression1 and if the expression if false it returns expression2</em></li>
</ul>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> var1 = <span class="hljs-number">10</span>;
<span class="hljs-keyword">const</span> var2 = <span class="hljs-number">20</span>;

<span class="hljs-built_in">console</span>.log(var2 &gt; var1 ? <span class="hljs-string">"true"</span> : <span class="hljs-string">"false"</span>); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(var1 &gt; var2 ? <span class="hljs-string">"true"</span> : <span class="hljs-string">"false"</span>); <span class="hljs-comment">// false</span>
</code></pre>
<h2 id="heading-conclusion">Conclusion 🙋‍♂️🙋‍♂️</h2>
<p>That's it for this article for exploring more about conditionals refer to the recourses mentioned below.</p>
<ul>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/conditionals#switch_statements">mdn docs</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[JavaScript Objects]]></title><description><![CDATA[Objects in javascript are one of the non-primitive datatypes which allow us to store multiple values in key-value pairs. These key-value pairs are known as properties. The key is also called as property name and the value is also called the property ...]]></description><link>https://blog.ankitdevelops.in/javascript-objects</link><guid isPermaLink="true">https://blog.ankitdevelops.in/javascript-objects</guid><category><![CDATA[iwritecode]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[#iwritecode  #hiteshchoudhary  #LCO  #markdown #git  #github]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Mon, 16 Jan 2023 12:30:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1673355675722/956ea7c4-2508-404f-8224-c06aa9c34026.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Objects in javascript are one of the non-primitive datatypes which allow us to store multiple values in key-value pairs. These key-value pairs are known as properties. The key is also called as property name and the value is also called the property value. In objects, values can be of any datatype, it can be string, numbers, boolean, function, array, etc, the main difference between a primitive datatype and an object is that a primitive datatype can store values of only a single datatype but in the object, we can store the value of any type. Key in objects can only be a string if we use the key of any other datatype then it is converted to a string. Enough of the talking let's take a look at an object.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> student = {
  <span class="hljs-attr">name</span>: <span class="hljs-string">"John Kumar"</span>,
  <span class="hljs-attr">age</span>: <span class="hljs-number">20</span>,
  <span class="hljs-attr">subjects</span>: [<span class="hljs-string">"Maths"</span>, <span class="hljs-string">"Science"</span>, <span class="hljs-string">"Computer Science"</span>],
  <span class="hljs-attr">isPresent</span>: <span class="hljs-literal">true</span>,
};
</code></pre>
<p>In the above example <code>student</code> is an object, and it has four properties. <code>name</code> ,<code>age</code> <code>subjects</code> <code>isPresent</code> are key or we can call it property name and <code>John Kumar</code> <code>20</code> <code>["Maths", "Science", "Computer Science"]</code> <code>isPresent</code> are values of different types. Anything left to the colon <code>:</code> is key and the right to the colon is value.</p>
<h2 id="heading-creating-objects">Creating Objects</h2>
<p>For creating objects in javascript there are many different syntaxes, but in this article, we are going to talk about two, <code>object literal</code> and <code>object constructor.</code></p>
<h3 id="heading-object-literal">Object Literal</h3>
<p>We can create an empty object with just a pair of curly brackets <code>{}</code> , this syntax of creating an object is known as Object Literal.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// object literal syntax</span>
<span class="hljs-keyword">const</span> student = {} <span class="hljs-comment">// an empty object</span>
</code></pre>
<h3 id="heading-object-constructor">Object Constructor</h3>
<p>We can also create a new object with the help of the <code>constructor</code> function and <code>new</code> keyword, this method of creating an object is known as an Object constructor.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// object constructor syntax</span>
<span class="hljs-keyword">const</span> teacher= <span class="hljs-keyword">new</span> <span class="hljs-built_in">Object</span>(); <span class="hljs-comment">// an empty object</span>
</code></pre>
<h2 id="heading-adding-properties-to-an-object">Adding Properties to an Object</h2>
<p>We have created two objects <code>student</code> and <code>teacher</code> let's now add some properties to these objects. The properties of an object are the same as the variable except that they are linked to objects rather than being confined to a specific scope. The properties of an object define its characteristics.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> Student= {}; <span class="hljs-comment">// empty object with Object literal syntax</span>

<span class="hljs-comment">// Adding properties to student object</span>

Student.name = <span class="hljs-string">"John Kumar"</span>;
Student.age = <span class="hljs-number">20</span>;
Student.subjects = [<span class="hljs-string">"Maths"</span>, <span class="hljs-string">"Science"</span>, <span class="hljs-string">"Computer Science"</span>];
Student.isPresent = <span class="hljs-literal">true</span>;
<span class="hljs-comment">// another syntax to add properties</span>
Student[<span class="hljs-string">"class"</span>] = <span class="hljs-string">"VII"</span>;
Student[<span class="hljs-string">"phone number"</span>] = <span class="hljs-number">9898989898</span>;

<span class="hljs-comment">// displaying the Student Object</span>
<span class="hljs-built_in">console</span>.log(Student);
<span class="hljs-comment">// Output</span>
<span class="hljs-comment">/*
{
  name: 'John Kumar',
  age: 20,
  subjects: [ 'Maths', 'Science', 'Computer Science' ],
  isPresent: true,
  class: 'VII',
  'phone number': 9898989898
}
*/</span>

<span class="hljs-comment">// teacher Object</span>

<span class="hljs-keyword">const</span> Teacher= <span class="hljs-keyword">new</span> <span class="hljs-built_in">Object</span>(); <span class="hljs-comment">// empty object with Object Constructor syntax</span>
<span class="hljs-comment">//Adding properties to teacher Object</span>
Teacher.name = <span class="hljs-string">"Tony Prasad"</span>;
Teacher.age = <span class="hljs-number">50</span>;
Teacher.teachers = [<span class="hljs-string">"Algorithms"</span>, <span class="hljs-string">"Maths"</span>];
Teacher.isPresent = <span class="hljs-literal">true</span>;
<span class="hljs-comment">// another syntax</span>
Teacher[<span class="hljs-string">"department"</span>] = <span class="hljs-string">"Computer Science"</span>;
Teacher[<span class="hljs-string">"phone number"</span>] = <span class="hljs-number">9898989898</span>;
<span class="hljs-built_in">console</span>.log(Teacher);
<span class="hljs-comment">// Output</span>
<span class="hljs-comment">/* 
{
  name: 'Tony Prasad',
  age: 50,
  teachers: [ 'Algorithms', 'Maths' ],
  isPresent: true,
  department: 'Computer Science',
  'phone number': 9898989898
}
*/</span>
</code></pre>
<h3 id="heading-accessing-properties-from-an-object">Accessing Properties from an Object</h3>
<p>We can access the properties of an Object using their properties name, there are two syntaxes to do that, let's talk about them one by one.</p>
<h3 id="heading-dot-notation">Dot Notation</h3>
<p>This is the most used syntax.</p>
<blockquote>
<p>objectName.propertyName</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-comment">// Display student name from student object </span>
<span class="hljs-built_in">console</span>.log(Student.name);
<span class="hljs-comment">// Display student subjects from student object</span>
<span class="hljs-built_in">console</span>.log(Student.subjects);
<span class="hljs-comment">// Display student phone number from student object</span>

<span class="hljs-comment">// We can't do that using dot notation, when we are using</span>
<span class="hljs-comment">// multi word property name we can't use dot notation syntax to do that.</span>
<span class="hljs-comment">// that's why multi word property name is not recommended use camelCase or </span>
<span class="hljs-comment">// sanke_case property name</span>
</code></pre>
<h3 id="heading-bracket-notation">Bracket Notation</h3>
<blockquote>
<p>objectName["propertyName"]</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-comment">// Display student name from student object </span>
<span class="hljs-built_in">console</span>.log(Student[<span class="hljs-string">"name"</span>]);

<span class="hljs-comment">// Display student subjects from student object</span>
<span class="hljs-built_in">console</span>.log(Student[<span class="hljs-string">"subjects"</span>]);

<span class="hljs-comment">// Display student phone number from student object</span>
<span class="hljs-built_in">console</span>.log(Student[<span class="hljs-string">"phone number"</span>]);
</code></pre>
<h2 id="heading-object-methods">Object Methods</h2>
<p>What are the methods? - Nothing to worry about methods are functions defined inside an object, yes we can define a function inside an object, let's do that. Let's define some methods inside an Object, but first, let's talk about <code>this</code> keyword.</p>
<blockquote>
<p><code>this</code> is a special keyword in Javascirpt, It refers to an Object but depends on where it's being used. If <code>this</code> keyword is being used in an object's methods then this refers to the current object, if <code>this</code> keyword is being used alone then it refers to global object.</p>
<p>I know this small introduction will not clear your confusion about this, I will talk more about this in the below example which would make more sense, also I will leave some link to resources at the end of this article so that you can understand it better.</p>
</blockquote>
<p><strong><em>Syntax 1</em></strong></p>
<pre><code class="lang-javascript"><span class="hljs-comment">// We can define Methods directly inside an Object</span>
<span class="hljs-keyword">const</span> Student = {
  <span class="hljs-attr">name</span>: <span class="hljs-string">"John Kumar"</span>,
  <span class="hljs-attr">age</span>: <span class="hljs-number">20</span>,
  <span class="hljs-attr">subjects</span>: [<span class="hljs-string">"Maths"</span>, <span class="hljs-string">"Science"</span>, <span class="hljs-string">"Computer Science"</span>],
  <span class="hljs-attr">isPresent</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">class</span>: <span class="hljs-string">"VII"</span>,
  <span class="hljs-string">"phone number"</span>: <span class="hljs-number">9898989898</span>,
  <span class="hljs-attr">sayHi</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello <span class="hljs-subst">${<span class="hljs-built_in">this</span>.name}</span>`</span>); <span class="hljs-comment">// here this refers to the Student Object</span>
  },
};

<span class="hljs-comment">// Calling the sayHi Method</span>
Student.sayHi(); <span class="hljs-comment">// sayHi is a function so we need to call it using ()</span>
</code></pre>
<p><strong><em>Syntax 2</em></strong></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> Student = {}; <span class="hljs-comment">// empty object</span>

<span class="hljs-comment">// Adding different properties to it</span>
Student.name = <span class="hljs-string">"John Kumar"</span>;
Student.age = <span class="hljs-number">20</span>;
Student.subjects = [<span class="hljs-string">"Maths"</span>, <span class="hljs-string">"Science"</span>, <span class="hljs-string">"Computer Science"</span>];
Student.isPresent = <span class="hljs-literal">true</span>;
Student[<span class="hljs-string">"class"</span>] = <span class="hljs-string">"VII"</span>;
Student[<span class="hljs-string">"phone number"</span>] = <span class="hljs-number">9898989898</span>;

<span class="hljs-comment">// Adding sayHi method to Student Object</span>
Student.sayHi = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello <span class="hljs-subst">${<span class="hljs-built_in">this</span>.name}</span>`</span>); <span class="hljs-comment">// here this refer to Student Object</span>
};

<span class="hljs-comment">// Calling the sayHi Method</span>
Student.sayHi(); <span class="hljs-comment">// sayHi is a function so we need to call it using ()</span>
</code></pre>
<p><strong><em>Syntax 3</em></strong></p>
<pre><code class="lang-javascript"><span class="hljs-comment">// We can declare an independent function</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">displayAge</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-built_in">this</span>.name}</span> is <span class="hljs-subst">${<span class="hljs-built_in">this</span>.age}</span> years old`</span>); <span class="hljs-comment">// </span>

}

<span class="hljs-comment">// Assigning it to different Object</span>

<span class="hljs-comment">// Assigning displayAge method to Student Object we defined earlier</span>
Student.displayAge = displayAge;

<span class="hljs-comment">// Assigning displayAge method to Teacher Object we defined earlier</span>
Teacher.displayAge = displayAge;

<span class="hljs-comment">// Calling the displayAge method from Student Object</span>
Student.displayAge();

<span class="hljs-comment">// Calling the displayAge  method from Teacher Object</span>
Teacher.displayAge();
</code></pre>
<blockquote>
<p>take a look at the <code>displayAge</code> function from the above example, here this will refer to object with which function displayAge will be used, if displayAge function will be used with Student object then <code>this</code> will refer to Student object and if displayAge function will be used with Teacher object then <code>this</code> will refer to Teacher object.</p>
</blockquote>
<h3 id="heading-deleting-object-property">Deleting Object Property</h3>
<p>To remove a property from the object we use <code>delete</code> operator.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">delete</span> Student.name; <span class="hljs-comment">// will delete name property from the student object.</span>
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>That's it for this article we will learn more about objects when we talk about Prototypes.</p>
<p><strong><em>Resources to Explore More:</em></strong></p>
<ul>
<li><p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this">Read more about this on mdn</a></p>
</li>
<li><p><a target="_blank" href="https://www.youtube.com/watch?v=jTPt5XO7KoU">YouTube Video on this keyword</a></p>
</li>
</ul>
<iframe src="https://giphy.com/embed/j1HOrWfwjiKCAW5e8g"></iframe>]]></content:encoded></item><item><title><![CDATA[Operators In JavaScript]]></title><description><![CDATA[The operator is a symbol that performs a specific operation on one or more operands (values or variables) and produces a result. Operators are an essential part of any programming language and are used to manipulate data and variables. These are the ...]]></description><link>https://blog.ankitdevelops.in/operators-in-javascript</link><guid isPermaLink="true">https://blog.ankitdevelops.in/operators-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[#iwritecode  #hiteshchoudhary  #LCO  #markdown #git  #github]]></category><category><![CDATA[#iNeuron #HiteshChaudhary #WebDev #Javascript #LCO #LearnCodeOnline  #LCO  #css #learncodeonline #cssselectors  @hiteshchoudharylco  code with hitesh choudhary]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Mon, 16 Jan 2023 06:27:27 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1673849423657/06f6ff22-580f-435d-947b-08b6058ebe50.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The operator is a symbol that performs a specific operation on one or more operands (values or variables) and produces a result. Operators are an essential part of any programming language and are used to manipulate data and variables. These are the different types of operators we are going to talk about in this article.</p>
<ul>
<li><p>Assignment Operator</p>
</li>
<li><p>Arithmetic Operator</p>
</li>
<li><p>Comparison Operator</p>
</li>
<li><p>Logical Operator</p>
</li>
<li><p>Ternary Operator</p>
</li>
</ul>
<blockquote>
<p>An operand is a value or variable on which an operator performs an operation.</p>
</blockquote>
<h1 id="heading-assignment-operator">Assignment Operator</h1>
<p>The assignment operator <code>=</code> is used to assign value to a variable. The value of the right operand is assigned to the left operand.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> age = <span class="hljs-number">36</span> <span class="hljs-comment">// here 36 will be assigned to variable age.</span>
<span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">'Banana'</span>,<span class="hljs-string">"grapes"</span>,<span class="hljs-string">"guava"</span>]
</code></pre>
<h1 id="heading-arithmetic-operator">Arithmetic Operator</h1>
<p>We can perform standard arithmetic operations on numbers using the arithmetic operators in JavaScript. Following are some of the many arithmetic operators in JavaScript.</p>
<ul>
<li><p>Addition <code>+</code></p>
</li>
<li><p>Subtraction <code>-</code></p>
</li>
<li><p>Multiplication <code>*</code></p>
</li>
<li><p>Division <code>/</code></p>
</li>
<li><p>Modulus <code>%</code> (also known as remainder)</p>
</li>
<li><p>Increment <code>++</code></p>
</li>
<li><p>Decrement <code>--</code></p>
</li>
</ul>
<h2 id="heading-addition">Addition</h2>
<p>The addition operator is used when we want to find the sum of two or more numbers.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">5</span> + <span class="hljs-number">5</span>); <span class="hljs-comment">// 10</span>
</code></pre>
<p>We can also use the Addition operator to join two or more strings together, This is known as string concatenation.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> firstName = <span class="hljs-string">"John "</span>;
<span class="hljs-keyword">let</span> lastName = <span class="hljs-string">"Kumar"</span>;
<span class="hljs-built_in">console</span>.log(firstName + lastName); <span class="hljs-comment">// John Kumar</span>
</code></pre>
<h2 id="heading-subtraction">Subtraction</h2>
<p>The subtraction operator is used to find the difference between numbers.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">55</span> - <span class="hljs-number">2</span>); <span class="hljs-comment">//53</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-number">55.235</span> - <span class="hljs-number">23.87</span>); <span class="hljs-comment">//31.365</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-number">10</span> - <span class="hljs-number">2</span> + <span class="hljs-number">5</span>); <span class="hljs-comment">// 13 This is definitely wrong answer according to bodmas rule.</span>
</code></pre>
<p>In the above example, line 3 is giving us the wrong answer because of the <em><mark>order of precedence.</mark></em> Both addition and subtraction have the same order of precedence so they are evaluated Right to the left that's why subtraction is being evaluated first. Here is how we can correct that.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">10</span> - (<span class="hljs-number">2</span> + <span class="hljs-number">5</span>)); <span class="hljs-comment">// 3</span>
</code></pre>
<p>Now the expression inside the parenthesis will be evaluated first because of the order of precedence.</p>
<h2 id="heading-division">Division (/)</h2>
<p>The division operator is used to find the quotient.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">55</span> / <span class="hljs-number">5</span>);  <span class="hljs-comment">// 11</span>
</code></pre>
<h2 id="heading-modulus">Modulus(%)</h2>
<p>The modulus operator is used to find the remainder.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">56</span> % <span class="hljs-number">5</span>); <span class="hljs-comment">// 1</span>
</code></pre>
<h2 id="heading-increment">Increment (++)</h2>
<p>The JavaScript increment operator (++) is used to increment (add 1 to) a variable's value. It can be used in both postfix and prefix notation. In postfix notation (i++), the current value of the variable is returned, and then the value is incremented. In prefix notation (++i), the value is incremented first, and then the new value is returned. It's important to note that the increment operator (++) only works on numbers and will result in a NaN if used on a non-numeric value.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> x = <span class="hljs-number">5</span>;
<span class="hljs-built_in">console</span>.log(x++); <span class="hljs-comment">// Output: 5</span>
<span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// Output: 6</span>

<span class="hljs-keyword">let</span> y = <span class="hljs-number">5</span>;
<span class="hljs-built_in">console</span>.log(++y); <span class="hljs-comment">// Output: 6</span>
<span class="hljs-built_in">console</span>.log(y); <span class="hljs-comment">// Output: 6</span>
</code></pre>
<h2 id="heading-decrement">Decrement (--)</h2>
<p>The JavaScript decrement operator (--) is used to decrement (subtract 1 from) a variable's value. It can be used in both postfix and prefix notation. In postfix notation (i--), the current value of the variable is returned, and then the value is decremented. In prefix notation (--i), the value is decremented first, and then the new value is returned.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> x = <span class="hljs-number">5</span>;
<span class="hljs-built_in">console</span>.log(x--); <span class="hljs-comment">// Output: 5</span>
<span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// Output: 4</span>

<span class="hljs-keyword">let</span> y = <span class="hljs-number">5</span>;
<span class="hljs-built_in">console</span>.log(--y); <span class="hljs-comment">// Output: 4</span>
<span class="hljs-built_in">console</span>.log(y); <span class="hljs-comment">// Output: 4</span>
</code></pre>
<h1 id="heading-comparison-operator">Comparison Operator</h1>
<p>A comparison Operator is used to compare two or more operands. These are the different types of comparison operators.</p>
<h2 id="heading-equal">Equal (==)</h2>
<p>The equal operator returns <code>ture</code> if both the operands are the same, it does not check the type of the operands.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> var1 = <span class="hljs-number">10</span>;
<span class="hljs-keyword">const</span> var2 = <span class="hljs-number">20</span>;
<span class="hljs-keyword">const</span> var3 = <span class="hljs-string">"10"</span>;
<span class="hljs-keyword">const</span> var4 = <span class="hljs-number">10</span>;
<span class="hljs-built_in">console</span>.log(var1 == var2); <span class="hljs-comment">// false</span>
<span class="hljs-built_in">console</span>.log(var1 == var3); <span class="hljs-comment">// true both variable have same value but they are of different type</span>
<span class="hljs-built_in">console</span>.log(var1 == var4); <span class="hljs-comment">//true</span>
</code></pre>
<h2 id="heading-strict-equal">Strict equal (===)</h2>
<p>This operator is the same as the <code>equal</code> operator, but it also checks the type of operands.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> var1 = <span class="hljs-number">10</span>;
<span class="hljs-keyword">const</span> var2 = <span class="hljs-number">20</span>;
<span class="hljs-keyword">const</span> var3 = <span class="hljs-string">"10"</span>;
<span class="hljs-keyword">const</span> var4 = <span class="hljs-number">10</span>;
<span class="hljs-built_in">console</span>.log(var1 === var2); <span class="hljs-comment">// false --different value</span>
<span class="hljs-built_in">console</span>.log(var1 === var3); <span class="hljs-comment">// false --because both value is of different type</span>
<span class="hljs-built_in">console</span>.log(var1 === var4); <span class="hljs-comment">//true --same value same type</span>
</code></pre>
<h2 id="heading-not-equal">Not Equal (!=)</h2>
<p>This operator returns <code>true</code> if the operands are not equal and return <code>false</code> if they are equal.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> var1 = <span class="hljs-number">10</span>;
<span class="hljs-keyword">const</span> var2 = <span class="hljs-number">20</span>;
<span class="hljs-keyword">const</span> var3 = <span class="hljs-string">"10"</span>;
<span class="hljs-keyword">const</span> var4 = <span class="hljs-number">10</span>;
<span class="hljs-built_in">console</span>.log(var1 != var2); <span class="hljs-comment">// true -- different value</span>
<span class="hljs-built_in">console</span>.log(var1 != var3); <span class="hljs-comment">// false -- same value</span>
<span class="hljs-built_in">console</span>.log(var1 != var4); <span class="hljs-comment">//false -- same value</span>
</code></pre>
<h2 id="heading-strict-not-equal">Strict Not Equal (!==)</h2>
<p>This operator returns <code>true</code> if the operands are not equal and of the same type, else it returns <code>false.</code></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> var1 = <span class="hljs-number">10</span>;
<span class="hljs-keyword">const</span> var2 = <span class="hljs-number">20</span>;
<span class="hljs-keyword">const</span> var3 = <span class="hljs-string">"10"</span>;
<span class="hljs-keyword">const</span> var4 = <span class="hljs-number">10</span>;
<span class="hljs-built_in">console</span>.log(var1 !== var2); <span class="hljs-comment">// true -- different values</span>
<span class="hljs-built_in">console</span>.log(var1 !== var3); <span class="hljs-comment">// true -- same value, different type</span>
<span class="hljs-built_in">console</span>.log(var1 !== var4); <span class="hljs-comment">// false -- same value, same type</span>
</code></pre>
<h2 id="heading-few-more-andgtandgtandltandlt">Few More (&gt;,&gt;=,&lt;,&lt;=)</h2>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> var1 = <span class="hljs-number">10</span>;
<span class="hljs-keyword">const</span> var2 = <span class="hljs-number">20</span>;
<span class="hljs-keyword">const</span> var3 = <span class="hljs-number">10</span>;

<span class="hljs-comment">// Greater than</span>
<span class="hljs-built_in">console</span>.log(var1 &gt; var2); <span class="hljs-comment">// false</span>
<span class="hljs-built_in">console</span>.log(var2 &gt; var1); <span class="hljs-comment">//true</span>

<span class="hljs-comment">// Less than</span>
<span class="hljs-built_in">console</span>.log(var1 &lt; var2); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(var2 &lt; var1); <span class="hljs-comment">// false</span>

<span class="hljs-comment">// Greater than or equal </span>
<span class="hljs-built_in">console</span>.log(var1 &gt;= var2); <span class="hljs-comment">// false</span>
<span class="hljs-built_in">console</span>.log(var2 &gt;= var1); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(var1 &gt;= var3); <span class="hljs-comment">// true</span>

<span class="hljs-comment">// Less than or equal</span>
<span class="hljs-built_in">console</span>.log(var1 &lt;= var2); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(var2 &lt;= var1); <span class="hljs-comment">// false</span>
<span class="hljs-built_in">console</span>.log(var1 &lt;= var3); <span class="hljs-comment">// true</span>
</code></pre>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Operator</td><td>Description</td><td>Example</td></tr>
</thead>
<tbody>
<tr>
<td>Greater Than (&gt;)</td><td>Return <code>true</code> if the left operand is greater than the right operand</td><td>see the above code example.</td></tr>
<tr>
<td>Less Than(&lt;)</td><td>Returns <code>true</code> if the left operand is less than the right operand</td><td>see the above code example.</td></tr>
<tr>
<td>Greater Than or Equal (&gt;=)</td><td>Evaluates to <code>true</code> if the value on the left is greater than or equal to the value on the right.</td><td>see the above code example.</td></tr>
<tr>
<td>Less Than or Equal(&lt;=)</td><td>Evaluates to <code>true</code> if the value on the left is less than or equal to the value on the right.</td><td>see the above code example.</td></tr>
</tbody>
</table>
</div><h1 id="heading-logical-operator">Logical Operator</h1>
<p>A logical operator evaluates expressions and returns a boolean value indicating whether a specific condition is true or false. When used with boolean operands, these operators will return a boolean result; when used with non-boolean values, they will produce a non-boolean result. There are three different types of logical operators in JavaScript.</p>
<ul>
<li><p>OR (||)</p>
</li>
<li><p>AND (&amp;&amp;)</p>
</li>
<li><p>NOT(!)</p>
</li>
</ul>
<h2 id="heading-or">OR</h2>
<p>The <code>OR</code> the operator is represented by a double vertical line <code>||</code> . <code>OR</code> The operator returns <code>true</code> if any of its expressions evaluates to <code>true</code> otherwise, it returns false. If the <code>OR</code> operator is used with non-boolean values then it returns non-boolean values.</p>
<blockquote>
<p>Syntax:</p>
<p>expression1 || expression2</p>
<ul>
<li><p>if expression 1 is evaluated to true then it returns expession 1 otherwise exprssion2</p>
</li>
<li><p>logical OR expression is evaluated left to right, so if the first expression is evaluated to true, then it doesnot check the next expression.</p>
</li>
<li><p>if all the expression evaluates to false then it reaturn false.</p>
</li>
</ul>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> var1 = <span class="hljs-number">10</span>;
<span class="hljs-keyword">const</span> var2 = <span class="hljs-number">20</span>;
<span class="hljs-keyword">const</span> var3 = <span class="hljs-number">10</span>;

<span class="hljs-built_in">console</span>.log(var1 &gt; var2 || var2 &gt; var1);<span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">true</span> || <span class="hljs-literal">true</span>); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">false</span> || <span class="hljs-literal">true</span>); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">true</span> || <span class="hljs-literal">false</span>); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">false</span> || <span class="hljs-literal">false</span>); <span class="hljs-comment">// false</span>
</code></pre>
<h2 id="heading-and-andampandamp">AND (&amp;&amp;)</h2>
<p>The <code>AND</code> operator is represented by two ampersands <code>&amp;&amp;</code> sign. The <code>AND</code> operator return <code>true</code> if all of its conditions evaluates to <code>true</code> if any of its conditions evaluates to <code>false</code> it returns <code>false</code>.</p>
<blockquote>
<p>Syntax:</p>
<p>expression 1 &amp;&amp; expression 2</p>
<ul>
<li><p>returns <code>true</code> if both the conditions if true otherwise it returns <code>false</code></p>
</li>
<li><p>AND operator is evaluated from left to right, so if any expression evaluates to false it doesn't evaluates any expression after that.</p>
</li>
<li><p>it returns last last operand if all the expression evalutes to 'true`</p>
</li>
</ul>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> var1 = <span class="hljs-number">10</span>;
<span class="hljs-keyword">const</span> var2 = <span class="hljs-number">20</span>;
<span class="hljs-keyword">const</span> var3 = <span class="hljs-number">10</span>;

<span class="hljs-built_in">console</span>.log(var1 &gt; var2 &amp;&amp; var2 &gt; var1); <span class="hljs-comment">// false &amp;&amp; true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">true</span> &amp;&amp; <span class="hljs-literal">true</span>); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">false</span> &amp;&amp; <span class="hljs-literal">true</span>); <span class="hljs-comment">// false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">true</span> &amp;&amp; <span class="hljs-literal">false</span>); <span class="hljs-comment">// false</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-literal">false</span> &amp;&amp; <span class="hljs-literal">false</span>); <span class="hljs-comment">// false</span>
</code></pre>
<h2 id="heading-not">NOT (!)</h2>
<p>The <code>NOT</code> the operator is represented by an explanation <code>!</code> sign. The <code>!</code> operator can be applied to boolean and non-boolean values. When used with a boolean value, it returns the opposite of that value, i.e. if the value is false it returns true, and if the value is true it returns false.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(!<span class="hljs-literal">true</span>); <span class="hljs-comment">//false</span>
<span class="hljs-built_in">console</span>.log(!<span class="hljs-literal">false</span>); <span class="hljs-comment">//true</span>
<span class="hljs-built_in">console</span>.log(!<span class="hljs-number">0</span>); <span class="hljs-comment">//true</span>
<span class="hljs-built_in">console</span>.log(!<span class="hljs-string">"Hello"</span>); <span class="hljs-comment">// false</span>
</code></pre>
<h1 id="heading-ternary-operator">Ternary Operator</h1>
<p>Ternary Operator takes three operands and returns values based on the condition.</p>
<blockquote>
<p>Syntax:</p>
<p>condition ? expression 1 : expression2</p>
<ul>
<li>if the condition is true then it returns expression1 and if the expression if false it returns expression2</li>
</ul>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> var1 = <span class="hljs-number">10</span>;
<span class="hljs-keyword">const</span> var2 = <span class="hljs-number">20</span>;

<span class="hljs-built_in">console</span>.log(var2 &gt; var1 ? <span class="hljs-string">"true"</span> : <span class="hljs-string">"false"</span>); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(var1 &gt; var2 ? <span class="hljs-string">"true"</span> : <span class="hljs-string">"false"</span>); <span class="hljs-comment">// false</span>
</code></pre>
<h1 id="heading-conclusion">Conclusion</h1>
<p>In this article I have only talked about the operators which are used most, there are a few more operators, and if you want to explore more then you can read about them here.</p>
<p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators">Click Here</a></p>
]]></content:encoded></item><item><title><![CDATA[Getting Started With Python]]></title><description><![CDATA[What is Python?
Python is a high-level object-oriented programming language. It was developed by Guido Van Rossum in 1991. Python is a very easy-to-use, interpreted, expressive language. It is one of the most popular programming languages and has man...]]></description><link>https://blog.ankitdevelops.in/getting-started-with-python</link><guid isPermaLink="true">https://blog.ankitdevelops.in/getting-started-with-python</guid><category><![CDATA[Python]]></category><category><![CDATA[Python 3]]></category><category><![CDATA[python beginner]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Mon, 02 Jan 2023 18:15:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1667844289218/AYBiOhUcW.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-what-is-python">What is Python?</h2>
<p>Python is a high-level object-oriented programming language. It was developed by Guido Van Rossum in 1991. Python is a very easy-to-use, interpreted, expressive language. It is one of the most popular programming languages and has many different use cases.</p>
<blockquote>
<p>Python is an interpreted language, which means that it executes code line by line and if there is any error, the program execution stops at that line. It can only continue after the error has been corrected.</p>
</blockquote>
<h2 id="heading-why-you-should-learn-python">Why you should learn Python.</h2>
<ul>
<li>Python is very easy to use and due to its very simple syntax, it is very beginners friendly.</li>
<li>Python is a cross-platform, open-source language, it is free to use python and you can use it on almost any operating system.</li>
<li>Python is being used in many different fields like machine learning, data science, web development, scripting, game development, GUI programs, etc.</li>
<li>Python is an interpreted language, so it makes it easy to debug compared to compiled languages.</li>
</ul>
<h2 id="heading-using-python-on-your-system">Using Python on Your System</h2>
<ul>
<li>The first thing you need to do is download and install python on your system, You can download python from its official website. <a target="_blank" href="https://www.python.org/">python.org</a></li>
<li>Next, you need to use some kind of code editor like <a target="_blank" href="https://vscode.dev/">vscode</a> and set it up for python, here is the official  <a target="_blank" href="https://code.visualstudio.com/docs/python/python-tutorial#:~:text=Getting%20Started%20with%20Python%20in%20VS%20Code">guide</a> or you can use PyCharm ide download it from <a target="_blank" href="https://www.jetbrains.com/pycharm/download/#section=windows">here</a></li>
<li>If you don't want to install it on your system, you can also use an online code editor like 
<a target="_blank" href="https://replit.com/">Replit</a> </li>
</ul>
<h2 id="heading-writing-your-first-python-program">Writing Your First Python Program</h2>
<p>Whenever we learn any new programming language, the first thing we do is that we write a hello world program that displays a “Hello World” message, and in this section of the article we are going to do the same. </p>
<p>In Python when we have to display any message or output on the screen we use <code>print</code> function.</p>
<pre><code class="lang-python">print(<span class="hljs-string">"Hello World"</span>)
</code></pre>
<blockquote>
<p>when we use print function anything inside the parenthesis is displayed on the screen. Here are the few more examples.</p>
</blockquote>
<pre><code class="lang-python">print(<span class="hljs-string">"Hello There"</span>)
print(<span class="hljs-string">'My name is Ankit'</span>)
print(<span class="hljs-number">23</span>)
print(<span class="hljs-number">45</span> + <span class="hljs-number">34</span>)
</code></pre>
<h2 id="heading-comments-in-python">Comments in Python.</h2>
<p>In the world of programming, comments are the lines of code that are not executed by the interpreter or compiler, these lines are simply ignored by the interpreter or compiler. Every programming language has a different syntax to write comments, but in python, we use the <code>#</code> symbol to write comments. </p>
<p>For writing comments in python, you just need to add the <code>#</code> symbol at the start of the line, and the python interpreter will just ignore that line during program execution.</p>
<pre><code class="lang-python"><span class="hljs-comment"># This line is commented</span>
<span class="hljs-comment"># print("Hello There")</span>
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>That's it for this article, I hope you enjoyed it, in the next article we are going to talk about Variables, keywords, character sets, values, etc.</p>
]]></content:encoded></item><item><title><![CDATA[Values & Variables In JavaScript]]></title><description><![CDATA[While working with computers we work with data only, Every Programming language work with some type of values, In this article, we are going to talk about values we can work with in JavaScript. These values are also known as datatypes.Let's explore t...]]></description><link>https://blog.ankitdevelops.in/values-variables-in-javascript</link><guid isPermaLink="true">https://blog.ankitdevelops.in/values-variables-in-javascript</guid><category><![CDATA[iwritecode]]></category><category><![CDATA[#iwritecode  #hiteshchoudhary  #LCO  #markdown #git  #github]]></category><category><![CDATA[#iwritecode #hiteshchoudhary #anuragtiwari #html ]]></category><category><![CDATA[#iwritecode #hiteshchoudary #html]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Mon, 02 Jan 2023 12:37:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1671106590322/LRvuV4dVz.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>While working with computers we work with data only, Every Programming language work with some type of values, In this article, we are going to talk about values we can work with in JavaScript. These values are also known as datatypes.Let's explore them one by one.</p>
<iframe src="https://giphy.com/embed/dO8UvllDU5igjgaTar"></iframe>

<h2 id="heading-number">Number</h2>
<p>In JavaScript, every numeric value is a floating-point number, there is no separate integer type. Values like 23, and 23.534 are all considered as one value i.e number. We can perform arithmetic operations using numbers in JavaScript.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">5</span> + <span class="hljs-number">3</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-number">234.23</span> - <span class="hljs-number">230</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-number">55</span> / <span class="hljs-number">5</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-number">55</span> % <span class="hljs-number">5</span>);
</code></pre>
<h2 id="heading-string">String</h2>
<p>Everything Inside single quotes, double quotes, or backticks is considered a string in JavaScript. The string is used to represent text or a sequence of characters. Make sure you are using the same quotes at the beginning and end of the string.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Valid String in javaSCript</span>
<span class="hljs-string">"Hello World"</span>
<span class="hljs-string">'Hello World'</span>
<span class="hljs-string">`Hello World`</span> <span class="hljs-comment">// backticks are below your esc key</span>

<span class="hljs-comment">// Invalid Strings in JavaScript</span>
<span class="hljs-string">"Hello World'
"</span>Hello World<span class="hljs-string">'
`Hello World"</span>
</code></pre>
<h2 id="heading-boolean">Boolean</h2>
<p>Boolean has only two values <code>true</code> and <code>false</code>. Either it's going to be true or false.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">89</span> &gt; <span class="hljs-number">23</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-number">103</span> &lt; <span class="hljs-number">23</span>);
</code></pre>
<h2 id="heading-empty-values">Empty Values</h2>
<p>There are two special values, null and undefined, that are used to represent the absence of a meaningful value. Although they are values, they do not carry any information or content. null represents the absence of any value, whereas "Undefined" refers to a value that has not been assigned or defined by the compiler. It represents the absence of a defined value.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> name; <span class="hljs-comment">// the value has not been defined</span>
<span class="hljs-built_in">console</span>.log(name); <span class="hljs-comment">// -&gt; undefined</span>
<span class="hljs-keyword">let</span> name2 = <span class="hljs-literal">null</span>;
<span class="hljs-built_in">console</span>.log(name); <span class="hljs-comment">// -&gt; null</span>
</code></pre>
<h2 id="heading-arrays">Arrays</h2>
<p>Arrays in JavaScript are written using square brackets. The array starts with an opening square bracket<code>[</code> and array ends with a closing square bracket <code>]</code> . In an array, we store multiple values separating them with <code>,</code> . In an array, we can store values of any type we talked till now. Let's see some examples of an array.</p>
<p>Each element inside an array is given a unique position known as an index. The index starts from 0 and goes up to a length of the array -1. In the given example "hello" is at index 0 and <code>true</code> is at index 4. We use this index to get elements at specific positions, we will learn more about this in coming articles.</p>
<pre><code class="lang-javascript">[<span class="hljs-string">"hello"</span>, <span class="hljs-string">"there"</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3.4</span>, <span class="hljs-literal">true</span>]
</code></pre>
<h2 id="heading-objects">Objects</h2>
<p>Objects are written using <code>{}</code> curly braces. JavaScript object mimics the behavior of a physical object, Just like a physical object has some properties JavScript objects also have properties, these properties are written in key-value pair separated by a comma. Here is an example of a person object.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> person = {
  <span class="hljs-attr">name</span>: <span class="hljs-string">"Rahul"</span>,
  <span class="hljs-attr">age</span>: <span class="hljs-number">34</span>,
  <span class="hljs-attr">address</span>: <span class="hljs-string">"Delhi"</span>,
  <span class="hljs-attr">designation</span>: <span class="hljs-string">"Full Stack Developer"</span>,
};
</code></pre>
<p>Now we have learned about different types of values we can use in JavaScript, now let's learn how we can store these values in variables.</p>
<h1 id="heading-variables">Variables.</h1>
<p>A Variable is a named label for a memory location where some values are stored. In other words, a Variable is like a container where we can store some values. There are three different ways to declare a variable in JavaScript. <code>var</code> <code>let</code> and <code>const</code> . This would be too early to talk about differences between them so just keep this in mind.</p>
<ul>
<li><p>var - don't use this</p>
</li>
<li><p>let - value can be changed later</p>
</li>
<li><p>const - once defined value can't be changed</p>
</li>
</ul>
<p>We use the assignment operator <code>=</code> to assign value to a variable, a variable is only created once you assign value to it. Let's create a value to store a message.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> message = <span class="hljs-string">"Hello World!"</span>;

<span class="hljs-built_in">console</span>.log(message);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1671105499484/Jgc_ZEScd.png" alt class="image--center mx-auto" /></p>
<p>in the above example, we use let to declare the variable, so we can change the message, let's do that.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1671105627814/8hfcIlZl9.png" alt class="image--center mx-auto" /></p>
<p>in the above example in line 4 I have used the same message variable to assign a new value. <code>console.log</code> function from line 2 is displaying the old message and <code>console.log</code> function from line 5 is displaying the new message. Now let's see an example of const.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1671105909604/6GwXGPko8.png" alt class="image--center mx-auto" /></p>
<p>In the above example, I have replaced <code>let</code> with <code>const</code> , Notice in the console <code>console.log</code> function from line 2 is displaying the message, but when I tried to assign a new message in line 4 it's giving me an error in the console.</p>
<h2 id="heading-rules-for-naming-a-variable">Rules for naming a variable.</h2>
<ul>
<li><p>The first character of a variable must be a letter</p>
</li>
<li><p>JavaScript is a case-sensitive language so Uppercase letters and lowercase letters are different. E.g variable <code>age</code> and <code>Age</code> will be treated as two different variables.</p>
</li>
<li><p>JavaScript reserved keywords can not be used as a variable name.</p>
</li>
</ul>
<iframe src="https://giphy.com/embed/zTyOZBq1WUY78bmqUv"></iframe>

<p>In the next article, we are going to talk about Operators in JavaScript.</p>
]]></content:encoded></item><item><title><![CDATA[JavaScript Array]]></title><description><![CDATA[The array is the most used data structure of any programming language, so then what is Array? The array is a collection of items or elements. It is like a container where you can store multiple values. in JavaScript Array is denoted by square bracket...]]></description><link>https://blog.ankitdevelops.in/javascript-array</link><guid isPermaLink="true">https://blog.ankitdevelops.in/javascript-array</guid><category><![CDATA[iwritecode]]></category><category><![CDATA[#iwritecode  #hiteshchoudhary  #LCO  #markdown #git  #github]]></category><category><![CDATA[#iwritecode #hiteshchoudhary #anuragtiwari #html ]]></category><category><![CDATA[#iwritecode #hiteshchoudary #html]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Mon, 19 Dec 2022 17:24:10 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1671339095703/oEWb5b_eM.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The array is the most used data structure of any programming language, so then what is Array? The array is a collection of items or elements. It is like a container where you can store multiple values. in JavaScript Array is denoted by square brackets <code>[]</code> , opening square brackets indicate the start of the array and closing square brackets indicate the end of the array. Inside the square brackets, we can store multiple values separating them with comma <code>,</code> . This is how an array in JavaScript looks like <code>let fruits = ["apple", "grapes", "banana", "orange", "guava"];</code> Here we have a list of fruits and we are storing them in a fruits variable.</p>
<p>In an Array, all the items of an array are known as elements, and elements in an Array are given a unique position known as an <mark>index </mark> we will talk more about the index later, so in the above example, the names of fruit are elements of the fruit array. In JavaScript Array, we can store values of any type, values can be anything e.g string, boolean, number, etc we can even store an array inside an array. Here are a few more examples of JavaScript Array.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> arr1 = []
<span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">'apple'</span>, <span class="hljs-string">'grapes'</span>, <span class="hljs-string">'banana'</span>, <span class="hljs-string">'orange'</span>, <span class="hljs-string">'guava'</span>] <span class="hljs-comment">// array of strings</span>
<span class="hljs-keyword">let</span> even_numbers = [<span class="hljs-number">2</span>,<span class="hljs-number">4</span>,<span class="hljs-number">6</span>,<span class="hljs-number">8</span>,<span class="hljs-number">10</span>] <span class="hljs-comment">// array of numbers</span>
<span class="hljs-keyword">let</span> vowels = [<span class="hljs-string">'a'</span>,<span class="hljs-string">'e'</span>,<span class="hljs-string">'i'</span>,<span class="hljs-string">'o'</span>,<span class="hljs-string">'u'</span>] <span class="hljs-comment">// array of characters</span>
<span class="hljs-keyword">let</span> arr2 = [<span class="hljs-string">'hello'</span>, <span class="hljs-string">"2"</span>, <span class="hljs-number">76</span>, <span class="hljs-number">8.23</span>] <span class="hljs-comment">// array of mixed values</span>
<span class="hljs-keyword">let</span> arr3 = [<span class="hljs-string">"How are You"</span>, [<span class="hljs-string">"hi"</span>, <span class="hljs-number">4.5</span>, <span class="hljs-number">9</span>], <span class="hljs-number">98</span>] <span class="hljs-comment">// array of mixed values</span>
</code></pre>
<h1 id="heading-how-to-create-an-array-in-javascript">How to create an Array in JavaScript</h1>
<p>We can create an array by just assigning a pair of square brackets to a variable let's look at this example <code>let fruits = [];</code> This fruit array has no element inside it, so it is known as an empty array. There is also another way to create an array in JavaScript but it's not used much.<code>let fruits = new Array();</code> this is also an empty array. take a look at the below code and you will know how we add elements to this Array.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = []; <span class="hljs-comment">// empty array</span>
<span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">'apple'</span>, <span class="hljs-string">'grapes'</span>, <span class="hljs-string">'banana'</span>, <span class="hljs-string">'orange'</span>, <span class="hljs-string">'guava'</span>]

<span class="hljs-keyword">let</span> fruits = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Array</span>(); .<span class="hljs-comment">// not used much</span>
<span class="hljs-keyword">let</span> fruits = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Array</span>(<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>);
</code></pre>
<h1 id="heading-more-on-index">More on Index</h1>
<p>An Array is an ordered collection of elements and an element in an array is given a unique position known as an Index, we use this index to access individual elements from the array. The index starts from 0 and goes up the length of the array - 1. The length of the array is the number of elements in the array. let's look at our fruits array <code>let fruits = ['apple', 'grapes', 'banana', 'orange', 'guava']</code> in this fruits array, there are five elements so the length of the array is 5. So the index of the array of fruits starts from 0 and goes up to 4, <code>apple</code> is at an index of 0, and <code>guava</code> is at the index of 4. Take a look at the below picture you will get a more clear picture of how the index works.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1671288737003/twbK1hBQ4.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-access-element-of-an-array-using-index">Access Element of an Array Using Index.</h2>
<p>We have already learned that we can access an individual element using the index, to access individual elements from an array we use this syntax.</p>
<blockquote>
<p>arrayName[index]</p>
</blockquote>
<p>Take a look at the example below it will make more sense.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];

<span class="hljs-comment">// get first element from the fruits array</span>
<span class="hljs-built_in">console</span>.log(fruits[<span class="hljs-number">0</span>]); <span class="hljs-comment">// first element is always at index 0</span>

<span class="hljs-comment">// get second element from the fruits array</span>
<span class="hljs-built_in">console</span>.log(fruits[<span class="hljs-number">1</span>]);

<span class="hljs-comment">// get third element from the array</span>
<span class="hljs-built_in">console</span>.log(fruits[<span class="hljs-number">2</span>]);

<span class="hljs-comment">// get last element from the fruits array</span>
<span class="hljs-built_in">console</span>.log(fruits[<span class="hljs-number">4</span>]); <span class="hljs-comment">// not recommended</span>
</code></pre>
<h2 id="heading-getting-the-last-element-of-an-array">Getting the Last element of an Array</h2>
<p>Getting the last element from an array is the same as getting the element at any other position in an array, take a look at the example below.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];

<span class="hljs-comment">// get last element from the fruits array</span>
<span class="hljs-built_in">console</span>.log(fruits[<span class="hljs-number">4</span>]); <span class="hljs-comment">// not recommended</span>
</code></pre>
<p>In the above example, you can see that <code>guava</code> at index <code>4</code> is the last element from the fruits array and we are able to access it just like any other element on any position, yes but think of a situation when you don't have any idea about the length of the array, you don't know at what index the last element of the array is going to be, this method works only when the array is short. Let's learn about the other method.</p>
<p>Remember I mentioned that the index starts from 0 and goes up to the length of the array -1, so if the length of the array is 10 then the index will start from 0 and will go up to 9, if we can somehow find the length of the array then it will be easy to find the last index of the array.</p>
<blockquote>
<h3 id="heading-length-of-the-array">length of the array:-</h3>
<p>lenght of the array is number of elemement in the array. I f there are 5 elements in the array then lenght of the array is 5.</p>
<p>JavaScript comes with inbuilt property to find length of the array, this is the syntax we use to find the lenght of the array.</p>
<p><code>arrayName.length</code></p>
<p>this return us the lenght of the array.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];

<span class="hljs-built_in">console</span>.log(fruits.length); <span class="hljs-comment">// output -&gt; 5</span>
</code></pre>
</blockquote>
<p>Now we know how to find the length of the array so to access the last element of the array we can use <code>fruits.length - 1</code> it will give us the last index of the array.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];

<span class="hljs-comment">// just for example gives us last index of any array</span>
<span class="hljs-built_in">console</span>.log(fruits.length - <span class="hljs-number">1</span>); <span class="hljs-comment">// output -&gt; 4</span>

<span class="hljs-comment">// get the last element from the fruits array</span>
<span class="hljs-built_in">console</span>.log(fruits[fruits.length - <span class="hljs-number">1</span>]); <span class="hljs-comment">// output -&gt; 4</span>
</code></pre>
<h2 id="heading-modifying-element-of-an-array-using-index">Modifying Element of An Array Using Index.</h2>
<p>We can access elements of an array using the index, we can also use the index to modify an array, take a look at the below syntax.</p>
<blockquote>
<p>arrayName[index] = "value"</p>
</blockquote>
<p>Here are a few more examples.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];

<span class="hljs-comment">// Remove banana from index 2 and add Papaya at index 2</span>
fruits[<span class="hljs-number">2</span>] = <span class="hljs-string">"Papaya"</span>; <span class="hljs-comment">// this will replace banana with papaya</span>
<span class="hljs-built_in">console</span>.log(fruits); <span class="hljs-comment">// o/p-&gt;[ 'apple', 'grapes', 'Papaya', 'orange', 'guava' ]</span>

<span class="hljs-keyword">let</span> newList = [];

<span class="hljs-comment">// Add element to the newList</span>

newList[<span class="hljs-number">0</span>] = <span class="hljs-string">"Pear"</span>;
newList[<span class="hljs-number">1</span>] = <span class="hljs-string">"Strawberry"</span>;
newList[<span class="hljs-number">2</span>] = <span class="hljs-string">"Watermelon"</span>;
<span class="hljs-built_in">console</span>.log(newList); <span class="hljs-comment">// o/p -&gt; [ 'Pear', 'Strawberry', 'Watermelon' ]</span>
</code></pre>
<h1 id="heading-array-methods">Array Methods</h1>
<p>JavaScript comes with lots of array methods, we can use this method to make perform different tasks and make our work easy. In this section of the article, we will talk about some of the most used array methods.</p>
<iframe src="https://giphy.com/embed/UxTBfyPE5JwRQbPCgU"></iframe>

<h2 id="heading-push">push()</h2>
<iframe src="https://giphy.com/embed/1rRkosgBYUL018uCOb"></iframe>

<p><code>push()</code> method is to add one or more elements at the end of the array, it also returns us the new length of the array after adding new elements.</p>
<blockquote>
<p>arrayName.push("element") when adding single element</p>
<p>arrayName.push("element","element2") adding multiple element</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];
<span class="hljs-keyword">let</span> newArrayLength = fruits.push(<span class="hljs-string">"Pear"</span>); <span class="hljs-comment">// this will return new array length </span>
<span class="hljs-built_in">console</span>.log(fruits); <span class="hljs-comment">// o/p -&gt; [ 'apple', 'grapes', 'banana', 'orange', 'guava', 'Pear' ]</span>
</code></pre>
<h2 id="heading-pop">pop()</h2>
<iframe src="https://giphy.com/embed/xT8qAZSxK7papPQDZK"></iframe>

<p><code>pop()</code> method removes the last element from the array and also returns the removed element. We use this method when we want to remove the last element from the array and use the returned element later.</p>
<blockquote>
<p>Syntax:</p>
<p>arrayName.pop()</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];
<span class="hljs-keyword">let</span> poppedElement = fruits.pop(); <span class="hljs-comment">// last element removed from the fruits array will be stored in poppedElement Variable</span>
<span class="hljs-built_in">console</span>.log(poppedElement); <span class="hljs-comment">// o/p -&gt; guava</span>
<span class="hljs-built_in">console</span>.log(fruits); <span class="hljs-comment">// o/p [ 'apple', 'grapes', 'banana', 'orange' ]</span>
</code></pre>
<h2 id="heading-shift">shift()</h2>
<p><code>shift()</code> method removes the first element i.e element at index 0, from the array and returns the removed element.</p>
<blockquote>
<p>Syntax:</p>
<p>arrayName.shit()</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];
<span class="hljs-comment">// fruits.shift(); // use this when you don't want to store the removed element</span>
<span class="hljs-keyword">let</span> removedElement = fruits.shift(); <span class="hljs-comment">// removes the first element from the fruits array and store the removed element</span>
<span class="hljs-comment">// fruits.shift();</span>
<span class="hljs-built_in">console</span>.log(removedElement); <span class="hljs-comment">// o/p -&gt; apple i.e first element of fruits array</span>
<span class="hljs-built_in">console</span>.log(fruits); <span class="hljs-comment">// o/p -&gt; [ 'grapes', 'banana', 'orange', 'guava' ]</span>
</code></pre>
<h2 id="heading-unshift">unshift()</h2>
<p><code>unshift()</code> method is used to add one or more elements at the start of the array, it also returns the new length of the array after adding the element.</p>
<blockquote>
<p>Syntax:</p>
<p>arrayName.unshift("element") // single element</p>
<p>arrayName.unshift("element1","element2") // multilple element</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];

<span class="hljs-comment">// Add "Pear" at the start of the array</span>
fruits.unshift(<span class="hljs-string">"Pear"</span>); <span class="hljs-comment">// adding one element</span>
<span class="hljs-built_in">console</span>.log(fruits); <span class="hljs-comment">// o/p -&gt; [ 'Pear', 'apple', 'grapes', 'banana', 'orange', 'guava' ]</span>

<span class="hljs-comment">// Add "Strawberry" and "Papaya" at the beginning  of the array</span>
fruits.unshift(<span class="hljs-string">"Strawberry"</span>, <span class="hljs-string">"Papaya"</span>); <span class="hljs-comment">// adding multiple element at the beginning of the array</span>
<span class="hljs-built_in">console</span>.log(fruits); <span class="hljs-comment">// o/p [ 'Strawberry',  'Papaya','Pear', 'apple', 'grapes', 'banana', 'orange', 'guava' ]</span>

<span class="hljs-comment">// Add "PineApple at the start of the array  and store the new length of the array in  newLength variable"</span>
<span class="hljs-keyword">let</span> newLength = fruits.unshift(<span class="hljs-string">"PineApple"</span>);
<span class="hljs-built_in">console</span>.log(newLength); <span class="hljs-comment">// o/p -&gt;    9</span>
</code></pre>
<h2 id="heading-sort">sort()</h2>
<p><code>sort()</code> method is used to sort the elements of the array. It sorts the strings in alphabetical order and numbers in ascending order. <code>sort()</code> the method doesn't use any extra space or use another array for sorting, it overwrites the original array and returns it.</p>
<blockquote>
<p>Syntax:</p>
<p>arrayName.sort()</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];
fruits.sort();
<span class="hljs-built_in">console</span>.log(fruits); <span class="hljs-comment">// o/p -&gt; [ 'apple', 'banana', 'grapes', 'guava', 'orange' ]</span>

<span class="hljs-keyword">let</span> marks = [<span class="hljs-number">23</span>, <span class="hljs-number">66</span>, <span class="hljs-number">22</span>, <span class="hljs-number">96.4</span>, <span class="hljs-number">84.3</span>];
<span class="hljs-comment">// displaying the returned array</span>
<span class="hljs-built_in">console</span>.log(marks.sort()); <span class="hljs-comment">// o/p -&gt; [ 22, 23, 66, 84.3, 96.4 ]</span>
</code></pre>
<h2 id="heading-reverse">reverse()</h2>
<p><code>reverse()</code> method is used when we want to reverse the original order of the array. <code>reverse()</code> method also doesn't use any extra space or use another array for sorting, it overwrites the original array and returns it.</p>
<blockquote>
<p>Syntax:</p>
<p>arrayName.reverse()</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];
fruits.reverse();
<span class="hljs-built_in">console</span>.log(fruits); <span class="hljs-comment">// o/p -&gt; [ 'guava', 'orange', 'banana', 'grapes', 'apple' ]</span>

<span class="hljs-keyword">let</span> marks = [<span class="hljs-number">23</span>, <span class="hljs-number">66</span>, <span class="hljs-number">22</span>, <span class="hljs-number">96.4</span>, <span class="hljs-number">84.3</span>];

<span class="hljs-comment">// displaying the returned value.</span>
<span class="hljs-built_in">console</span>.log(marks.reverse()); <span class="hljs-comment">// o/p -&gt; [ 84.3, 96.4, 22, 66, 23 ]</span>
</code></pre>
<h2 id="heading-splice">splice()</h2>
<p><code>splice()</code> method is used to remove, replace or add elements to the array. It returns an array of removed items.</p>
<blockquote>
<p>Syntax:</p>
<p>arrayName.splice(startIndex,deleteCount, item1, item2, n_item)</p>
<p>startIndex-&gt; The index from where you want to modify the array</p>
<p>deleteCount(optional) -&gt; It is the number of element you want to delete.</p>
<p>item1,item2,n_item(optional) -&gt;Elements you want to add to the start index. if this is not specfied,<code>splice()</code> wll only remove elements from the array.</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];

<span class="hljs-comment">// Add PineApple at index 1</span>
fruits.splice(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-string">"PineApple"</span>); <span class="hljs-comment">// no items is being removed so it will remove empty array.</span>
<span class="hljs-built_in">console</span>.log(removedFruits); <span class="hljs-comment">// o/p -&gt;[]</span>
<span class="hljs-built_in">console</span>.log(fruits); <span class="hljs-comment">// o/p -&gt; [ 'apple', 'PineApple', 'grapes', 'banana', 'orange', 'guava' ]</span>

<span class="hljs-comment">// now our fruits array contains following elements [ 'apple', 'PineApple', 'grapes', 'banana', 'orange', 'guava' ]</span>

<span class="hljs-comment">// Add 2 Elements "Watermelon" and "Strawberry"  starting at index 2</span>
fruits.splice(<span class="hljs-number">2</span>, <span class="hljs-number">0</span>, <span class="hljs-string">"Watermelon"</span>, <span class="hljs-string">"Strawberry"</span>);
<span class="hljs-built_in">console</span>.log(fruits);

<span class="hljs-comment">// Remove all the newly added elements from the array and store the removed element in a variable.</span>
<span class="hljs-keyword">let</span> removedFruits = fruits.splice(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>);
<span class="hljs-built_in">console</span>.log(removedFruits); <span class="hljs-comment">// 0/p -&gt; [ 'PineApple', 'Watermelon', 'Strawberry' ]</span>
<span class="hljs-built_in">console</span>.log(fruits);  <span class="hljs-comment">// o/p -&gt;[ 'apple', 'grapes', 'banana', 'orange', 'guava' ]</span>
</code></pre>
<h2 id="heading-slice">slice()</h2>
<p><code>slice()</code> method is used when we want a copy of an array's portion. It returns a copy of a small portion of the array from the original array without modifying the original array.</p>
<blockquote>
<p>Syntax:-</p>
<p>arrayName.slice(startIndex,stopIndex)</p>
<p>startIndex(optional) -&gt; Index from where you want to start the slicing.</p>
<p>stopIndex(optional) -&gt; Index at where you want to stop slicing. Rembember stopIndex is always exclusive element at this index is not included in the returned array.</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];

<span class="hljs-comment">// get a new array of "grapes", "banana" and "orange"</span>
<span class="hljs-keyword">let</span> newSlicedList = fruits.slice(<span class="hljs-number">1</span>, <span class="hljs-number">4</span>); <span class="hljs-comment">// stop index is 4 instead of 3 because stop index is exclusive.</span>
<span class="hljs-built_in">console</span>.log(newSlicedList); <span class="hljs-comment">// o/p -&gt;[ 'grapes', 'banana', 'orange' ]</span>
<span class="hljs-built_in">console</span>.log(fruits); <span class="hljs-comment">// o/p -&gt; [ 'apple', 'grapes', 'banana', 'orange', 'guava' ]</span>

<span class="hljs-built_in">console</span>.log(fruits.slice()); <span class="hljs-comment">// if no parameter is passed it slice the array from start to end</span>
</code></pre>
<h2 id="heading-concat">concat()</h2>
<p><code>concat()</code> method is used to join two or more arrays or values. It does not modify the original array instead it returns a new array with the values from both the array.</p>
<blockquote>
<p>Syntax:</p>
<p>arrayName.concat(array/value,array/value)</p>
<p>array/value: can be a single value or an array.</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];
<span class="hljs-keyword">let</span> anotherFruitsArray = [<span class="hljs-string">"PineApple"</span>, <span class="hljs-string">"Watermelon"</span>, <span class="hljs-string">"Strawberry"</span>];

<span class="hljs-comment">// create a new array which has all the elements from both the array.</span>
<span class="hljs-keyword">let</span> newJoinedArray = fruits.concat(anotherFruitsArray);
<span class="hljs-built_in">console</span>.log(newJoinedArray); <span class="hljs-comment">// ["apple","grapes", "banana", "orange","guava", "PineApple","Watermelon","Strawberry",];</span>
</code></pre>
<h2 id="heading-join">join()</h2>
<p><code>join()</code> method returns a new string by concatenating all of the elements of an array, separated by a comma or by the specified separator.</p>
<blockquote>
<p>Syntax:</p>
<p>arrayName.join(seprator)</p>
<p>seprator(optional) :- optional seprator string</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];

<span class="hljs-built_in">console</span>.log(fruits.join()); <span class="hljs-comment">// apple,grapes,banana,orange,guava</span>
<span class="hljs-built_in">console</span>.log(fruits.join(<span class="hljs-string">"-"</span>)); <span class="hljs-comment">// apple-grapes-banana-orange-guava</span>
</code></pre>
<h2 id="heading-includes">includes()</h2>
<p><code>includes()</code> method is used to check whether an element is in the array or not. It returns true if the element is in the array else it returns false.</p>
<blockquote>
<p>Syntax:</p>
<p>arrayName.includes(element)</p>
<p>element :- element you want to check for</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];

<span class="hljs-comment">// check if grapes is in the fruits array</span>
<span class="hljs-built_in">console</span>.log(fruits.includes(<span class="hljs-string">"grapes"</span>)); <span class="hljs-comment">// o/p-&gt; true</span>

<span class="hljs-comment">// check if "PineApple" in in the fruits array</span>
<span class="hljs-built_in">console</span>.log(fruits.includes(<span class="hljs-string">"PineApple"</span>)); <span class="hljs-comment">// o/p -&gt; false</span>
</code></pre>
<h2 id="heading-fill">fill()</h2>
<p><code>fill()</code> method fills the array with the specified value and returns the new modified array. It modifies the original array.</p>
<blockquote>
<p>Syntax:-</p>
<p>arrayName.fill(value,startIndex,stopIndex)</p>
<p>value -&gt; value you want to fill with</p>
<p>startIndex(optional) -&gt; strarting index from where you want to start filling the value. The default value for this is 0.</p>
<p>stopIndex(optional) -&gt; index where you want to stop the filling.rembember stopIndex is always exclusive. the default value for this is the length of the array.</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>];

<span class="hljs-comment">// fill "PineApple" from index 1 to index 3</span>
<span class="hljs-keyword">let</span> returnedArray = fruits.fill(<span class="hljs-string">"PineApple"</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>);
<span class="hljs-built_in">console</span>.log(returnedArray); <span class="hljs-comment">// [ 'apple', 'PineApple', 'PineApple', 'PineApple', 'guava' ]</span>
<span class="hljs-built_in">console</span>.log(fruits); <span class="hljs-comment">//[ 'apple', 'PineApple', 'PineApple', 'PineApple', 'guava' ]</span>

<span class="hljs-comment">// fill fruits array with "Watermelon"</span>
<span class="hljs-keyword">let</span> returnedArray2 = fruits.fill(<span class="hljs-string">"Watermelon"</span>);
<span class="hljs-built_in">console</span>.log(returnedArray); <span class="hljs-comment">// ["Watermelon", "Watermelon", "Watermelon", "Watermelon", "Watermelon"];</span>
<span class="hljs-built_in">console</span>.log(fruits); <span class="hljs-comment">// ["Watermelon", "Watermelon", "Watermelon", "Watermelon", "Watermelon"];</span>
</code></pre>
<h2 id="heading-indexof">indexOf()</h2>
<p><code>indexOf</code> method returns the index of the given element, if there are duplicate elements in the array then it returns the index of elements that occur first and if the element is not present in the array then it returns -1.</p>
<blockquote>
<p>Syntax:-</p>
<p>arrayName.indexOf(element)</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>, <span class="hljs-string">"apple"</span>];
<span class="hljs-comment">// find the index of "grapes"</span>
<span class="hljs-built_in">console</span>.log(fruits.indexOf(<span class="hljs-string">"grapes"</span>)); <span class="hljs-comment">// 1</span>

<span class="hljs-comment">// find the index of "apple"</span>

<span class="hljs-comment">// apple can be found at index 0 and at index 5, but this method only return index of element which occur first.</span>
<span class="hljs-built_in">console</span>.log(fruits.indexOf(<span class="hljs-string">"apple"</span>)); <span class="hljs-comment">// 0</span>

<span class="hljs-comment">// find the index of "PineApple"</span>
<span class="hljs-built_in">console</span>.log(fruits.indexOf(<span class="hljs-string">"PineApple"</span>)); <span class="hljs-comment">// -1 because "PineApple is not present in the array."</span>
</code></pre>
<h2 id="heading-isarray">isArray()</h2>
<p><code>isArray()</code> method checks if the object is an array. We know that everything in JavaScript is an object. If the object is an array then it returns true and if not then it returns false.</p>
<blockquote>
<p>Syntax:-</p>
<p>Array.isArray(arrayName)</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>, <span class="hljs-string">"apple"</span>];

<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Array</span>.isArray(fruits)); <span class="hljs-comment">// true</span>
</code></pre>
<h2 id="heading-keys">keys()</h2>
<p>The <code>keys()</code> method returns an Array Iterator object that contains the keys for each index in the array.</p>
<blockquote>
<p>Syntax:-</p>
<p>arrayName.keys()</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>, <span class="hljs-string">"apple"</span>];

<span class="hljs-keyword">let</span> KeyIterator = fruits.keys();

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> key <span class="hljs-keyword">of</span> iterator) {
  <span class="hljs-built_in">console</span>.log(key);
}

<span class="hljs-comment">// output</span>
<span class="hljs-comment">/*
0
1
2
3
4
5
*/</span>
</code></pre>
<h2 id="heading-values">values()</h2>
<p>The <code>values()</code> method creates a new iterator object that allows you to access the values of each index in the array. This iterator can be used to iterate through the values of the array in a loop.</p>
<blockquote>
<p>Syntax :-</p>
<p>arrayName.values()</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"guava"</span>, <span class="hljs-string">"apple"</span>];

<span class="hljs-keyword">let</span> valueIterator = fruits.values();

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> value <span class="hljs-keyword">of</span> valueIterator) {
  <span class="hljs-built_in">console</span>.log(value);
}

<span class="hljs-comment">// Output</span>
<span class="hljs-comment">/*
apple
grapes
banana
orange
guava
apple
*/</span>
</code></pre>
<h1 id="heading-not-the-end">Not the End <sup>😒</sup></h1>
<iframe src="https://giphy.com/embed/l3q2PKbEGcnuY6KE8"></iframe>

<p>That's it, for now, there are many more array methods that are left to cover in this article, I will be updating this article as I learn and explore more. Write your feedback in the comment section.</p>
]]></content:encoded></item><item><title><![CDATA[Getting Started With JavaScript.]]></title><description><![CDATA[JavaScript is one of the most popular programming languages. JavaScript allows us to add dynamic content to our web pages, and it allows us to make our web pages more interactive eg. You must have used dropdown menus, alerts, and other dynamic conten...]]></description><link>https://blog.ankitdevelops.in/getting-started-with-javascript</link><guid isPermaLink="true">https://blog.ankitdevelops.in/getting-started-with-javascript</guid><category><![CDATA[iwritecode]]></category><category><![CDATA[#iwritecode  #hiteshchoudhary  #LCO  #markdown #git  #github]]></category><category><![CDATA[#LCO]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Wed, 14 Dec 2022 09:10:31 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1670821712845/Ik0LogwiV.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>JavaScript is one of the most popular programming languages. JavaScript allows us to add dynamic content to our web pages, and it allows us to make our web pages more interactive eg. You must have used dropdown menus, alerts, and other dynamic content on the web pages, it might be possible that they all are achieved using JavaScript. It has been the most popular programming language for more than 5 to 6 years in a row.</p>
<h2 id="heading-why-javascript">Why JavaScript?</h2>
<iframe src="https://giphy.com/embed/5BWVeEbJsaB4UZAzSz" width="auto" height="auto"></iframe>

<p>As I have already mentioned that JavaScript is one of the most popular programming languages. In today's internet, almost more than 90% of website uses JavaScript, so there are always requirements for JavaScript Developers. In the early days, JavaScript was used on the client-side only but with help of runtime environments like <code>node</code> we can now run JavaScript also on the server-side.</p>
<blockquote>
<ul>
<li><p>client-side:code is downloaded as plain text and is executed on the user's computer only.</p>
</li>
<li><p>server-side: code is executed on the server, and only results are downloaded and displayed on the screen.</p>
</li>
</ul>
</blockquote>
<p>Now JavaScript has evolved so much that with help of JavaScript only you can develop webpages, Single Page Applications, mobile apps, desktop apps, backend applications, Game development and even now you can do machine learning with JavaScript, you can say that possibilities are endless.</p>
<h2 id="heading-a-pinch-of-history">A pinch of history.</h2>
<iframe src="https://giphy.com/embed/8OOgjViHEdxCynoKu0"></iframe>

<p>During the browser war between Microsoft (Internet Explorer) and Netscape (Netscape Navigator). In 1995 Netscape's engineer named <code>Brendan Eich</code> developed a new lightweight scripting language called <code>Mocha</code> , and later it was renamed to JavaScript. Soon after that in 1996 Microsoft also developed its own version of JavaScript and this brought compatibility issues to the website. To develop JavaScript, Netscape and Brendan Eich took it to the ECMA international standards organization in 1996. In 1997 the language specification called ECMAScript as a ECMA-262 standard was published. This specification could be used by other browser vendors to prepare their implementations.</p>
<h2 id="heading-getting-started">Getting Started</h2>
<p>Now, let's get started and write our first program in JavaScript, but where, we can run JavaScript directly in the browser console but we are not going to do that. For executing the JavaScript code we are going to use nodejs which is a runtime environment based on the chrome v8 engine, we will learn about the runtime environment and engine later and for writing the codes we are going to use vs code. Download these tools from their official website and install them in your system.</p>
<h3 id="heading-hello-world">Hello World</h3>
<p>Create a directory and open that directory in vs code, and create a new file in that directory <code>hello.js</code> notice the <code>.js</code> extension all JavaScript code files have <code>.js</code> extension. Write the following line in that file.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello World!"</span>); 
<span class="hljs-comment">// console.log is used to dislplay message in the console</span>
</code></pre>
<p>Open vs code inbuilt terminal by pressing <code>cltl + ` </code> together, and write<code>node hello.js</code> and press enter. This will display <code>Hello World!</code> message in your terminal. That's it we have written our first program in JavaScript. In the next article, we are going to learn about the values we can use in JavaScript.</p>
]]></content:encoded></item><item><title><![CDATA[Using media queries.]]></title><description><![CDATA[What are media queries?
It is a way of applying styles or running other code based on specific characteristics, features, and browser preferences. It allows us to conditionally use styles based on different media types like print, screen etc. media q...]]></description><link>https://blog.ankitdevelops.in/using-media-queries</link><guid isPermaLink="true">https://blog.ankitdevelops.in/using-media-queries</guid><category><![CDATA[iwritecode]]></category><category><![CDATA[#iwritecode  #hiteshchoudhary  #LCO  #markdown #git  #github]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Sun, 27 Nov 2022 14:08:15 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1669528607163/lWY4Nh46G.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-what-are-media-queries">What are media queries?</h2>
<p>It is a way of applying styles or running other code based on specific characteristics, features, and browser preferences. It allows us to conditionally use styles based on different media types like <code>print</code>, <code>screen</code> etc. media queries are mostly used to apply styles based on the viewport so that web developers can develop responsive websites across different screen sizes. In this article, we are going to discuss the same.</p>
<h2 id="heading-syntax">Syntax</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669530258606/6Pf1Sz7ZN.png" alt="Untitled-2022-11-27-1146.png" /></p>
<h3 id="heading-media"><code>@media</code></h3>
<p>Media queries use the <code>@media</code> rule to apply different styles to different types of media and devices based on the type of media.</p>
<h3 id="heading-screen"><code>screen</code></h3>
<p>the screen is the media type we are using here, it matches devices with a screen, and we can also use different media types like print to the printer.</p>
<h3 id="heading-and"><code>and</code></h3>
<p> The media query supports logical operators, which enable us to match media types based on certain conditions.</p>
<h3 id="heading-max-width"><code>max-width</code></h3>
<p><code>max-width</code> is the media feature we are defining here, and we can apply some styles when the features match. The max-width specifies a width less or equal to the specified width. All the styles will apply when the width is less than the specified width.</p>
<h3 id="heading-min-width"><code>min-width</code></h3>
<p>This is another media feature we can define instead of max-width, <code>min-width</code> specifies a width greater or equal to the specified width. All the styles will apply when the width is greater than the specified width.</p>
<h2 id="heading-example-1-min-width">Example 1 min-width</h2>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"X-UA-Compatible"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"IE=edge"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Media Queries<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
    <span class="hljs-keyword">@media</span> screen <span class="hljs-keyword">and</span> (<span class="hljs-attribute">min-width:</span> <span class="hljs-number">768px</span>) {
      <span class="hljs-selector-tag">body</span> {
        <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#3944f7</span>;
      }
      <span class="hljs-selector-tag">h1</span> {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">50px</span>;
      }
    }
  </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Media Queries<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p><img src="https://media.giphy.com/media/zy27jILVfgjRxAm0v7/giphy-downsized-large.gif" alt="gif" /></p>
<blockquote>
<p>Notice that in the above gif when I increase the window size to greater than 768 px our styles get applied, the background color changes, and the font size of the heading becomes 50px. Try running the code on your system and check the output.</p>
</blockquote>
<h2 id="heading-example-2-max-width">Example 2 max-width</h2>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"X-UA-Compatible"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"IE=edge"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Media Queries<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
    <span class="hljs-keyword">@media</span> screen <span class="hljs-keyword">and</span> (<span class="hljs-attribute">max-width:</span> <span class="hljs-number">768px</span>) {
      <span class="hljs-selector-tag">body</span> {
        <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#3944f7</span>;
      }
      <span class="hljs-selector-tag">h1</span> {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">50px</span>;
      }
    }
  </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Media Queries<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p><img src="https://media.giphy.com/media/4lQ9aEh6t9v4WmpwdB/giphy.gif" alt="gif" /></p>
<blockquote>
<p>Notice that in the above example styles only apply when the window width is less than and or equal to 768px.</p>
</blockquote>
<h2 id="heading-using-and-operator">Using <code>and</code> operator</h2>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"X-UA-Compatible"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"IE=edge"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Media Queries<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
    <span class="hljs-keyword">@media</span> screen <span class="hljs-keyword">and</span> (<span class="hljs-attribute">min-width:</span> <span class="hljs-number">768px</span>) <span class="hljs-keyword">and</span> (<span class="hljs-attribute">max-width:</span> <span class="hljs-number">1200px</span>) {
      <span class="hljs-selector-tag">body</span> {
        <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#3944f7</span>;
      }
      <span class="hljs-selector-tag">h1</span> {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">50px</span>;
      }
    }
  </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Media Queries<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p><img src="https://media.giphy.com/media/G619jPYPkY5GMxYMC8/giphy.gif" alt="gif" /></p>
<blockquote>
<p>Notice in the above example that styles only apply when the window width is greater than or equal to 768px and less than or equal to 1200px.</p>
</blockquote>
<h2 id="heading-using-print-media-type">Using <code>print</code> media-type</h2>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"X-UA-Compatible"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"IE=edge"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Media Queries<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
    <span class="hljs-selector-tag">body</span> {
      <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#3944f7</span>;
    }
    <span class="hljs-selector-tag">h1</span> {
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">100px</span>;
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#b4161b</span>;
    }
    <span class="hljs-keyword">@media</span> print {
      <span class="hljs-selector-tag">body</span> {
        <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#ffffff</span>;
      }
      <span class="hljs-selector-tag">h1</span> {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">20px</span>;
        <span class="hljs-attribute">color</span>: <span class="hljs-number">#000000</span>;
      }
    }
  </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Media Queries<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<blockquote>
<p>The print media type matches the document type viewed in the print preview</p>
</blockquote>
<p>webpage with normal styles</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669557593636/AMiK9rW9S.png" alt="Screenshot from 2022-11-27 19-29-14.png" /></p>
<p>styles applied on print preview</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669557995176/tk6m02Yjx.png" alt="Screenshot from 2022-11-27 19-29-25.png" /></p>
]]></content:encoded></item><item><title><![CDATA[An overview of Flexbox]]></title><description><![CDATA[What is Flexbox?
CSS Flexbox allows items to be laid out in a single dimension. With Flexbox's properties, we can control the dimensions, spaces, and positions of items. Flexbox is used for designing the one-dimensional layout, at a time you can eith...]]></description><link>https://blog.ankitdevelops.in/an-overview-of-flexbox</link><guid isPermaLink="true">https://blog.ankitdevelops.in/an-overview-of-flexbox</guid><category><![CDATA[iwritecode]]></category><category><![CDATA[#iwritecode  #hiteshchoudhary  #LCO  #markdown #git  #github]]></category><dc:creator><![CDATA[Ankit Kumar]]></dc:creator><pubDate>Mon, 21 Nov 2022 13:21:17 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1669034998361/USqsC9-aa.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-what-is-flexbox">What is Flexbox?</h2>
<p>CSS Flexbox allows items to be laid out in a single dimension. With Flexbox's properties, we can control the dimensions, spaces, and positions of items. Flexbox is used for designing the one-dimensional layout, at a time you can either design your layout in columns or rows. Everything in flexbox depends on these two terms main axis and cross-axis they are perpendicular to each other. The main axis is determined by the flex-direction property. </p>
<p>Flexbox works with the concept of containers and items, every element which is to be layout needs to be inside a container, this container is known as a flex container and the element is known as a flex-items. Let’s try to understand this with an example.</p>
<p>This is the HTML file we are going to use for this article, in this HTML document we have a div element with the class as container and it has five div child elements.</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"X-UA-Compatible"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"IE=edge"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Positioning<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
    <span class="hljs-selector-class">.container</span> {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">90vw</span>;
      <span class="hljs-attribute">height</span>: <span class="hljs-number">80vh</span>;
      <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid <span class="hljs-number">#242b2e</span>;
      <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;

      <span class="hljs-comment">/* changes below this*/</span>
    }
    <span class="hljs-selector-class">.box</span> {
      <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#242b2e</span>;
      <span class="hljs-attribute">height</span>: <span class="hljs-number">100px</span>;
      <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;
            <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span>;
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">40px</span>;
      <span class="hljs-attribute">text-align</span>: center;
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#ffffff</span>;
    }
  </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box box-1"</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box box-2"</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box box-3"</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box box-4"</span>&gt;</span>4<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box box-5"</span>&gt;</span>5<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669035141247/isNmfixje.png" alt="f1.png" />
As you can see all five boxes are stacked one after another, let’s add <code>display: flex</code> to the container.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">600px</span>;
      <span class="hljs-attribute">height</span>: <span class="hljs-number">500px</span>;
      <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid <span class="hljs-number">#242b2e</span>;
      <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;

      <span class="hljs-comment">/* changes below this*/</span>
      <span class="hljs-attribute">display</span>: flex;
    }
    <span class="hljs-selector-class">.box</span> {
      <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#242b2e</span>;
      <span class="hljs-attribute">height</span>: <span class="hljs-number">100px</span>;
      <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;
      <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span>;
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">40px</span>;
      <span class="hljs-attribute">text-align</span>: center;
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#ffffff</span>;
    }
</code></pre>
<p> Adding <code>display: flex</code> to the container element will define it as a flex container and all the flex items will be layout according to their main axis depending on the <code>flex-direction</code> by default the <code>flex: direction</code> is set to row so horizontal is our main axis, so the item will layout in a row.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669035248652/ebYaPFbF1.png" alt="f2.png" />
Now let’s change the flex-direction to a column and see the changes.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">600px</span>;
      <span class="hljs-attribute">height</span>: <span class="hljs-number">500px</span>;
      <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid <span class="hljs-number">#242b2e</span>;
      <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;

      <span class="hljs-comment">/* changes below this*/</span>
      <span class="hljs-attribute">display</span>: flex;
      <span class="hljs-attribute">flex-direction</span>: column;
    }
    <span class="hljs-selector-class">.box</span> {
      <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#242b2e</span>;
      <span class="hljs-attribute">height</span>: <span class="hljs-number">100px</span>;
      <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;
      <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span>;
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">40px</span>;
      <span class="hljs-attribute">text-align</span>: center;
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#ffffff</span>;
    }
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669035357326/vOtbsm-97.png" alt="f3.png" />
As you can see when we change the flex-direction to the column, the column becomes the main axis and the horizontal becomes the cross axis. </p>
<h2 id="heading-flex-direction">Flex direction</h2>
<p>The <code>flex-direction</code> property is used to establish the main axis. <code>flex-direction</code> takes 4 values <code>row</code> <code>column</code> <code>row-reverse</code> and <code>column-reverse.</code> On webpages, the default value of `flex-direction is set to row.</p>
<h3 id="heading-flex-direction-column">flex-direction: column</h3>
<p>This property set’s the column as the main axis, so items are arranged from top to bottom.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669035632871/fe6MS50HO.png" alt="Screenshot from 2022-11-20 19-14-24.png" /></p>
<h3 id="heading-flex-directionrow-reverse">flex-direction:row-reverse</h3>
<p>This property reverses the items from left to right to right to left and moves the element toward the end of the container.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669035678857/b9mJ-tHv-.png" alt="Screenshot from 2022-11-20 19-40-56.png" /></p>
<h3 id="heading-flex-direction-column-reverse">flex-direction: column-reverse</h3>
<p>This property reverses the items from top to bottom to bottom to top and moves the item toward the end of the container.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669035737067/k9O55TpOc.png" alt="Screenshot from 2022-11-20 19-42-41.png" /></p>
<h2 id="heading-flex-wrap">flex-wrap</h2>
<p>Flexbox is a one-dimensional layout system, so by default, all the items will try to fit in one line, but we can allow items to wrap and move to the next line, just like word wrap in vs code. We can pass three values to <code>flex-wrap</code> <code>nowrap</code> <code>wrap</code> <code>wrap-reverse</code> By default, flex-wrap is set to no-wrap <code>flex-wrap: nowrap</code>.</p>
<h3 id="heading-flex-wrap-wrap"><code>flex-wrap: wrap</code></h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669036065577/x_KZIFtPi.png" alt="Screenshot from 2022-11-20 19-54-53.png" />
When we change the size of the container items move to the next line.</p>
<h3 id="heading-flex-wrapwrap-reverse"><code>flex-wrap:wrap-reverse</code></h3>
<p>Applying <code>wrap-reverse</code> will wrap items to the next line but from bottom to top.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669036107415/UVHH_VWSM.png" alt="Screenshot from 2022-11-20 19-59-52.png" /></p>
<h2 id="heading-justify-content">justify-content</h2>
<p><code>justify-content</code> is used to align the items along the main axis. This is generally used to distribute the space between the items. <code>justify-content</code> takes many values but we are going to discuss the values that are used most.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669036164719/agwADqOGG.png" alt="justify-content center (2).png" /></p>
<h3 id="heading-justify-content-center"><code>justify-content: center</code></h3>
<blockquote>
<p>the <code>justify-content</code> center is used to move the items to the center of the main axis.</p>
</blockquote>
<h3 id="heading-justify-content-flex-start"><code>justify-content: flex-start</code></h3>
<blockquote>
<p>This is used to move the items to the start of the main axis. This is also the default value.</p>
</blockquote>
<h3 id="heading-justify-content-flex-end"><code>justify-content: flex-end</code></h3>
<blockquote>
<p>This property is used to move the items to the end of the main axis.</p>
</blockquote>
<h3 id="heading-justify-contentspace-between"><code>justify-content:space-between</code></h3>
<blockquote>
<p>This property moves the first item to the start of the main axis and the last item to the last of the main axis and leftover space is event distributed between the items.</p>
</blockquote>
<h3 id="heading-justify-content-space-around"><code>justify-content: space-around</code></h3>
<blockquote>
<p>This property is used to evenly distributes the items, with equal space around them.</p>
</blockquote>
<h3 id="heading-justify-content-space-evenly"><code>justify-content: space-evenly</code></h3>
<blockquote>
<p>This property is used to distribute equal space around the items.</p>
</blockquote>
<h2 id="heading-align-items">align-items</h2>
<p><code>align-items</code> is used to control the alignment of items along the cross-axis. <code>align-items</code> takes the following values <code>flex-start</code> <code>flex-end</code> <code>center</code> <code>stretch</code> <code>baseline.</code></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669036396106/5khPoJVug.png" alt="justify-content center (1) (1).png" /></p>
<h3 id="heading-align-items-center"><code>align-items: center</code></h3>
<blockquote>
<p>This property is used to align items to the center along the cross-axis.</p>
</blockquote>
<h3 id="heading-align-items-flex-start"><code>align-items: flex-start</code></h3>
<blockquote>
<p>This property is used to align and move the items to the start of the container along the cross-axis.</p>
</blockquote>
<h3 id="heading-align-items-flex-end"><code>align-items: flex-end</code></h3>
<blockquote>
<p>This property is used to align and move the items to the end of the container along the cross-axis.</p>
</blockquote>
<h3 id="heading-align-itemsstretch"><code>align-items:stretch</code></h3>
<blockquote>
<p>This property is used to stretch and fill the items along the cross-axis.</p>
</blockquote>
<h2 id="heading-align-content">align-content</h2>
<p><code>align-content</code> is very similar to <code>justify-content</code> property. This is also used to distribute the space between the items but along the cross-axis. <code>align-content</code> takes the following values. <code>flex-start</code> <code>flex-end</code> <code>center</code> <code>stretch</code> <code>space-between</code> and <code>space-around</code></p>
<p><em>Now let’s discuss some properties which can be applied only to the items themselves.</em></p>
<h2 id="heading-order">order</h2>
<p>order property is used to control the order of the items. By default, all the flex items have an order value of 0. The order of items is controlled by the order property. </p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">50%</span>;
      <span class="hljs-attribute">height</span>: <span class="hljs-number">500px</span>;
      <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid <span class="hljs-number">#242b2e</span>;
      <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;

      <span class="hljs-comment">/* changes below this*/</span>
      <span class="hljs-attribute">display</span>: flex;
    }
    <span class="hljs-selector-class">.box</span> {
      <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#242b2e</span>;
      <span class="hljs-attribute">height</span>: <span class="hljs-number">100px</span>;
      <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;
      <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span>;
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">40px</span>;
      <span class="hljs-attribute">text-align</span>: center;
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#ffffff</span>;
    }

    <span class="hljs-selector-class">.box-1</span> {
      <span class="hljs-attribute">order</span>: <span class="hljs-number">3</span>;
    }
    <span class="hljs-selector-class">.box-4</span> {
      <span class="hljs-attribute">order</span>: -<span class="hljs-number">1</span>;
    }
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669036528838/tQnhEIw3n.png" alt="Screenshot from 2022-11-20 23-52-48.png" /></p>
<h2 id="heading-flex-grow">flex-grow</h2>
<p>It specifies the amount by which an item will grow relative to the rest of the flexible items in the same container.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1669036546603/qE15kT8XE.png" alt="Screenshot from 2022-11-21 00-07-04.png" /></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>The article only discusses the most commonly used flexbox properties. If you want to learn more about flexbox, CSS Tricks has a great article on it. That's all for this article, let me know what you think in the comments. </p>
]]></content:encoded></item></channel></rss>