<?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[Kritika Pattalam Bharathkumar | Web Developer - The official blog of Kritika Pattalam Bharathkumar]]></title><description><![CDATA[Kritika Pattalam BharathKumar - Web developer. I write technical blogs about general programming, web development, CSS, JavaScript, React JS etc.]]></description><link>https://blog.kritikapattalam.com</link><generator>RSS for Node</generator><lastBuildDate>Thu, 23 Apr 2026 00:25:18 GMT</lastBuildDate><atom:link href="https://blog.kritikapattalam.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[HTML Roadmap]]></title><description><![CDATA[Here is a list of things one should learn while starting with HTML.
Note: Though this does not cover all that HTML has to offer, but covers a list of things one should know to start with web development.
HTML Roadmap
1. HTML main level tags

Doctype
...]]></description><link>https://blog.kritikapattalam.com/html-roadmap</link><guid isPermaLink="true">https://blog.kritikapattalam.com/html-roadmap</guid><category><![CDATA[HTML5]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[Frontend Development]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Wed, 05 Jan 2022 18:39:27 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1641407406793/meNHNXBMI.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Here is a list of things one should learn while starting with HTML.
<strong>Note:</strong> Though this does not cover all that HTML has to offer, but covers a list of things one should know to start with web development.</p>
<h3 id="heading-html-roadmap">HTML Roadmap</h3>
<h4 id="heading-1-html-main-level-tags">1. HTML main level tags</h4>
<ul>
<li>Doctype</li>
<li>title tag</li>
<li>meta tags</li>
<li>link tag,</li>
<li>script tag</li>
<li>body tag</li>
<li>HTML comments</li>
<li>adding a favicon</li>
<li>og:image tag</li>
</ul>
<h4 id="heading-2-html-semantic-elements">2. HTML semantic elements</h4>
<ul>
<li>header</li>
<li>footer</li>
<li>section</li>
<li>aside</li>
<li>nav</li>
<li>article</li>
<li>main</li>
</ul>
<h4 id="heading-3-list-of-other-most-commonly-used-html-tags">3. List of other most commonly used HTML tags</h4>
<ul>
<li>div <ul>
<li>paragraph tag</li>
<li>span</li>
<li>anchor tag</li>
<li>list, ordered list, unordered list</li>
<li>iframes</li>
<li>heading tags</li>
<li>dl /dt, datalist</li>
<li>br, hr</li>
</ul>
</li>
</ul>
<h4 id="heading-4-table-elements">4. table elements</h4>
<ul>
<li>thead</li>
<li>tbody</li>
<li>td</li>
<li>tr</li>
<li>tfoot</li>
<li>th</li>
</ul>
<h4 id="heading-5-form-elements">5. Form elements</h4>
<ul>
<li>form</li>
<li>different input types (text, email, phone, color, date, URL, radio, checkbox,etc)</li>
<li>textarea</li>
<li>select</li>
<li>button</li>
<li>label</li>
</ul>
<h4 id="heading-6-multimedia-tags">6. Multimedia tags</h4>
<ul>
<li>canvas</li>
<li>video</li>
<li>audio</li>
<li>svg</li>
<li>progress</li>
<li>img</li>
</ul>
<h4 id="heading-7-html-events-good-to-know-when-using-javascript">7. HTML events -[Good to know when using JavaScript]</h4>
<p>Mostly used</p>
<ul>
<li>window events </li>
<li>keyboard events</li>
<li>mouse events
Used if using the specific tags like form/media tags etc</li>
<li>form events</li>
<li>media events</li>
<li>drag events</li>
<li>clipboard events</li>
</ul>
<h4 id="heading-8-html-global-attributes-on-elements">8. HTML global attributes on elements</h4>
<ul>
<li>class</li>
<li>id</li>
<li>data-* (custom attribute)</li>
<li>tabindex</li>
<li>lang</li>
</ul>
<p>The above is a list of items one should know as a beginner to get started with web development. Do you have more suggestions on few other things one should learn when starting with HTML? Let me know in the comments below.</p>
<p>Follow me on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related tips and posts. Feedbacks and suggestions are welcome.</p>
<h4 id="heading-are-you-more-of-a-twitter-person-then-check-out-the-short-version-of-this-blog-in-the-below-thread">Are you more of a Twitter person?. Then check out the short version of this blog in the below thread.</h4>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://twitter.com/KritikaPattalam/status/1476011358185533449">https://twitter.com/KritikaPattalam/status/1476011358185533449</a></div>
]]></content:encoded></item><item><title><![CDATA[Do you use git? Then this is for you...]]></title><description><![CDATA[For anyone who uses git, git add is the most commonly used terminal command to add files. But have you ever wondered if you can add only newly created files or just a specific type of file? Well, you can, and what's more interesting is you can do mor...]]></description><link>https://blog.kritikapattalam.com/different-ways-to-use-git-add</link><guid isPermaLink="true">https://blog.kritikapattalam.com/different-ways-to-use-git-add</guid><category><![CDATA[GitHub]]></category><category><![CDATA[Git]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Mon, 06 Dec 2021 05:03:24 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1638766584032/VdX4Yo8wG.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>For anyone who uses git, <strong>git add</strong> is the most commonly used terminal command to add files. But have you ever wondered if you can add only newly created files or just a specific type of file? Well, you can, and what's more interesting is you can do more than that with git add.</p>
<p>Let's dive right in!!!</p>
<h3 id="heading-1-add-all-files">1) Add all files</h3>
<p>The below line can be used to select all files such as</p>
<ul>
<li><p>newly created files</p>
</li>
<li><p>deleted files</p>
</li>
<li><p>modified files</p>
</li>
</ul>
<pre><code class="lang-javascript">git add .
</code></pre>
<h3 id="heading-2-add-just-a-specified-file">2) Add just a specified file</h3>
<p>Lets assume that you edited multiple files, but you want to add just a specific file to the commit, then use the below command</p>
<blockquote>
<p>git add &lt; path-to-file &gt;</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-comment">//eg: add just the CSS file from the specified path</span>
git add apps/project-structure/styles.css
</code></pre>
<h3 id="heading-3-add-files-with-a-specific-extension">3) Add files with a specific extension</h3>
<p>Using the below command one can choose a specific type of file, e.g: One can choose to add just HTML files, CSS files or JavaScript files, etc.</p>
<blockquote>
<p>git add *.&lt; file-extension &gt;</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-comment">//eg: add all files with extension scss</span>
git add *.scss
</code></pre>
<h3 id="heading-4-add-all-files-under-a-particular-directory">4) Add all files under a particular directory</h3>
<p>Using the below command one can add all the files under a particular directory path</p>
<blockquote>
<p>git add &lt; directory &gt;</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-comment">//eg: add all files under the folder project-test</span>
git add apps/project-test/
</code></pre>
<h3 id="heading-5-add-all-files-which-are-usually-ignored-via-gitignore">5) Add all files which are usually ignored via gitignore</h3>
<p>Using the below command one can add all the files which are generally ignored via gitignore</p>
<blockquote>
<p>git add -f</p>
</blockquote>
<p>or</p>
<blockquote>
<p>git add --force</p>
</blockquote>
<h3 id="heading-6-add-all-files-except-deleted-files">6) Add all files except deleted files</h3>
<p>Using the below command one can add all the files i.e newly created files and modified files, but this command will ignore the deleted files</p>
<blockquote>
<p>git add --ignore-removal</p>
</blockquote>
<h3 id="heading-7-add-all-files-except-new-files">7) Add all files except new files</h3>
<p>Using the below command one can add all the files i.e modified files and deleted files, but this command will ignore newly created files</p>
<blockquote>
<p>git add -u</p>
</blockquote>
<p>or</p>
<blockquote>
<p>git add --update</p>
</blockquote>
<p>Follow me on <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a> | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related tips and posts. Feedbacks and suggestions are welcome.</p>
<h4 id="heading-are-you-more-of-a-twitter-person-then-check-out-the-short-version-of-this-blog-in-the-below-thread">Are you more of a Twitter person? Then check out the short version of this blog in the below thread.</h4>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://twitter.com/KritikaPattalam/status/1419770034851876869">https://twitter.com/KritikaPattalam/status/1419770034851876869</a></div>
]]></content:encoded></item><item><title><![CDATA[Create a DOWNLOAD link in a single line of HTML]]></title><description><![CDATA[As a website user, one would have come across links or buttons which say "Click to download" or "Download"  on a website. Have you ever wondered how can you add one to your website?.  Keep reading!!!. 
As part of this blog let us see how one can crea...]]></description><link>https://blog.kritikapattalam.com/create-a-download-link-in-html</link><guid isPermaLink="true">https://blog.kritikapattalam.com/create-a-download-link-in-html</guid><category><![CDATA[HTML5]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Tue, 19 Oct 2021 15:12:50 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1634655997596/0boUCjpXQ.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As a website user, one would have come across links or buttons which say "Click to download" or "Download"  on a website. Have you ever wondered how can you add one to your website?.  Keep reading!!!. </p>
<p>As part of this blog let us see how one can create a download link or button which lets you download an asset (e.g. image, pdf, files, etc.).  Do you know what is even more interesting?. This can be done with just a single line of HTML.</p>
<h3 id="how-to-create-a-download-link-or-button-in-html">How to create a download link or button in HTML</h3>
<p>The simple way to achieve this is </p>
<ul>
<li>create an anchor tag with the text of your choice e.g "Click to download"</li>
<li>to the href attribute add the path to the URL of the asset you want to be downloaded. In the below example,  I have added a path to an image.</li>
<li>next comes the more important part, add the<em> <strong>download </strong></em>attribute to the anchor tag.</li>
</ul>
<p>Yay!!! You have created your download link now.</p>
<h4 id="how-do-i-make-this-look-like-a-button">How do I make this look like a button?.</h4>
<p>Simply add some CSS to make the anchor tag look like a button.</p>
<pre><code><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://blog.kritikapattalam.com/images/click-me.jpg"</span> <span class="hljs-attr">download</span>&gt;</span>Click to download<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
</code></pre><h3 id="save-the-file-using-a-custom-name">Save the file using a custom name</h3>
<p>In the above-given example, when the user clicks on download, it will save the file as "click-me.jpg", since that is the file name specified in the href attribute.</p>
<h4 id="what-about-if-you-want-to-give-it-a-different-name">What about if you want to give it a different name?.</h4>
<p>Simply add the name as a value for the download attribute. For example in the below code, I have added <strong>download = test</strong>, hence when the user clicks on download the file will be saved as test.jpg</p>
<pre><code><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://blog.kritikapattalam.com/images/click-me.jpg"</span> <span class="hljs-attr">download</span>=<span class="hljs-string">"test"</span>&gt;</span>Click to download<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
</code></pre><h4 id="can-the-assethref-value-be-of-a-different-domain">Can the asset/href value be of a different domain?</h4>
<p>NO, they cannot. Download attribute works only for files from the same Origin URL's (i.e the file should be hosted on the same origin as the site you are trying to implement it in) or blob or data URL files.</p>
<p>In the below example, href value is from twitter.com whereas my site is blog.kritikapattalam.com. In this case, the download button will not work.</p>
<pre><code><span class="hljs-comment">&lt;!--- Site origin - https://blog.kritikapattalam.com ---&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://twitter.com/images/click-me.jpg"</span> <span class="hljs-attr">download</span>=<span class="hljs-string">"test"</span>&gt;</span>Click to download<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
</code></pre><p>Well, that is it for this blog, and by now you should know how to create a download on your website.</p>
<p>Follow me on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related tips and posts. Feedbacks and suggestions are welcome.</p>
<h4 id="are-you-more-of-a-twitter-person-then-check-out-the-short-version-of-this-blog-in-the-below-thread">Are you more of a Twitter person?. Then check out the short version of this blog in the below thread.</h4>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://twitter.com/KritikaPattalam/status/1414245816064225280">https://twitter.com/KritikaPattalam/status/1414245816064225280</a></div>
]]></content:encoded></item><item><title><![CDATA[First Step to Learning Vue JS - 3]]></title><description><![CDATA[As part of this blog, let's see how to get started with Vue JS -3. 
Just like any other tech stack, the first step is to print a hello world, as much as anyone can say that it is the easiest thing. But that first step can be overwhelming for anyone w...]]></description><link>https://blog.kritikapattalam.com/first-step-to-learning-vue-js-3</link><guid isPermaLink="true">https://blog.kritikapattalam.com/first-step-to-learning-vue-js-3</guid><category><![CDATA[Vue.js]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Sun, 26 Sep 2021 01:37:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1632236980199/lLo9M_8uC.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As part of this blog, let's see how to get started with Vue JS -3. 
Just like any other tech stack, the first step is to print a hello world, as much as anyone can say that it is the easiest thing. But that first step can be overwhelming for anyone who is very new to programming. Hence this blog post.</p>
<h4 id="project-structure">Project Structure</h4>
<ul>
<li>index.html</li>
<li>script.js</li>
</ul>
<h3 id="indexhtml">index.html</h3>
<p>There are 3 things that need to be done, in order to get our HTML running</p>
<ol>
<li>Include the VUE-JS package</li>
<li>Add a div placeholder to display our static message "Hello World"</li>
<li>Include our custom Vue JS script which is going to hold our "Hello World" message</li>
</ol>
<h4 id="1-include-the-vue-js-package">1. Include the Vue Js package</h4>
<p>The first step to starting with a Vue project is including the Vue package. As part of this simple project I am going to include the below script tag in the head section of the HTML. If you are planning to use it in production, probably would want to consider using versioned resources.</p>
<pre><code><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/vue@next"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre><h4 id="2-add-a-placeholder-div">2. Add a placeholder div</h4>
<p>Include a div with the id "hello". This id will be used in our custom script and inside this is where you will display the message from the custom script</p>
<pre><code><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"hello"</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span> Display my message here<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre><h4 id="3-include-the-custom-vue-js-script">3. Include the custom Vue JS script</h4>
<p>Create a file called script.js inside which we will write our script to display our static message, and this file should be included as the last thing in the body section. The main reason to include this as the last item is, the script gets included in the sequential order. </p>
<pre><code><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">id</span>=<span class="hljs-string">"hello"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span> Display my message here<span class="hljs-tag">&lt;/<span class="hljs-name">h1</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">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"script.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
 <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
</code></pre><h4 id="why-should-the-script-be-included-at-the-end-of-the-body">Why should the script be included at the end of the body?</h4>
<p>So let's assume if this script was added in the head section, what happens is, the Vue js package script first gets executed, and the next script to execute will be the script.js. Script.js has code that looks for the div with <strong>id=hello</strong> and it will throw an error. This is because, the div hello is added after the script in the body section, hence it will not be available yet for the script to push the custom message into that div. </p>
<p>Hence include the script.js as the last file in the body, so that the DOM is available when our custom script executes.</p>
<h3 id="scriptjs">script.js</h3>
<p>In the below script </p>
<ul>
<li>Create helloWorld, which when called will return the message "Hello World - This is Vue 3"</li>
<li>Next create a Vue App, which calls the helloWorld which in turn will return the message and insert it inside the div with the id hello.</li>
</ul>
<pre><code>## Final Script
<span class="hljs-keyword">const</span> helloWorld = {
  <span class="hljs-keyword">data</span>() {
    <span class="hljs-keyword">return</span> {
        message: <span class="hljs-string">"Hello World - This is Vue 3"</span>
    }
  }
}

Vue.createApp(helloWorld).mount(<span class="hljs-string">'#hello'</span>);
</code></pre><p>Now save the above script file and what do you see?.
Nothing yet, you will still be seeing the h1 with the value "Display my message here"  that is because we need to access the message in the HTML. This can be done using text interpolation.
Replace the dummy message with <strong>{{message}}</strong></p>
<pre><code> <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"hello"</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{{ message }}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre><h4 id="final-html">Final HTML</h4>
<pre><code><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">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1"</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Hello World Vue - 3<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/vue@next"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</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">body</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"hello"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{{ message }}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</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">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"script.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</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>Now save the above Final HTML and Final Javascript, when you open the HTML in the browser you should be seeing the below output</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632619345128/0lnKM94mT.png" alt="E_X8w2vX0AAFFVO.png" /></p>
<h3 id="bonus">Bonus</h3>
<p>Well, we saw how to display a static message, how about trying to get the input from the user and display that message instead.</p>
<p>I am going to include a v-model directive, which accepts input from the user, and as and when the user types, the message gets displayed on the page. This is called two-way data binding, this can be done by just updating the HTML as below</p>
<pre><code><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"hello"</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{{ message }}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">message</span>  /&gt;</span>
 <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre><h3 id="output">Output</h3>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://codepen.io/kpattalam/pen/oNwpYaa">https://codepen.io/kpattalam/pen/oNwpYaa</a></div>
<h4 id="references-vue-js-docshttpsv3vuejsorgguideinstallationhtml">References -  <a target="_blank" href="https://v3.vuejs.org/guide/installation.html">Vue JS Docs</a></h4>
<p>Follow me on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related tips and posts. Feedbacks and suggestions are welcome.</p>
<p>Checkout my other blog post series</p>
<ul>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/html">HTML</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/css">CSS</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/javascript">JavaScript</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/web-development">Web Development</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Display random quotes from Chuck Norris API]]></title><description><![CDATA[What a fun way to learn JavaScript than by building projects. In my previous blog post, I showed how one can  build a random quote generator using arrays, it was simple and beginner-friendly. It would have helped one understand how to get started whe...]]></description><link>https://blog.kritikapattalam.com/random-quotes-from-chuck-norris-api</link><guid isPermaLink="true">https://blog.kritikapattalam.com/random-quotes-from-chuck-norris-api</guid><category><![CDATA[APIs]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Tue, 14 Sep 2021 02:55:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1631308864796/vpw-RglRn.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>What a fun way to learn JavaScript than by building projects. In my previous blog post, I showed how one can  <a target="_blank" href="https://blog.kritikapattalam.com/build-a-random-quote-generator-using-javascript">build a random quote generator using arrays</a>, it was simple and beginner-friendly. It would have helped one understand how to get started when building projects or how to address a problem. </p>
<p>I thought let me take this one step above and show how one can create a similar random quote generator using an API, and I am going to be using the famous Chuck Norris API for this. </p>
<p>Let's get started.</p>
<h4 id="what-will-we-learn-as-part-of-this-blog">What will we learn as part of this blog</h4>
<ul>
<li>function declaration vs function expression</li>
<li>window.onload function</li>
<li>Fetch() method</li>
<li>attaching onclick eventlistener to an element</li>
</ul>
<h3 id="what-will-be-the-end-result">What will be the end result</h3>
<ul>
<li>Create random quotes on button click. The quotes are going to be from the Chuck Norris API</li>
<li>Add a dropdown category filter that will display random quotes from the category chosen.</li>
</ul>
<h3 id="project-structure">Project structure</h3>
<p>Create the list of files as listed below</p>
<ol>
<li>index.html</li>
<li>script.js</li>
<li>style.css [ styles if we have any. As part of this blog, I am not going to add any styles.].</li>
</ol>
<h3 id="html">HTML</h3>
<p>HTML is going to be very simple</p>
<ol>
<li><p><strong>Select field for the category</strong>-  A simple select field with a list of all categories available in the Chuck Norris API. The options from inside the select field are created by the retrieved list from here 
<a target="_blank" href="https://api.chucknorris.io/jokes/categories">Retrieve the categories</a>
https://api.chucknorris.io/jokes/categories</p>
</li>
<li><p><strong>Section to display Quotes</strong> - A div "display_facts", is going to have the img tag which is going to have the chuck Norris icon from the API, and a p tag which will be populated with the quote from the API</p>
</li>
<li><p><strong>Generate Quote button</strong> - This is the button onclick of which we will generate/retrieve a new quote from the API.</p>
</li>
</ol>
<pre><code><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">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Random Quote Generator<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"styles.css"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"script.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</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">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">"outer-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">"heading"</span>&gt;</span>
                   <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Chuck Norris Facts - Select category from the dropdown<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
                       <span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"category"</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"all"</span>&gt;</span>All<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"animal"</span>&gt;</span>Animal<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"career"</span>&gt;</span>Career<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"celebrity"</span>&gt;</span>Celebrity<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"explicit"</span>&gt;</span>Explicit<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"fashion"</span>&gt;</span>Fashion<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"food"</span>&gt;</span>Food<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"history"</span>&gt;</span>History<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"money"</span>&gt;</span>Money<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"movie"</span>&gt;</span>Movie<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"music"</span>&gt;</span>Money<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"political"</span>&gt;</span>Political<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"religion"</span>&gt;</span>Religion<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"science"</span>&gt;</span>Science<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"sport"</span>&gt;</span>Sport<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"travel"</span>&gt;</span>Travel<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">select</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">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"display_facts"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">""</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"icon"</span> /&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"quote"</span>&gt;</span>Author<span class="hljs-tag">&lt;/<span class="hljs-name">p</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">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"generate"</span>&gt;</span>Generate<span class="hljs-tag">&lt;/<span class="hljs-name">button</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><h3 id="javascript">JavaScript</h3>
<p>Let's break this down into four steps</p>
<ol>
<li>Create an array inside a function and call that function on window load.</li>
<li>Check what is Chuch Norris API capable of.</li>
<li>Using fetch method retrieve all the results</li>
<li>Generate quote on button click</li>
<li>Let's generate quotes based on the category selected.</li>
</ol>
<h4 id="1-create-an-array-inside-a-function-and-call-that-function-on-window-load">1) Create an array inside a function and call that function on window load.</h4>
<p>This can be done in two ways using <strong>function expression</strong> or vs <strong>function declaration</strong>.  Read more about it  <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function">here</a> .
The main difference with function expression is they are not hoisted, you cannot call the function expression before they are declared.</p>
<ul>
<li>In the below example, I created a function called <strong>generateQuote</strong>, which contains a console.log hi. </li>
<li>Next inside window.onload function I call the function generateQuote.</li>
</ul>
<p>Now save this file, and open index.html in your browser. In the chrome developer tool console, you should see the text "hi". What happens here is,  once the entire window including the DOM &amp; assets is loaded, generateQuote function is called.</p>
<blockquote>
<p>As per MDN docs, The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links, and sub-frames have finished loading.</p>
</blockquote>
<pre><code><span class="hljs-keyword">const</span> generateQuote = <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">"Hi"</span>);
}
<span class="hljs-built_in">window</span>.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    generateQuote();
 }
</code></pre><h3 id="2-check-what-is-chuch-norris-api-capable-of">2. Check what is Chuch Norris API capable of.</h3>
<p>As part of this step, we are going to retrieve the results from the Chuck Norris API</p>
<p>Let's first see go check out the API </p>
<p> <a target="_blank" href="https://api.chucknorris.io/">ChuckNorris API</a> </p>
<p>An important tip for anyone trying to use an API is to read the documentation of the API, they usually have answers to all your questions as to how to use them and what is the API capable of doing. </p>
<p>When reading the API docs, what we concur is that the API is capable of doing the following things</p>
<ul>
<li>Retrieve random quotes</li>
<li>Retrieve a list of random quotes based on a list of categories</li>
<li>Retrieve the list of category</li>
<li>Retrieve quotes based on the text entered by the user.</li>
</ul>
<h3 id="3-using-fetch-method-retrieve-all-the-results">3. Using fetch method retrieve all the results</h3>
<p>As per the docs, the URL to fetch the results in a JSON format is to use the below URL</p>
<pre><code>//Retrieve a random chuck joke <span class="hljs-keyword">in</span> <span class="hljs-type">JSON</span> <span class="hljs-keyword">format</span>.
<span class="hljs-keyword">GET</span> https://api.chucknorris.io/jokes/random
</code></pre><p>Just try opening this URL  <a target="_blank" href="https://api.chucknorris.io/jokes/random">https://api.chucknorris.io/jokes/random</a> in a browser window, you should see a similar response that is a single random quote with below details </p>
<ul>
<li>The time when the random joke was created</li>
<li>an icon image of Chuck Norris</li>
<li>id for that particular quote</li>
<li>the updated date</li>
<li>a URL to the quote on the Chuck Norris API page</li>
<li>value - which is actually the quote that will be displayed on our page.</li>
</ul>
<pre><code><span class="hljs-comment">//The result has been formatted for a better view</span>
{
   <span class="hljs-attr">"categories"</span>:[

   ],
   <span class="hljs-attr">"created_at"</span>:<span class="hljs-string">"2020-01-05 13:42:24.142371"</span>,
   <span class="hljs-attr">"icon_url"</span>:<span class="hljs-string">"https://assets.chucknorris.host/img/avatar/chuck-norris.png"</span>,
   <span class="hljs-attr">"id"</span>:<span class="hljs-string">"DciuboTEQxqEaVG0Qd2T2w"</span>,
   <span class="hljs-attr">"updated_at"</span>:<span class="hljs-string">"2020-01-05 13:42:24.142371"</span>,
   <span class="hljs-attr">"url"</span>:<span class="hljs-string">"https://api.chucknorris.io/jokes/DciuboTEQxqEaVG0Qd2T2w"</span>,
   <span class="hljs-attr">"value"</span>:<span class="hljs-string">"Chuck Norris flashes the peace sign a lot because it's the easiest way to go for the eyes."</span>
}
</code></pre><p>Now try refreshing the API URL and you should see a different response. Let's go ahead and see how to add this via code and display it on the page.</p>
<p>The fetch method accepts one parameter which is the URL and returns a promise, hence we can use "then" which will return as a raw JSON first, and then once that raw JSON retrieve is successful, </p>
<ul>
<li>we then retrieve the icon </li>
</ul>
<blockquote>
<p>this.data.icon_url</p>
</blockquote>
<ul>
<li>and the value(which is the quote here)</li>
</ul>
<blockquote>
<p>this.data.value</p>
</blockquote>
<p>and display it into the respective DOMS using their HTML id. </p>
<p>If at all there is an error, then the promise will return the console.log inside the catch statement.</p>
<pre><code><span class="hljs-keyword">const</span> generateQuote = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
   <span class="hljs-keyword">let</span> url =<span class="hljs-string">"https://api.chucknorris.io/jokes/random"</span>;

    fetch(url)
    .then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>) </span>{
        <span class="hljs-keyword">return</span> response.json(); 
    }) <span class="hljs-comment">// Getting the raw JSON data</span>
    .then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">data</span>) </span>{

        <span class="hljs-comment">// Storing the quotes internally upon </span>
        <span class="hljs-comment">// successful completion of request</span>
        <span class="hljs-built_in">this</span>.data = data; 

        <span class="hljs-comment">// Displaying the quote When the Webpage loads</span>
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.data);
        <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"icon"</span>).src = <span class="hljs-built_in">this</span>.data.icon_url;
        <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"quote"</span>).innerHTML = <span class="hljs-built_in">this</span>.data.value;
}).catch(<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">"error"</span>);
});

}
<span class="hljs-built_in">window</span>.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    generateQuote();
}
</code></pre><p>Now, let's run the above piece of code and you should be seeing different quotes every single time you refresh your index.html. When you refresh the page, generateQuote a function inside the window.load gets executed, trying to fetch the new quote every refresh. </p>
<h3 id="4-generate-quote-on-button-click">4. Generate quote on button click</h3>
<p>This can easily be done by adding a click event listener, which calls the generateQuote function onclick.</p>
<pre><code><span class="hljs-built_in">window</span>.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    generateQuote();
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"generate"</span>).addEventListener(<span class="hljs-string">'click'</span>, generateQuote);
}
</code></pre><h3 id="5-lets-generate-quotes-based-on-the-category-selected">5. Let's generate quotes based on the category selected.</h3>
<p>To retrieve random quotes the API URL to be used is below</p>
<blockquote>
<p>GET https://api.chucknorris.io/jokes/random?category={category}</p>
</blockquote>
<p>In this URL, {category} will be the value chosen from the dropdown .e</p>
<p>eg: https://api.chucknorris.io/jokes/random?category=movie</p>
<p>So in order to retrieve the results based on category is going to be done in just a few steps</p>
<ol>
<li><p>Retrieve the chosen category value from the dropdown</p>
<blockquote>
<p>let category =  document.getElementById("category").value;</p>
</blockquote>
</li>
<li><p>Pass the value of the category to the URL. The URL to use if you see below is decided based on an if/else condition. On the initial page load or if the user decides to view quotes from all the categories then we use the URL from the else condition, if the category chosen is something other than the option "all" eg. movie or sports then we use the URL inside the "if" condition.</p>
</li>
</ol>
<pre><code> <span class="hljs-keyword">let</span> url;
   <span class="hljs-keyword">let</span> category =  document.getElementById(<span class="hljs-string">"category"</span>).<span class="hljs-keyword">value</span>;

   <span class="hljs-keyword">if</span>(category !== <span class="hljs-string">"all"</span>) {
     url =<span class="hljs-string">"https://api.chucknorris.io/jokes/random?category="</span> + category;
   } <span class="hljs-keyword">else</span> {
        url =<span class="hljs-string">"https://api.chucknorris.io/jokes/random"</span>;
   }
</code></pre><h4 id="final-javascript">Final JavaScript</h4>
<pre><code><span class="hljs-keyword">const</span> generateQuote = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
   <span class="hljs-keyword">let</span> url;
   <span class="hljs-keyword">let</span> category =  <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"category"</span>).value;

   <span class="hljs-keyword">if</span>(category !== <span class="hljs-string">"all"</span>) {
     url =<span class="hljs-string">"https://api.chucknorris.io/jokes/random?category="</span> + category;
   } <span class="hljs-keyword">else</span> {
        url =<span class="hljs-string">"https://api.chucknorris.io/jokes/random"</span>;
   }

    fetch(url)
    .then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>) </span>{
        <span class="hljs-keyword">return</span> response.json(); 
    }) <span class="hljs-comment">// Getting the raw JSON data</span>
    .then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">data</span>) </span>{

        <span class="hljs-comment">// Storing the quotes internally upon </span>
        <span class="hljs-comment">// successful completion of request</span>
        <span class="hljs-built_in">this</span>.data = data; 

        <span class="hljs-comment">// Displaying the quote When the Webpage loads</span>
        <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.data);
        <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"icon"</span>).src = <span class="hljs-built_in">this</span>.data.icon_url;
        <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"quote"</span>).innerHTML = <span class="hljs-built_in">this</span>.data.value;
}).catch(<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">"error"</span>);
});

}
<span class="hljs-built_in">window</span>.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    generateQuote();
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"generate"</span>).addEventListener(<span class="hljs-string">'click'</span>, generateQuote);
}
</code></pre><h3 id="output-working-demo">Output - Working Demo</h3>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://codepen.io/kpattalam/pen/qBjjqJd">https://codepen.io/kpattalam/pen/qBjjqJd</a></div>
<h3 id="references">References</h3>
<ul>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions">Function declaration vs Function expression</a> </li>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener">addEventListener</a> </li>
<li>[Fetch Method] (https://developer.mozilla.org/en-US/docs/Web/API/fetch)</li>
</ul>
<p>Follow me on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related tips and posts. Feedbacks and suggestions are welcome.</p>
<p>Checkout my other blog post series</p>
<ul>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/html">HTML</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/css">CSS</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/javascript">JavaScript</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/web-development">Web Development</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Build a Random Quote Generator using JavaScript]]></title><description><![CDATA[The best way to learn to code is by practice and by building small projects. As part of this blog, let's build a beginner-friendly random quote generator in JavaScript.
Check out my previous blog post  Build a Simple Clock using JavaScript. 
What wil...]]></description><link>https://blog.kritikapattalam.com/build-a-random-quote-generator-using-javascript</link><guid isPermaLink="true">https://blog.kritikapattalam.com/build-a-random-quote-generator-using-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Frontend Development]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Wed, 01 Sep 2021 00:57:04 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1630444332377/2jPNuOveqM.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The best way to learn to code is by practice and by building small projects. As part of this blog, let's build a beginner-friendly random quote generator in JavaScript.</p>
<p>Check out my previous blog post  <a target="_blank" href="https://blog.kritikapattalam.com/build-a-simple-clock-using-javascript">Build a Simple Clock using JavaScript.</a> </p>
<h4 id="what-will-we-learn-as-part-of-this-blog">What will we learn as part of this blog</h4>
<ul>
<li>function declaration vs function expression</li>
<li>window.onload function</li>
<li>Math.random()</li>
<li>Math.floor</li>
<li>attaching onclick eventlistener to an element</li>
</ul>
<p>Let's get started!!!.</p>
<h3 id="project-structure">Project structure</h3>
<p>Create the list of files as listed below</p>
<ol>
<li>index.html</li>
<li>script.js</li>
<li>style.css [ styles if we have any. As part of this blog, I am not going to add any styles.].</li>
</ol>
<h3 id="html-structure-indexhtml">HTML Structure - index.html</h3>
<p>HTML is going to be very simple, nothing fancy here.
In the below HTML, </p>
<ul>
<li>link the stylesheet inside the head tag</li>
<li>link the javascript file inside the head tag</li>
<li>Inside the body tag we are going to have <pre><code>   A main <span class="hljs-keyword">outer</span> container div which <span class="hljs-keyword">is</span> going <span class="hljs-keyword">to</span> hold the below elements
   - a paragraph tag <span class="hljs-keyword">to</span> display the <span class="hljs-keyword">quote</span>
   - a paragraph tag <span class="hljs-keyword">to</span> display the author
   - a button - onclick <span class="hljs-keyword">of</span> which we will generate random quotes
</code></pre></li>
</ul>
<pre><code><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">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Random Quote Generator<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"styles.css"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"script.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</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">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">"outer-container"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"quotes"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"author"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"generate"</span>&gt;</span>Generate<span class="hljs-tag">&lt;/<span class="hljs-name">button</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><h3 id="javascript">JavaScript</h3>
<p>Let's break this down into four steps</p>
<ol>
<li>Create an array inside a function and call that function on window load.</li>
<li>Retrieve the quotes/author from the array.</li>
<li>Onclick of the button, pick a random quote(index) from the array to display.</li>
<li>Use the index generated to display the quotes and author into the paragraph tag using innerHTML.</li>
<li>Call the function onclick</li>
</ol>
<h4 id="1-create-an-array-inside-a-function-and-call-that-function-on-window-load">1) Create an array inside a function and call that function on window load.</h4>
<p>This can be done in two ways using <strong>function expression</strong> or vs <strong>function declaration</strong>.  Read more about it  <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function">here</a> .
The main difference with function expression is they are not hoisted, you cannot call the function expression before they are declared.</p>
<ul>
<li>In the below example, I created a function called <strong>generateQuote</strong>, which contains an array called <strong>quotes</strong> and a console.log hi. I have declared <strong>quotes</strong> as a <strong>const</strong> variable because the value of the array is going to be always constant and will never change throughout this project.</li>
<li>Next inside window.onload function I call the function generateQuote.</li>
</ul>
<p>Now save this file, and open index.html in your browser. In the chrome developer tool console, you should see the text "hi". What happens here is,  once the entire window including the DOM &amp; assets is loaded, generateQuote function is called.</p>
<blockquote>
<p>As per MDN docs, The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links, and sub-frames have finished loading.</p>
</blockquote>
<pre><code>const generateQuote = <span class="hljs-keyword">function</span>() {
    const quotes = [
    {
        <span class="hljs-keyword">quote</span>: "Do not pity the dead, Harry. Pity the living, and, above all those who live without love.",
        author: "Albus Dumbledore"
    },
    {
        <span class="hljs-keyword">quote</span>: "It is impossible to manufacture or imitate love",
        author: "Horace Slughorn"
    },
    {
        <span class="hljs-keyword">quote</span>: "Being different isn't a bad thing. I mean that you are brave enough to be yourself.",
        author: "Luna Lovegood"
    },
    {
        <span class="hljs-keyword">quote</span>: "If you want to know what a man’s like, take a good look at how he treats his inferiors, not his equals.",
        author: "Sirius Black"
    },
    {
        <span class="hljs-keyword">quote</span>: "Never trust anything that can think for itself if you can’t see where it keeps its brain.",
        author: "Arthur Weasley"
    },
    {
        <span class="hljs-keyword">quote</span>: "Every human life is worth the same, and worth saving.",
        author: "Kingsley Shacklebolt"
    },
    {
        <span class="hljs-keyword">quote</span>: "Have a biscuit, Potter.",
        author: "Minerva McGonagall"
    },
    {
        <span class="hljs-keyword">quote</span>: "Happiness can be found even in the darkest of times if one only remembers to turn on the light.",
        author: "Albus Dumbledore"
    },
    {
        <span class="hljs-keyword">quote</span>: "Socks are Dobby’s favorite, favorite clothes, sir!",
        author: "Dobby"
    }
];
console.log("Hi");
}
<span class="hljs-keyword">window</span>.onload = <span class="hljs-keyword">function</span>() {
    generateQuote();
 }
</code></pre><h4 id="2-lets-see-how-to-retrieve-the-quotesauthor-from-the-array">2. Let's see how to retrieve the quotes/author from the array</h4>
<p>First, let's see how to get the array values. Array values can be retrieved by their indexes. The index of an array starts from 0 to arraylength - 1. This means array index 0 will have the below values</p>
<pre><code> {
    <span class="hljs-attribute">quote</span>: <span class="hljs-string">"Do not pity the dead, Harry. Pity the living, and, above all those who live without love."</span>,
     author: <span class="hljs-string">"Albus Dumbledore"</span>
  }
</code></pre><p>So if you want to print the fourth value, our index would be 3 [Remeber the index starts from 0 ] , so let's try to access the values for quotes and author using the index</p>
<pre><code><span class="hljs-comment">//Syntax : arrayVariableName[index]</span>
console<span class="hljs-selector-class">.log</span>(<span class="hljs-attribute">quotes</span><span class="hljs-selector-attr">[3]</span><span class="hljs-selector-class">.quote</span>); <span class="hljs-comment">// If you want to know what a man’s like, take a good look at how he treats his inferiors, not his equals.</span>
console<span class="hljs-selector-class">.log</span>(<span class="hljs-attribute">quotes</span><span class="hljs-selector-attr">[3]</span><span class="hljs-selector-class">.author</span>); <span class="hljs-comment">// Sirius Black</span>
</code></pre><h4 id="3-onclick-of-the-button-pick-a-random-quoteindex-from-the-array-to-display">3. Onclick of the button, pick a random quote(index) from the array to display.</h4>
<p>The next thing we want to do is pick a random quote from the array. In the previous step we saw that we can access the quote using the index number, so we are going to need a random number to pick the quote. </p>
<blockquote>
<p>As per MDN Docs, Math.random() returns a floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive).</p>
</blockquote>
<p>Since math.random returns a floating-point number eg: 0.65 and in order to access the array values we need an integer so let's use math.floor which will give us an integer</p>
<p>As per MDN Docs, Math.floor() returns a number representing the largest integer less than or equal to the specified number.</p>
<p>in order to return a more efficient number let's multiply the random number with the array length and use math.floor which will return us an integer and save that value in a variable.</p>
<pre><code> <span class="hljs-keyword">let</span> arrayIndex = <span class="hljs-built_in">Math</span>.floor(<span class="hljs-built_in">Math</span>.random() * quotes.length);
</code></pre><h4 id="4-use-the-index-generated-to-display-the-quotes-and-author-into-the-paragraph-tag-using-innerhtml">4. Use the index generated to display the quotes and author into the paragraph tag using innerHTML</h4>
<pre><code><span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"quotes"</span>).innerHTML = quotes[arrayIndex].quote;
<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"author"</span>).innerHTML = quotes[arrayIndex].author;
</code></pre><p>Now try saving the file, and when you open the index.html in the browser you should see a quote and author being displayed. When you refresh the browser, you should see a different quote</p>
<h4 id="5-call-the-function-onclick">5. Call the function onclick</h4>
<p>In the previous step, we were able to see different quotes being displayed every time when we refresh the browser because for every refresh the function gets called, which returns a random index number, displaying a random quote. </p>
<p>So now let's attach a eventlistener onclick to the button, which when clicked will call the generateQuote function</p>
<blockquote>
<p>As per MDN docs, The EventTarget method addEventListener() sets up a function that will be called whenever the specified event is delivered to the target.</p>
</blockquote>
<pre><code><span class="hljs-comment">//Final JavaScript</span>
<span class="hljs-keyword">const</span> generateQuote = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> quotes = [
    {
        <span class="hljs-attr">quote</span>: <span class="hljs-string">"Do not pity the dead, Harry. Pity the living, and, above all those who live without love."</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Albus Dumbledore"</span>
    },
    {
        <span class="hljs-attr">quote</span>: <span class="hljs-string">"It is impossible to manufacture or imitate love"</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Horace Slughorn"</span>
    },
    {
        <span class="hljs-attr">quote</span>: <span class="hljs-string">"Being different isn't a bad thing. I means that you are brave enough to be yourself."</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Luna Lovegood"</span>
    },
    {
        <span class="hljs-attr">quote</span>: <span class="hljs-string">"If you want to know what a man’s like, take a good look at how he treats his inferiors, not his equals."</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Sirius Black"</span>
    },
    {
        <span class="hljs-attr">quote</span>: <span class="hljs-string">"Never trust anything that can think for itself if you can’t see where it keeps its brain."</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Arthur Weasley"</span>
    },
    {
        <span class="hljs-attr">quote</span>: <span class="hljs-string">"Every human life is worth the same, and worth saving."</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Kingsley Shacklebolt"</span>
    },
    {
        <span class="hljs-attr">quote</span>: <span class="hljs-string">"Have a biscuit, Potter."</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Minerva McGonagall"</span>
    },
    {
        <span class="hljs-attr">quote</span>: <span class="hljs-string">"Happiness can be found even in the darkest of times, if one only remembers to turn on the light."</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Albus Dumbledore"</span>
    },
    {
        <span class="hljs-attr">quote</span>: <span class="hljs-string">"Socks are Dobby’s favorite, favorite clothes, sir!"</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Dobby"</span>
    }
];

    <span class="hljs-keyword">let</span> arrayIndex = <span class="hljs-built_in">Math</span>.floor(<span class="hljs-built_in">Math</span>.random() * quotes.length);
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"quotes"</span>).innerHTML = quotes[arrayIndex].quote;
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"author"</span>).innerHTML = quotes[arrayIndex].author;

}
<span class="hljs-built_in">window</span>.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    generateQuote();
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"generate"</span>).addEventListener(<span class="hljs-string">'click'</span>, generateQuote);
}
</code></pre><h3 id="output-working-demo">Output - Working Demo</h3>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://codepen.io/kpattalam/pen/MWoKGmy">https://codepen.io/kpattalam/pen/MWoKGmy</a></div>
<h4 id="conclusion">Conclusion</h4>
<p>Now you should have a working random quote generator, which generates random quotes onclick of a button. Now go ahead and try to load different quotes every few minutes or an hour. [<strong>Tip</strong>: Call the function inside setInterval, instead of onclick]</p>
<h3 id="references">References</h3>
<ul>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions">Function declaration vs Function expression</a> </li>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener">addEventListener</a> </li>
<li>[Math random] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random)</li>
<li>[Math floor] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor)</li>
</ul>
<p>Follow me on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related tips and posts. Feedbacks and suggestions are welcome.</p>
<p>Checkout my other blog post series</p>
<ul>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/html">HTML</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/css">CSS</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/javascript">JavaScript</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/web-development">Web Development</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Build a Simple Clock  using JavaScript]]></title><description><![CDATA[The best way to learn to code is by practicing and building small projects. As part of this blog, let's build a simple clock that will print the local time of the user using JavaScript. This is great for beginners and for someone who has started with...]]></description><link>https://blog.kritikapattalam.com/build-a-simple-clock-using-javascript</link><guid isPermaLink="true">https://blog.kritikapattalam.com/build-a-simple-clock-using-javascript</guid><category><![CDATA[2Articles1Week]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Mon, 23 Aug 2021 01:39:55 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1629671429893/qAmnxUg1F.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The best way to learn to code is by practicing and building small projects. As part of this blog, let's build a simple clock that will print the local time of the user using JavaScript. This is great for beginners and for someone who has started with JavaScript.</p>
<h4 id="heading-what-will-we-learn-as-part-of-this-blog">What will we learn as part of this blog?</h4>
<ol>
<li><p>Defining a function using function expression vs function declaration</p>
</li>
<li><p>document.getElementById</p>
</li>
<li><p>setInterval</p>
</li>
<li><p>Element.innerHTML</p>
</li>
<li><p>Date.prototype.toLocaleDateString()</p>
</li>
</ol>
<p>Let's get started!!!.</p>
<h3 id="heading-project-structure">Project structure</h3>
<p>Create the list of files as listed below</p>
<ol>
<li><p>index.html</p>
</li>
<li><p>script.js</p>
</li>
<li><p>style.css [ styles if we have any. As part of this blog, I am not going to add any styles as such.].</p>
</li>
</ol>
<h3 id="heading-html-structure-indexhtml">HTML Structure - index.html</h3>
<p>In the below HTML,</p>
<ul>
<li><p>link the stylesheet inside the head tag</p>
</li>
<li><p>link the javascript file inside the head tag what will be present in those two files we will see later in the post.</p>
</li>
</ul>
<p>The next step is, inside the body tag add an element of your choice with an id attribute. I have added a paragraph as the element with the id value as a clock. This paragraph is where we are going to display our time.</p>
<pre><code class="lang-xml"><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">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Simple Clock using JavaScript<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"styles.css"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"script.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</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">body</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Simple Clock - Current Time<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"clock"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</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>
<h3 id="heading-script-scriptjs">Script - script.js</h3>
<p>Let's break this down into steps</p>
<ol>
<li><p>Create a function called clock and call the function.</p>
</li>
<li><p>Create a date object and retrieve the local time.</p>
</li>
<li><p>Call the function clock every second so that the clock gets updated with the new value.</p>
</li>
</ol>
<h4 id="heading-1-create-a-function-called-clock-and-call-the-function">1. Create a function called clock and call the function.</h4>
<p>This can be done in two ways using <strong>function expression</strong> or vs <strong>function declaration</strong>. Read more about it <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function">here</a> . The main difference with function expression is they are not hoisted, you cannot call the function expression before they are declared.</p>
<p>In the below example, I have defined a function called clock which has console.log, and then I call the function at the end of the script. Save the script file and when you open the index.html, you should see "Hi" in the developer tool.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//function expression</span>
<span class="hljs-keyword">const</span> clock = <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">"Hi"</span>);
}
<span class="hljs-comment">// or</span>
<span class="hljs-comment">//function declaration</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">clock</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hi"</span>);
}
clock();
</code></pre>
<h4 id="heading-2-create-a-date-object-and-retrieve-the-local-time">2. Create a date object and retrieve the local time.</h4>
<blockquote>
<p>new Date() When called as a constructor, returns a new Date object.</p>
</blockquote>
<ul>
<li>toLocaleTimeString returns the current local time in a string format.</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> clock = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> getDate = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>();
    <span class="hljs-built_in">console</span>.log(getDate.toLocaleTimeString());
}
</code></pre>
<h4 id="heading-3-display-the-time-inside-the-paragraph-tag">3. Display the time inside the paragraph tag</h4>
<ul>
<li><p>In the previous step, we retrieved the value of the time, now we should display that value inside the paragraph tag.</p>
</li>
<li><p>First, let's grab the element using its ID "clock"</p>
</li>
</ul>
<blockquote>
<p>The Document method getElementById() returns an Element object representing the element whose id property matches the specified string.</p>
</blockquote>
<ul>
<li>Into that element let's insert the time value returned by the date object. Using innerHTML we can set the value.</li>
</ul>
<p>Now let's view this in the browser, what you should be seeing is, the current local time when the browser loaded for the first time. If you refresh the browser, you will see that it updates the time to the current minute. So every single time you want to see the current time, you will have to refresh the browser.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> clock = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> getDate = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>();
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'clock'</span>).innerHTML = getDate.toLocaleTimeString();
}
</code></pre>
<h4 id="heading-4-call-the-function-using-setinterval-every-few-seconds">4. Call the function using setInterval, every few seconds.</h4>
<p>With the last update, in order to see the current time, we had to refresh the browser every time so that the function gets called on refresh/page load and retrieves the current time. But a clock is supposed to update on its own, which means we will need the clock function to be executed every second to retrieve the current value. This is where we use the setInterval function.</p>
<blockquote>
<p>setInterval executes a function continuously for the given milliseconds.</p>
</blockquote>
<p>Let's add the clock function inside the setInterval, now the clock function is repeatedly called every 1000 seconds.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> clock = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> getDate = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>();
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'clock'</span>).innerHTML = getDate.toLocaleTimeString();
}
<span class="hljs-built_in">setInterval</span>(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{ clock();}, <span class="hljs-number">1000</span>);
</code></pre>
<h3 id="heading-output">Output</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629682378037/JHkYxByG1.gif" alt="clock.gif" /></p>
<h3 id="heading-references">References</h3>
<ul>
<li><p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions">Function declaration vs Function expression</a></p>
</li>
<li><p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString">date object</a></p>
</li>
</ul>
<p>Follow me on <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a> | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related tips and posts. Feedbacks and suggestions are welcome.</p>
<p>Checkout my other blog post series</p>
<ul>
<li><p><a target="_blank" href="https://blog.kritikapattalam.com/series/html">HTML</a></p>
</li>
<li><p><a target="_blank" href="https://blog.kritikapattalam.com/series/css">CSS</a></p>
</li>
<li><p><a target="_blank" href="https://blog.kritikapattalam.com/series/javascript">JavaScript</a></p>
</li>
<li><p><a target="_blank" href="https://blog.kritikapattalam.com/series/web-development">Web Development</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Do you know what "use strict" does in JavaScript?]]></title><description><![CDATA[As JavaScript developers, we often have "use strict" - strict mode enabled in our projects. But do we know what that expression does? Let's find out.
What is a strict mode?
JavaScript's strict mode makes JS be executed in strict mode, which helps ide...]]></description><link>https://blog.kritikapattalam.com/use-strict-mode-in-javascript</link><guid isPermaLink="true">https://blog.kritikapattalam.com/use-strict-mode-in-javascript</guid><category><![CDATA[2Articles1Week]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Frontend Development]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Sat, 21 Aug 2021 00:09:33 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1629503111292/jE2zYsNXL.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As JavaScript developers, we often have "use strict" - strict mode enabled in our projects. But do we know what that expression does? Let's find out.</p>
<h3 id="heading-what-is-a-strict-mode">What is a strict mode?</h3>
<p>JavaScript's strict mode makes JS be executed in strict mode, which helps identify common mistakes in coding practices which might lead to potential bugs.</p>
<p><strong>Syntax</strong></p>
<p>"use strict;"</p>
<p>As part of this blog, let's see a list of things to remember while using strict mode in JavaScript</p>
<ol>
<li><p>Declaring strict mode</p>
</li>
<li><p>Declaring a variable/object in strict mode</p>
</li>
<li><p>Deleting a variable/object in strict mode</p>
</li>
<li><p>Naming variables in strict mode</p>
</li>
<li><p>Duplicating a parameter in a function</p>
</li>
<li><p>Octal numeric literals are not allowed</p>
</li>
<li><p>Eval function to create a variable</p>
</li>
<li><p>Writing to a read-only property is not allowed</p>
</li>
<li><p>Writing to a get-only property is not allowed</p>
</li>
<li><p>'this' in strict mode</p>
</li>
</ol>
<h3 id="heading-below-is-the-list-of-few-things-which-are-identified-as-issues-in-strict-mode-vs-without-one">Below is the list of few things which are identified as issues in strict mode vs without one</h3>
<h3 id="heading-1-declaring-strict-mode">1. Declaring strict mode</h3>
<p>The <strong>"use strict"</strong> directive/expression is only recognized at the beginning of a script or a function.</p>
<h4 id="heading-declare-strict-mode-at-the-top-of-the-file">Declare strict mode at the top of the file</h4>
<p>Strict mode can be declared at the top of the file as the first thing. When declared at the top it takes a global scope. In the below piece of code, use strict is declared as the first thing on the script file, making it a global scope, causing the strict mode feature to be enabled for the whole file.</p>
<pre><code class="lang-plaintext">"use strict";
alert("I am part of the file");

function test() {
   console.log("javascript use strict is declared at the top of the file");
}
test();
</code></pre>
<h4 id="heading-what-happens-when-strict-mode-is-declared-inside-a-function-and-not-at-the-top">What happens when strict mode is declared inside a function and not at the top?</h4>
<p>When used inside a function it has local scope and only that function will execute in strict mode. In the example below, the strict mode will be enabled only inside the function(local scope), and the code outside it will not follow the strict rules.</p>
<pre><code class="lang-plaintext">function log(x) {
    "use strict;"
    console.log(x);
}
log(5);
</code></pre>
<h4 id="heading-what-to-avoid">What to avoid?</h4>
<p>In the example below, use strict is not the first line &amp; should be avoided. Preferably it's better to declare it on the top of the file due to global scope, instead of declaring it inside a function.</p>
<pre><code class="lang-plaintext">alert("I am inside this file");

"use strict;" //not declared as the first line
console.log("javascript use strict is not declared at the first thing");
</code></pre>
<h3 id="heading-2-variableobject-declaring-in-strict-mode">2. Variable/Object declaring in strict mode</h3>
<p>Using a variable/object without declaring it is not allowed in strict mode. In the example below, <strong>myVar</strong> variable is assigned a value before declaring, this will throw an error.</p>
<p><strong>What happens if strict mode is not enabled?</strong> - myVar will just be created as a global variable in that scenario.</p>
<pre><code class="lang-plaintext">"use strict;"
myVar = 5; // This will throw an error
function log(x) {
    console.log(x);
}
log(5);
</code></pre>
<h3 id="heading-3-deleting-a-variableobjectfunction">3. Deleting a variable/object/function</h3>
<p>Deleting a variable or object or function when in strict mode enabled is not possible.</p>
<pre><code class="lang-plaintext">use strict;
let myVar = 5; // This will throw an error
function log(x) {
    console.log(x);
}
log(6);
delete log; //throws an error
delete myVar; //throws an error
</code></pre>
<h3 id="heading-4-naming-variables-in-strict-mode">4. Naming variables in strict mode</h3>
<p>When learning JS for the first time, the thing which we are sure often would have read is "Do not use reserved words as variable names".</p>
<p>But at times people do tend to use reserved words without realizing it, in such cases strict mode helps by throwing an error. Just try it out in a chrome developer tool console and one should be able to see the difference.</p>
<pre><code class="lang-plaintext">"use strict;"
var static = "hello";
//static is a reserved word, hence when using strict mode, it will throw below error
// Uncaught SyntaxError: Unexpected strict mode reserved word
</code></pre>
<h3 id="heading-5-duplicating-a-parameter-in-a-function">5. Duplicating a parameter in a function</h3>
<p>The example below shows that the x parameter in the function log has been duplicated. This is not allowed in strict mode.</p>
<pre><code class="lang-plaintext">use strict;
// This will throw an error because "x" parameter is duplicated
function log(x,y,x) {
    console.log(x);
}
log(6,10);
</code></pre>
<h3 id="heading-6-octal-numeric-literals-are-not-allowed">6. Octal numeric literals are not allowed</h3>
<p>In the example below, one might assume a preceding zero before 1 does nothing and the value might just be 12, but in JavaScript preceding zero before a number means its Octal numeric. Strict mode does not allow octal numeric.</p>
<pre><code class="lang-plaintext">'use strict';
 // Will throw an error
 let x = 012;
</code></pre>
<h3 id="heading-7-eval-function-to-create-a-variable-is-not-allowed">7. Eval function to create a variable is not allowed</h3>
<pre><code class="lang-plaintext">"use strict";
eval("var x = 1");// error
</code></pre>
<h3 id="heading-8-writing-to-a-read-only-property-is-not-allowed">8. Writing to a read-only property is not allowed.</h3>
<p>In the example below, we define an object, and in the configurable property, we set the writable value to false, which means that a particular object is read-only. So when you try to assign a new value to that object, the strict mode will throw an error.</p>
<pre><code class="lang-plaintext">'use strict';
let obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});
// Will throw an error
obj.x = 25;
</code></pre>
<h3 id="heading-9-writing-to-a-get-only-property-is-not-allowed">9. Writing to a get-only property is not allowed</h3>
<p>In the example below, trying to write to a get-only property throws an error.</p>
<pre><code class="lang-plaintext">'use strict';
let obj = {get x() {return 0} };
// Will throw an error
obj.x = 25;
</code></pre>
<h3 id="heading-10-this-in-strict-mode">10. 'this' in strict mode</h3>
<p>In non-strict mode, when you call a function that isn't bound to any object "this" will refer to the global window object, whereas in strict mode it returns undefined.</p>
<pre><code class="lang-plaintext">'use strict';
const foo = function() {
  console.log(this); // undefined in strict mode
foo();
</code></pre>
<h4 id="heading-conclusion">Conclusion</h4>
<p>The concludes what is strict mode is, how/where to declare it, and the list of few items to keep in mind/expect while using "use strict" in JavaScript.</p>
<h4 id="heading-references">References</h4>
<ul>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict</a></li>
</ul>
<p>Follow me on <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a> | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related tips and posts. Feedbacks and suggestions are welcome.</p>
<p>Checkout my other blog post series</p>
<ul>
<li><p><a target="_blank" href="https://blog.kritikapattalam.com/series/html">HTML</a></p>
</li>
<li><p><a target="_blank" href="https://blog.kritikapattalam.com/series/css">CSS</a></p>
</li>
<li><p><a target="_blank" href="https://blog.kritikapattalam.com/series/javascript">JavaScript</a></p>
</li>
<li><p><a target="_blank" href="https://blog.kritikapattalam.com/series/web-development">Web Development</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Difference between delete and splice an element in an array using JavaScript]]></title><description><![CDATA[As a JavaScript developer, many times in interviews or when building projects we would need to remove some items from an array. And in order to do this, the two things which come to our mind are using delete or splice on the array. 
But do you really...]]></description><link>https://blog.kritikapattalam.com/difference-between-delete-and-splice-an-element-in-an-array-using-javascript</link><guid isPermaLink="true">https://blog.kritikapattalam.com/difference-between-delete-and-splice-an-element-in-an-array-using-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[2Articles1Week]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Thu, 12 Aug 2021 03:12:22 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1628733375665/QgxvQsEKn.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As a JavaScript developer, many times in interviews or when building projects we would need to remove some items from an array. And in order to do this, the two things which come to our mind are using delete or splice on the array. </p>
<p>But do you really know what is the difference between the both?. 
Well, you have come to the right place. Keep reading!!!</p>
<p>As part of this blog, we are going to see</p>
<ol>
<li>What is delete operator?</li>
<li>What is splice?</li>
<li>What is the difference between splice and delete?</li>
<li>What is the best way to delete an element?.</li>
</ol>
<h3 id="define-delete-operator">Define delete operator</h3>
<p>According to MDN docs, </p>
<blockquote>
<p>The JavaScript delete operator removes a property from an object.</p>
</blockquote>
<p><strong>Syntax</strong> : delete expression</p>
<p>where expression should evaluate to </p>
<pre><code><span class="hljs-keyword">delete</span> <span class="hljs-keyword">object</span>.property
<span class="hljs-keyword">delete</span> <span class="hljs-keyword">object</span>[<span class="hljs-string">'property'</span>]
</code></pre><p>Lets see an example</p>
<pre><code><span class="hljs-string">let</span> <span class="hljs-string">items_delete</span> <span class="hljs-string">=</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]<span class="hljs-string">;</span>
<span class="hljs-string">items_delete.length;</span> <span class="hljs-string">//Returns</span> <span class="hljs-number">5</span>
<span class="hljs-string">delete</span> <span class="hljs-string">items_delete[3];</span>
<span class="hljs-string">items_delete.length;</span> <span class="hljs-string">//Returns</span> <span class="hljs-number">5</span>
<span class="hljs-string">console.log(items_delete);</span> <span class="hljs-string">//</span> <span class="hljs-string">Returns</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-string">empty</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
</code></pre><p>In the above example,</p>
<ul>
<li>lets declare and initialize an array with 5 items, whose length is returned as 5.</li>
<li>In the next step using delete operator delete the third item in the array. </li>
<li>When you check the length of the array after deletion it will still return as 5. </li>
<li>As a final step when you try to print the array again you will see that in the position of third key, the value will be seen as empty.</li>
</ul>
<h3 id="define-splice-operator">Define splice operator</h3>
<p>According to MDN docs</p>
<blockquote>
<p>The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.</p>
</blockquote>
<pre><code><span class="hljs-string">let</span> <span class="hljs-string">items_splice</span> <span class="hljs-string">=</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]<span class="hljs-string">;</span>
<span class="hljs-string">items_splice.length;</span> <span class="hljs-string">//Returns</span> <span class="hljs-number">5</span>
<span class="hljs-string">items_splice.splice(3,1);</span>
<span class="hljs-string">items_splice.length;</span> <span class="hljs-string">//Returns</span> <span class="hljs-number">4</span>
<span class="hljs-string">console.log(items_delete);</span> <span class="hljs-string">//</span> <span class="hljs-string">Returns</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
</code></pre><p>In the above example,</p>
<ul>
<li>let's declare and initialize an array with 5 items, whose length is returned as 5.</li>
<li>In the next step using splice method delete the third item in the array. </li>
<li>When you check the length of the array after deletion it will still return as 4. </li>
<li>As a final step when you try to print the array again you will see that only 4 items are present in the array.</li>
</ul>
<h3 id="difference-between-delete-and-splice">Difference between delete and splice</h3>
<p>So by the example what we see is </p>
<ul>
<li><strong>Delete</strong> - will delete the element and replace it with empty/defined, which means the length of the array always remains the same.</li>
<li>Whereas when you use <strong>splice</strong> to delete an element, it removes the value or the index associated with it, which means the length of the array also changes.</li>
</ul>
<h4 id="what-is-the-preferred-method">What is the preferred method?.</h4>
<p>Based on the example we have seen above, when you want to delete an element in the array it is best to use splice, since delete will result in an empty placeholder.</p>
<h4 id="conclusion">Conclusion</h4>
<p>Now you should know the difference between splice and delete and which one to use during development.</p>
<h4 id="references">References</h4>
<ul>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice">splice</a> </li>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete">delete</a> </li>
</ul>
<h4 id="are-you-more-of-a-twitter-person-then-you-can-read-the-same-thing-in-the-below-thread">Are you more of a Twitter person?. Then you can read the same thing in the below thread</h4>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://twitter.com/KritikaPattalam/status/1412794415219892225">https://twitter.com/KritikaPattalam/status/1412794415219892225</a></div>
<p>Follow me on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related tips and posts. Feedbacks and suggestions are welcome.</p>
<p>Checkout my other blog post series</p>
<ul>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/html">HTML</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/css">CSS</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/javascript">JavaScript</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/web-development">Web Development</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Difference between var and let in JavaScript]]></title><description><![CDATA[As a JavaScript developer, we have been told multiple times that var is similar to let, and as part of ES6, we should all move to use let instead of var. 
But do we really know what is the difference between them both and why should one consider usin...]]></description><link>https://blog.kritikapattalam.com/difference-between-var-and-let-in-javascript</link><guid isPermaLink="true">https://blog.kritikapattalam.com/difference-between-var-and-let-in-javascript</guid><category><![CDATA[2Articles1Week]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Wed, 11 Aug 2021 02:33:13 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1628648965839/9A53M9brn.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As a JavaScript developer, we have been told multiple times that var is similar to let, and as part of ES6, we should all move to use <strong>let </strong>instead of <strong>var</strong>. </p>
<p>But do we really know what is the difference between them both and why should one consider using <strong>let</strong>?. Let's find out.</p>
<p>Here are a few of the things which differentiate <strong>var </strong>from <strong>let</strong></p>
<ol>
<li><strong>Global Object</strong></li>
<li><strong>Re-declaration</strong></li>
<li><strong>Hoisting</strong></li>
<li><strong>Scoping</strong></li>
</ol>
<h3 id="1-creating-global-object">1. Creating global object</h3>
<p>When using var at the top of the file, it creates a **global property object which is windows, which does not apply when using let instead.</p>
<pre><code><span class="hljs-keyword">var</span> counter = <span class="hljs-number">0</span>;
<span class="hljs-keyword">let</span> counter2 = <span class="hljs-number">0</span>;
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">window</span>.counter); <span class="hljs-comment">//  0</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">window</span>.counter2); <span class="hljs-comment">//  undefined</span>
</code></pre><p>In the above example, both counter and counter2 are global objects created at the top, but when trying to access as via <strong>window.variableName</strong> <em>let </em>results in undefined error.</p>
<h3 id="2-re-declaration">2. Re-declaration</h3>
<p>In strict mode, let cannot be redeclared and if done it will throw an error, whereas var can be redeclared.</p>
<pre><code><span class="hljs-meta">'use strict'</span>;
<span class="hljs-keyword">var</span> string1= <span class="hljs-string">"hello"</span>;
<span class="hljs-keyword">var</span> string1= <span class="hljs-string">"hi"</span>; <span class="hljs-comment">// "hello" will be replaced without any errors</span>

<span class="hljs-keyword">let</span> string2= <span class="hljs-string">"world"</span>; 
<span class="hljs-keyword">let</span> string2= <span class="hljs-string">"hello"</span>; <span class="hljs-comment">// SyntaxError: Identifier 'string2' has already been declared</span>
</code></pre><h3 id="3-hoisting">3. Hoisting</h3>
<p>Hoisting is when variables can be used even before they are declared. In the below example the variable string1is used, here it saves space for foo with the value undefined and the declaration/initialization follows later in the code. But whereas with <strong>let</strong>, this is not possible it will hit a temporal dead zone resulting in a reference error</p>
<pre><code><span class="hljs-built_in">console</span>.log(string1); <span class="hljs-comment">// undefined</span>
<span class="hljs-keyword">var</span> string1= <span class="hljs-string">"hello"</span>;
<span class="hljs-built_in">console</span>.log(string1); <span class="hljs-comment">// hello</span>

<span class="hljs-built_in">console</span>.log(string2); <span class="hljs-comment">// ReferenceError</span>
<span class="hljs-keyword">let</span> string2= <span class="hljs-string">"world"</span>;
<span class="hljs-built_in">console</span>.log(string2); <span class="hljs-comment">// world</span>
</code></pre><h3 id="4-scoping">4. Scoping</h3>
<p>The main difference between let and var is scoping of the variables. In the below example, Var string3 inside Level-enclosed brackets are available even outside the level when inside a function, whereas let is block-level scoped or enclosing brackets scoped.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">run</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">var</span> string1= <span class="hljs-string">"Hello1"</span>;
  <span class="hljs-keyword">let</span> string2= <span class="hljs-string">"World1"</span>;

  <span class="hljs-built_in">console</span>.log(string1, string2); <span class="hljs-comment">// Hello1 World1</span>
 <span class="hljs-comment">//Level - enclosed brackets</span>
  {
    <span class="hljs-keyword">var</span> string3= <span class="hljs-string">"Hello2"</span>
    <span class="hljs-keyword">let</span> string4= <span class="hljs-string">"World2"</span>;
    <span class="hljs-built_in">console</span>.log(string3, string4); <span class="hljs-comment">// Hello2 World2</span>
  }

  <span class="hljs-built_in">console</span>.log(string3); <span class="hljs-comment">// Hello2</span>
  <span class="hljs-built_in">console</span>.log(string4); <span class="hljs-comment">// ReferenceError</span>
}
</code></pre><p>In the above example, since let is scoped to block or enclosing brackets, when you try accessing string4 outside of the braces block it will not be available and throw an reference error.</p>
<p>Follow me on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related tips and posts. Feedbacks and suggestions are welcome.</p>
<p>Checkout my other blog post series</p>
<ul>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/html">HTML</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/css">CSS</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/javascript">JavaScript</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/series/web-development">Web Development</a> </li>
</ul>
]]></content:encoded></item><item><title><![CDATA[How to add a favicon to a website in a single line of HTML?]]></title><description><![CDATA[Each of us would log in to websites or view multiple sites every day in our life. And most of us are guilty of having more than one tab open in a browser eg: Facebook, Amazon, Google, Gmail, etc, and even with so many tabs open, we are still able to ...]]></description><link>https://blog.kritikapattalam.com/how-to-add-a-favicon-to-a-website-in-a-single-line-of-html</link><guid isPermaLink="true">https://blog.kritikapattalam.com/how-to-add-a-favicon-to-a-website-in-a-single-line-of-html</guid><category><![CDATA[HTML5]]></category><category><![CDATA[webdev]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[2Articles1Week]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Thu, 05 Aug 2021 22:27:27 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1628128721612/lAvNBeLBL.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Each of us would log in to websites or view multiple sites every day in our life. And most of us are guilty of having more than one tab open in a browser eg: Facebook, Amazon, Google, Gmail, etc, and even with so many tabs open, we are still able to click on the correct tab by looking at that small icon on the browser tab which gives us an indication as to which site it is.</p>
<h4 id="heading-have-you-ever-wondered-what-that-small-icon-next-to-the-browser-title-called-is">Have you ever wondered what that small icon next to the browser title called is?</h4>
<p>Well, that is the <strong>favicon</strong>.</p>
<p>As part of this blog, we are going to see,</p>
<ol>
<li><p>What is a favicon?</p>
</li>
<li><p>How to identify a favicon on a webpage?</p>
</li>
<li><p>How to add a favicon to a website with just a single line of HTML?.</p>
</li>
<li><p>Does favicon help with SEO?</p>
</li>
</ol>
<h3 id="heading-what-is-a-favicon">What is a favicon?</h3>
<blockquote>
<p>A favicon is a small icon associated with a website/page/application which is present next to the webpage title in a browser.</p>
</blockquote>
<p>It helps users visually identify websites more easily within browser tabs, bookmarks, shortcuts, and address bars. <strong>Example</strong> - When more browser tabs are open by the favicon user can quickly identify the tab and click on it to view the webpage or the same goes for when there are more bookmarks present etc. With so many tabs/bookmarks open favicon makes it easier for users to identify the site by the image.</p>
<h3 id="heading-how-to-identify-a-favicon-on-a-webpage">How to identify a favicon on a webpage?</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1628128975086/ynGPvFPzI.png" alt="fav-tweet.png" /></p>
<ul>
<li><p>The highlighted icon in a circle is how it shows on the browser when the tabs are open.</p>
</li>
<li><p>The highlighted icon in the rectangle below is how it shows when the website is bookmarked. You can see a list of favicons for websites bookmarked here eg: google, Facebook, Gmail, etc</p>
</li>
</ul>
<h3 id="heading-how-to-add-a-favicon-to-a-website">How to add a favicon to a website?</h3>
<pre><code class="lang-plaintext">&lt;head&gt;
    &lt;title&gt; My test site &lt;/title&gt;
    &lt;link rel="shortcut icon" type="image/jpg" href="images/favicon.png"/&gt;
    .....
&lt;/head&gt;
</code></pre>
<ul>
<li>add a link tag in the head section of your webpage. [Refer to the above code piece.].</li>
</ul>
<p>link tag should have the rel attribute value as</p>
<ul>
<li><p><strong>icon</strong> if just using it for a favicon next to the browser title/bookmarks/address bars etc.</p>
</li>
<li><p>the same icon will be used when the webpage is saved as a shortcut to your desktop/laptop.</p>
</li>
</ul>
<p>href="images/favicon.png"</p>
<ul>
<li><p>add the path to the favicon as the href value</p>
</li>
<li><p>Square icons are expected to be used as favicons. Most used sizes are - 16x16, 32x32, or 48x48.</p>
</li>
<li><p>Back in the day, the image extension for favicon was <strong>.ico</strong> for old browsers, but in recent times mostly <strong>.png</strong> or <strong>svg</strong> type images are used.</p>
</li>
</ul>
<h3 id="heading-does-favicon-help-with-seo">Does favicon help with SEO?</h3>
<p>The more important question to ask does favicon helps with search engine optimization(SEO). No, it directly does not influence SEO, but it does help in creating brand awareness among the users visiting the site because most favicons are logos of the particular brand. Users are mostly seen to revisit pages that have the favicon present compared to the ones which does not. Because when people see a website without one, they tend to feel that it's not a genuine site.</p>
<p>Well, that is it for this blog, and by now you should know how to add a favicon to a website and its uses.</p>
<p>Follow me on <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a> | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related tips and posts. Feedbacks and suggestions are welcome.</p>
<h4 id="heading-are-you-more-of-a-twitter-person-then-check-out-the-short-version-of-this-blog-in-the-below-thread">Are you more of a Twitter person? Then check out the short version of this blog in the below thread.</h4>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://twitter.com/KritikaPattalam/status/1423059971315535879">https://twitter.com/KritikaPattalam/status/1423059971315535879</a></div>
]]></content:encoded></item><item><title><![CDATA[Drop CAP effect / Changing the style of the first letter in a paragraph using CSS.]]></title><description><![CDATA[As part of this blog, we are going to see how we can style the first letter of a paragraph different from the remaining words or letter. 
How to style the first letter in a paragraph

::first letter is a pseudo element which will apply styles just to...]]></description><link>https://blog.kritikapattalam.com/drop-cap-effect-changing-the-style-of-the-first-letter-in-a-paragraph-using-css</link><guid isPermaLink="true">https://blog.kritikapattalam.com/drop-cap-effect-changing-the-style-of-the-first-letter-in-a-paragraph-using-css</guid><category><![CDATA[CSS]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[2Articles1Week]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Sun, 25 Jul 2021 19:48:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1625691597563/rLQ4N-uOA.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As part of this blog, we are going to see how we can style the first letter of a paragraph different from the remaining words or letter. </p>
<h3 id="how-to-style-the-first-letter-in-a-paragraph">How to style the first letter in a paragraph</h3>
<blockquote>
<p>::first letter is a pseudo element which will apply styles just to the first letter of the first line of any block level element.</p>
</blockquote>
<pre><code><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
    <span class="hljs-selector-tag">p</span><span class="hljs-selector-pseudo">::first-letter</span> {
       <span class="hljs-attribute">font-size</span>: <span class="hljs-number">40px</span>;
       <span class="hljs-attribute">color</span>: orange;
    }
</span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
</code></pre><p>In the above piece of code, paragraph tag is a block level element. To the first letter of the paragraph tag which is "L" font-size 40px and color orange would be applied.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1627239856703/oEJb1KS7G.jpeg" alt="FIRST-LETTER.jpeg" /></p>
<p>Now let's see how we can make  a dropcap effect i.e the same first letter big enough such that it as long as three or so lines, and the remaining content just flows around it. This can be done in two ways.</p>
<ol>
<li>Using CSS initial-letter property</li>
<li>Adding some customization to the first-letter by adding float, adjusting the line-height and font-size of the letter.</li>
</ol>
<h3 id="1-css-initial-letter">1. CSS initial-letter</h3>
<ul>
<li>Using css initial-letter property - you can produce a drop cap effect i.e make the letter occupy specified number of lines in a paragraph.</li>
<li>This accepts only a positive integer. Eg: In the below snippet, letter "L" will span/sink four lines.</li>
<li>Note - This is only supported by safari at the moment.</li>
</ul>
<pre><code><span class="hljs-selector-tag">p</span><span class="hljs-selector-pseudo">::first-letter</span> {
   <span class="hljs-attribute">-webkit-initial-letter</span>: <span class="hljs-number">4</span>;
   <span class="hljs-attribute">initial-letter</span>: <span class="hljs-number">4</span>;
   <span class="hljs-attribute">font-size</span>: <span class="hljs-number">40px</span>;
   <span class="hljs-attribute">color</span>: orange;
}
</code></pre><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1627241070183/Tyy2C5-z_.jpeg" alt="initial-letter.jpeg" /></p>
<h3 id="2-customize-the-font-of-the-first-letter">2. Customize the font of the first-letter</h3>
<pre><code><span class="hljs-selector-tag">p</span><span class="hljs-selector-pseudo">::first-letter</span> {
   <span class="hljs-attribute">float</span>: left;
   <span class="hljs-attribute">font-size</span>: <span class="hljs-number">75px</span>;
   <span class="hljs-attribute">line-height</span>: <span class="hljs-number">60px</span>;
   <span class="hljs-attribute">padding</span>: <span class="hljs-number">3px</span>;
   <span class="hljs-attribute">color</span>: orange;
}
</code></pre><p>In the above piece of code, to the first letter of the paragraph increase the font-size, line-height and float it in such a way that it spans to the number of lines you required. And by adding padding you can adjust the spacing of the first-letter as well</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1627241453569/O4yvo_gi-.jpeg" alt="first-letter-customized.jpeg" /></p>
<h4 id="are-you-more-of-a-twitter-person-then-you-can-read-the-same-thing-in-the-below-thread">Are you more of a Twitter person?. Then you can read the same thing in the below thread</h4>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://twitter.com/KritikaPattalam/status/1411416678827704328">https://twitter.com/KritikaPattalam/status/1411416678827704328</a></div>
<h3 id="conclusion">Conclusion</h3>
<p>Now you should know how to </p>
<ol>
<li>Add styles to just the first letter of a paragraph</li>
<li>Add a drop cap effect to the first letter using CSS initial property or by customizing the fonts of the first letter.</li>
</ol>
<p>For browser compatability , check  <a target="_blank" href="https://caniuse.com/">canisuse.com</a> </p>
<h3 id="references">References</h3>
<ul>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/::first-letter">first letter</a></li>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/initial-letter">initial letter</a></li>
</ul>
<p>Follow me on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related tips and posts.</p>
]]></content:encoded></item><item><title><![CDATA[Different ways to remove an element at the end of the array using JavaScript]]></title><description><![CDATA[As part of this blog we are going to see how to remove an element at the end of the array in JavaScript using the following methods.

reduce the length of the array
using Array.prototype.pop() method
using Array.prototype.splice() method.

What is an...]]></description><link>https://blog.kritikapattalam.com/different-ways-to-remove-an-element-at-the-end-of-the-array-using-javascript</link><guid isPermaLink="true">https://blog.kritikapattalam.com/different-ways-to-remove-an-element-at-the-end-of-the-array-using-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[General Programming]]></category><category><![CDATA[data structures]]></category><category><![CDATA[2Articles1Week]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Sun, 18 Jul 2021 19:32:56 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1626620754027/OB3bawbXJ.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As part of this blog we are going to see how to remove an element at the end of the array in JavaScript using the following methods.</p>
<ol>
<li>reduce the length of the array</li>
<li>using Array.prototype.pop() method</li>
<li>using Array.prototype.splice() method.</li>
</ol>
<h3 id="what-is-an-array">What is an Array</h3>
<blockquote>
<p>Array is an ordered set of list of values, where each value is specified by an index. And  a single array can hold different types of values. eg, a single array can hold both strings and integers.</p>
</blockquote>
<p>Lets consider the below set of array, with length 6.</p>
<pre><code><span class="hljs-string">let</span> <span class="hljs-string">arrayExample</span> <span class="hljs-string">=</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span> ,<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]
</code></pre><p>Lets reduce see how we can remove the last element in the array</p>
<h3 id="1-remove-by-reducing-the-length-of-the-array">1) Remove by reducing the length of the array.</h3>
<p>By explicitly setting the length of the array to be less, will delete the last value automatically</p>
<pre><code><span class="hljs-string">let</span> <span class="hljs-string">arrayExample</span> <span class="hljs-string">=</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span> ,<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]<span class="hljs-string">;</span>
<span class="hljs-string">arrayExample.length</span> <span class="hljs-string">=</span> <span class="hljs-number">5</span><span class="hljs-string">;</span> <span class="hljs-string">//</span> <span class="hljs-string">Changing</span> <span class="hljs-string">the</span> <span class="hljs-string">length</span> <span class="hljs-string">of</span> <span class="hljs-string">the</span> <span class="hljs-string">array</span> 
<span class="hljs-string">console.log(arrayExample);</span> <span class="hljs-string">//Output</span> <span class="hljs-string">:</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]<span class="hljs-string">;</span>
</code></pre><h3 id="2-remove-using-arrayprototypepop-method">2) Remove using Array.prototype.pop() method.</h3>
<blockquote>
<p>As per MDN docs, "The pop() method removes the last element from an array and returns that element. This method changes the length of the array".</p>
</blockquote>
<pre><code><span class="hljs-string">let</span> <span class="hljs-string">arrayExample</span> <span class="hljs-string">=</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span> ,<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]<span class="hljs-string">;</span>
<span class="hljs-string">arrayExample.pop();</span> <span class="hljs-string">//</span> <span class="hljs-string">removes</span> <span class="hljs-string">the</span> <span class="hljs-string">element</span> <span class="hljs-number">6</span>
<span class="hljs-string">console.log(arrayExample);</span> <span class="hljs-string">//Output</span> <span class="hljs-string">:</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]<span class="hljs-string">;</span>
</code></pre><p>In the above example , the last element in the array is "6". As per pop() method the last element in the array is removed.</p>
<h3 id="3-remove-using-arrayprototypesplice-method">3) Remove using Array.prototype.splice() method.</h3>
<blockquote>
<p>As per MDN docs, " The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place."</p>
</blockquote>
<pre><code><span class="hljs-string">let</span> <span class="hljs-string">arrayExample</span> <span class="hljs-string">=</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span> ,<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]<span class="hljs-string">;</span>
<span class="hljs-string">arrayExample.splice(arrayExample.length</span> <span class="hljs-bullet">-</span> <span class="hljs-number">1</span><span class="hljs-string">);</span> <span class="hljs-string">//</span> <span class="hljs-string">returns</span> <span class="hljs-string">elements</span> <span class="hljs-string">up</span> <span class="hljs-string">until</span> <span class="hljs-number">5</span>
<span class="hljs-string">console.log(arrayExample);</span> <span class="hljs-string">//Output</span> <span class="hljs-string">:</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]<span class="hljs-string">;</span>
</code></pre><p>In the above example, we specify the index value as array length -1 which is 5. Hence it returns the array up until 5 array elements.</p>
<h3 id="references">References</h3>
<ul>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop">Array.prototype.pop()</a> </li>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice">Array.prototype.splice()</a> </li>
</ul>
<h4 id="check-out-my-other-blog-posts">Check out my other blog posts</h4>
<ul>
<li><a target="_blank" href="https://blog.kritikapattalam.com/10-things-you-can-do-in-chrome-developer-tools">10 things you can do in chrome developer tools</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/2-simple-ways-you-can-truncate-text-using-css"> 2 Simple ways you can truncate text using CSS</a>  </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/create-a-github-profile-readme-with-widgets">Create a GitHub Profile README.md with widgets</a></li>
</ul>
<p>Lets connect on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related tips.</p>
]]></content:encoded></item><item><title><![CDATA[Disable Zoom on pinch in mobile using HTML tag.]]></title><description><![CDATA[As a user when you open a site in mobile, we tend to pinch and zoom the page to see things more clearly. Have you ever wondered if that feature can be disabled?.
Yes, it can be disabled. What's more interesting is that it can be done by just a single...]]></description><link>https://blog.kritikapattalam.com/disable-zoom-on-pinch-in-mobile-using-html-tag</link><guid isPermaLink="true">https://blog.kritikapattalam.com/disable-zoom-on-pinch-in-mobile-using-html-tag</guid><category><![CDATA[Web Development]]></category><category><![CDATA[HTML5]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[Accessibility]]></category><category><![CDATA[2Articles1Week]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Sat, 10 Jul 2021 03:19:27 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1625691735172/mcd463bLG.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As a user when you open a site in mobile, we tend to pinch and zoom the page to see things more clearly. Have you ever wondered if that feature can be disabled?.</p>
<p>Yes, it can be disabled. What's more interesting is that it can be done by just a single line of code in HTML.</p>
<h3 id="how-to-disable-zoom-on-pinch">How to disable zoom on pinch</h3>
<ul>
<li>On the head section of the HTML, include the following piece of line which is a meta tag that tells the browser the details of what should happen on a device's viewport</li>
</ul>
<pre><code><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">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 ,
    maximum-scale=1.0 , user-scalable=no"</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">body</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>name="viewport"</p>
</blockquote>
<p>this means that this meta tag is used for device viewport</p>
<blockquote>
<p>width=device-width</p>
</blockquote>
<p>assigns the width of the device as the viewport width</p>
<blockquote>
<p>initial-scale and maximum-scale is set to 1 -</p>
</blockquote>
<p> which means it occupies the full 100% of the viewport on page load</p>
<blockquote>
<p>user-scalable=no, </p>
</blockquote>
<p>setting the value to no or 0 is what tells the browser to prevent the page from scaling(zoom) in/out</p>
<h3 id="things-to-keep-in-mind-while-using-this">Things to keep in mind while using this</h3>
<ul>
<li>This can affect accessibility, people with poor vision will have concerns when visiting the page, since it will prevent them for zooming/out and viewing content closely when required. So use wisely.</li>
<li>iOS or safari still lets the users zoom for the above mentioned accessibility reason, so this fix might not work in those platform.</li>
</ul>
<h4 id="check-out-my-other-blog-posts">Check out my other blog posts</h4>
<ul>
<li><a target="_blank" href="https://blog.kritikapattalam.com/10-things-you-can-do-in-chrome-developer-tools">10 things you can do in chrome developer tools</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/2-simple-ways-you-can-truncate-text-using-css"> 2 Simple ways you can truncate text using CSS</a>  </li>
</ul>
<p>Lets connect on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related chats.</p>
]]></content:encoded></item><item><title><![CDATA[How to add "SKIP TO MAIN CONTENT" to websites]]></title><description><![CDATA[As part of this blog post let's see what is web accessibility, and how to add "skip to main content" link for web pages and why?.
Accessibility
As per w3.org

Web accessibility means that websites, tools, and technologies are designed and developed s...]]></description><link>https://blog.kritikapattalam.com/how-to-add-skip-to-main-content-to-websites</link><guid isPermaLink="true">https://blog.kritikapattalam.com/how-to-add-skip-to-main-content-to-websites</guid><category><![CDATA[Web Development]]></category><category><![CDATA[Accessibility]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[2Articles1Week]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Sat, 03 Jul 2021 15:31:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1625318916245/CAyJyYcCl.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As part of this blog post let's see what is web accessibility, and how to add "skip to main content" link for web pages and why?.</p>
<h3 id="accessibility">Accessibility</h3>
<p>As per w3.org</p>
<blockquote>
<p>Web accessibility means that websites, tools, and technologies are designed and developed so that people with disabilities can use them. More specifically, people can: perceive, understand, navigate, and interact with the Web
contribute to the Web</p>
</blockquote>
<p>For a normal user when he visits a page, he/she directly jumps to the section of the page where they want to read. But if we consider visually impaired users or someone who uses screen reader, the assistive device has to read through the entire page before they can reach a point which interests them. </p>
<p>Let's assume when a user lands on the page , the screen reader will read the entire navigation/header and then goes to the actual content of the page. The user reads through the first page of your website once, now he/she goes to the second page and the screen reader will again start reading the entire navigation for them before they can go to the main content. This is where "SKIP TO MAIN CONTENT" link comes in handy for them.</p>
<h3 id="how-to-add-skip-to-main-content-on-the-page">How to add "SKIP TO MAIN CONTENT" on the page</h3>
<h4 id="html">HTML</h4>
<ul>
<li>Inside the body tag add an anchor tag as the first element. </li>
<li>Add an id to the content you want the screenreader to jump/skip to. eg: 'main-content'</li>
<li>Add the same id as the href value to the anchor tag eg: 'main-content'</li>
</ul>
<pre><code><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"skip-link"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#main-content"</span>&gt;</span>SKIP TO MAIN CONTENT<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span> .....<span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"main-content"</span>&gt;</span>
         Main Body Content .....
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">footer</span>&gt;</span>......<span class="hljs-tag">&lt;/<span class="hljs-name">footer</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
</code></pre><h4 id="css">CSS</h4>
<ul>
<li>Add CSS to the link to hide it from view.
<strong>[P.S Do not use display:none or visibility hidden because screen readers will not read the element. Our main focus is for the screen reader to read the link]</strong></li>
<li>When the screenreader focuses on that element, make the element visible and also the screen reader will read the content inside the anchor tag . i.e "SKIP TO MAIN CONTENT" in our example.</li>
</ul>
<pre><code><span class="hljs-selector-class">.skip-link</span> {
    <span class="hljs-attribute">position</span>: absolute;
    <span class="hljs-attribute">left</span>: -<span class="hljs-number">999px</span>;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">1px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">1px</span>;
    <span class="hljs-attribute">top</span>: auto;
}

<span class="hljs-selector-class">.skip-link</span><span class="hljs-selector-pseudo">:active</span>, <span class="hljs-selector-class">.skip-link</span><span class="hljs-selector-pseudo">:focus</span> {
    <span class="hljs-attribute">color</span>: <span class="hljs-number">#000</span>;
    <span class="hljs-attribute">display</span>: inline-block;
    <span class="hljs-attribute">height</span>: auto;
    <span class="hljs-attribute">width</span>: auto;
    <span class="hljs-attribute">position</span>: static;
    <span class="hljs-attribute">margin</span>: auto
}
</code></pre><h4 id="references">References</h4>
<ul>
<li><a target="_blank" href="https://www.w3.org/WAI/fundamentals/accessibility-intro/"> Accessibility</a> </li>
</ul>
<h3 id="checkout-my-other-blog-posts">Checkout my other blog posts</h3>
<ul>
<li><a target="_blank" href="https://blog.kritikapattalam.com/getting-started-with-javascript-ultimate-free-resources">Getting Started with JavaScript - Ultimate Free Resources</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/10-things-you-can-do-in-chrome-developer-tools">10 Things you can do in Chrome Developer Tools </a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/2-simple-ways-you-can-truncate-text-using-css">2 Simple ways you can truncate text using CSS</a> </li>
</ul>
<p>Please share your feedbacks and suggestions in the comments below.</p>
<p>Lets connect on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related chats.</p>
]]></content:encoded></item><item><title><![CDATA[2 Uses of concat() method when working with arrays in JavaScript]]></title><description><![CDATA[Let's see two ways in which concat() method can be used while working with arrays in JavaScript. 
1) Merging two or more arrays
Let's now see how we can use the concat method to merge two given arrays, array 1 and array 2
const array1 = [1,2,3,4];
co...]]></description><link>https://blog.kritikapattalam.com/2-uses-of-concat-method-when-working-with-arrays-in-javascript</link><guid isPermaLink="true">https://blog.kritikapattalam.com/2-uses-of-concat-method-when-working-with-arrays-in-javascript</guid><category><![CDATA[Web Development]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[ES6]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Tue, 29 Jun 2021 03:22:20 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1630599396205/_v2VDa14B.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Let's see two ways in which concat() method can be used while working with arrays in JavaScript. </p>
<h3 id="1-merging-two-or-more-arrays">1) Merging two or more arrays</h3>
<p>Let's now see how we can use the concat method to merge two given arrays, array 1 and array 2</p>
<pre><code><span class="hljs-string">const</span> <span class="hljs-string">array1</span> <span class="hljs-string">=</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]<span class="hljs-string">;</span>
<span class="hljs-string">const</span> <span class="hljs-string">array2</span> <span class="hljs-string">=</span> [<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]<span class="hljs-string">;</span>
<span class="hljs-string">//</span> <span class="hljs-string">merge</span> <span class="hljs-string">array</span> <span class="hljs-number">1</span> <span class="hljs-string">into</span> <span class="hljs-string">array</span> <span class="hljs-number">2</span> <span class="hljs-string">and</span> <span class="hljs-string">store</span> <span class="hljs-string">it</span> <span class="hljs-string">in</span> <span class="hljs-string">a</span> <span class="hljs-string">new</span> <span class="hljs-string">variable</span> 
<span class="hljs-string">const</span> <span class="hljs-string">newMergedArray</span> <span class="hljs-string">=</span> []<span class="hljs-string">.concat(array1,</span> <span class="hljs-string">array2);</span>
<span class="hljs-string">console.log(newMergedArray);</span> <span class="hljs-string">//</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</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-string">console.log(array1);</span> <span class="hljs-string">//</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]
<span class="hljs-string">console.log(array2);</span> <span class="hljs-string">//</span> [<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]
</code></pre><blockquote>
<p>const newMergedArray = [].concat(array1, array2);</p>
</blockquote>
<p>In the above line [] creates a new array and the concat methods combine both array1, array2. This can also accept more than 2 arrays for concatenation by just adding them one next to the other</p>
<blockquote>
<p>const newMergedArray = [].concat(array1, array2, array 3, arrayN);</p>
</blockquote>
<p>Another alternative for merging two arrays is below</p>
<blockquote>
<p>const newMergedArray = array1.concat(array2, array 3, arrayN);</p>
</blockquote>
<h3 id="2-flatten-single-level-nested-arrays">2) Flatten single level nested arrays</h3>
<p>Let's assume there is a nested array as below, and we would want to flatten it in such a way that the output is [1,2,3,4,5,6,7,8], this can be achieved by using concat() method with the help of spread operator.</p>
<pre><code><span class="hljs-string">const</span> <span class="hljs-string">array1</span> <span class="hljs-string">=</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,[<span class="hljs-number">3</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-string">;</span>
<span class="hljs-string">const</span> <span class="hljs-string">flattened</span> <span class="hljs-string">=</span> []<span class="hljs-string">.concat(...array1);</span>
<span class="hljs-string">console.log(flattened);</span> <span class="hljs-string">//</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</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>]
</code></pre><p>P.S. The above code flattens just a single-level nested array.</p>
<h3 id="references">References</h3>
<ul>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat">concat()</a>  </li>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax">spread operator</a> </li>
</ul>
<h3 id="check-out-my-other-blog-posts">Check out my other blog posts</h3>
<ul>
<li><a target="_blank" href="https://blog.kritikapattalam.com/getting-started-with-javascript-ultimate-free-resources">Getting Started with JavaScript - Ultimate Free Resources</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/10-things-you-can-do-in-chrome-developer-tools">10 Things you can do in Chrome Developer Tools </a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/2-simple-ways-you-can-truncate-text-using-css">2 Simple ways you can truncate text using CSS</a> </li>
</ul>
<p>Lets connect on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related chats.</p>
]]></content:encoded></item><item><title><![CDATA[How to display content in multiple columns using CSS]]></title><description><![CDATA[In this article, we are going to see how or when  orphans or widows property in CSS can be used in showing content as multiple columns. 
[P.S : Refer the codepen given below to see visually how each property behaves]
Consider that you have a list of ...]]></description><link>https://blog.kritikapattalam.com/how-to-display-content-in-multiple-columns-using-css</link><guid isPermaLink="true">https://blog.kritikapattalam.com/how-to-display-content-in-multiple-columns-using-css</guid><category><![CDATA[Web Development]]></category><category><![CDATA[CSS]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[HTML5]]></category><category><![CDATA[General Programming]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Sun, 27 Jun 2021 02:04:36 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1624748893770/ShKAsGldd.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, we are going to see how or when  orphans or widows property in CSS can be used in showing content as multiple columns. </p>
<p><strong>[P.S : Refer the codepen given below to see visually how each property behaves]</strong></p>
<p>Consider that you have a list of paragraphs and you want to show it in 3 or 4 columns and also decide where or how the content can break. Let's see the first step on how you can split the content in a simple way.</p>
<h3 id="simple-steps-on-how-to-show-content-into-multiple-columns">Simple steps on how to show content into multiple columns.</h3>
<ul>
<li>Create a div with multiple paragraph tags or a ul with multiple li tags</li>
<li>Add the below code snippet</li>
</ul>
<blockquote>
<p>columns: 3 //  should be a positive integer. Here the paragraph will be split into 3 columns.</p>
</blockquote>
<pre><code><span class="hljs-selector-class">.split-3-columns</span> {
  <span class="hljs-attribute">display</span>: block;
  <span class="hljs-attribute">columns</span>: <span class="hljs-number">3</span>; <span class="hljs-comment">// value based on how many columns you want the content to split</span>
  <span class="hljs-attribute">height</span>: <span class="hljs-number">250px</span>; <span class="hljs-comment">// height of the div as per your need</span>
}
</code></pre><p>In the above example, columns property accepts a positive integer value.Eg: value 3 means, the paragraphs will be split into 3 columns in the browser. 
When the content is split into multiple columns at times the layout or the paragraph might not look as expected.</p>
<p>Eg: You might end up seeing something below, where in the second column, the column begins with the last line of the paragraph and seeing it in just two line looks weird. In this case you might wish that when the content does break you might want to show the last three lines of a paragraph at the start of the column instead of just two lines. This is where widows come into the picture. The same goes for the paragraph for <strong>"It was popularised..."</strong> the first lines of the paragraph at the bottom of the column is in 2 lines, but you want to change how many lines of a paragraph are shown at the bottom of the column, this can be done using orphans.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1624757961409/CIZ6tqJ83_.png" alt="Example" /></p>
<h3 id="orphans">Orphans</h3>
<blockquote>
<p>The orphans property sets the minimum number of lines that can stay by themselves at the bottom of a fragment(columns) before a fragmentation(column) break. The value must be positive and applies to block level container elements. This value typically affects the first line of the paragraph</p>
</blockquote>
<ul>
<li>In the below codepen example , orphan value has been set to 1. Hence if you check the paragraph <strong>"It was popularised in the 1960s"</strong>, it breaks as a single line i.e if the paragraph at the bottom of the column is expected to break at the first few lines then it will make sure that atleast 1 line of paragraph is seen at the bottom of the column(fragment), if there is no enough space to break based on the orphan value then it will rather start in the next column.</li>
</ul>
<pre><code><span class="hljs-selector-class">.split-3-columns</span> {
  <span class="hljs-attribute">display</span>: block;
  <span class="hljs-attribute">orphans</span>: <span class="hljs-number">1</span>; 
  <span class="hljs-attribute">columns</span>: <span class="hljs-number">3</span>; <span class="hljs-comment">// value based on how many columns you want the content to split</span>
  <span class="hljs-attribute">height</span>: <span class="hljs-number">250px</span>; <span class="hljs-comment">// height of the div as per your need</span>
}
</code></pre><h3 id="widows">Widows</h3>
<blockquote>
<p>The widow property sets the minimum number of lines that can stay by themselves at the top of a new fragment(column) after a fragmentation(column) break. The value must be positive and applies to block level container elements. This value is typically for the last line of a paragraph</p>
</blockquote>
<ul>
<li>In the below codepen example , widows value has been set to 5. Hence if you check the paragraph <strong>"Lorem Ipsum is simply dummy "</strong> you would see atleast there are 5 lines of text from the end of the parapgraph is shown at the top of the second new column/fragment.</li>
</ul>
<pre><code><span class="hljs-selector-class">.split-3-columns</span> {
  <span class="hljs-attribute">display</span>: block;
  <span class="hljs-attribute">widows</span>: <span class="hljs-number">5</span>; 
  <span class="hljs-attribute">columns</span>: <span class="hljs-number">3</span>; <span class="hljs-comment">// value based on how many columns you want the content to split</span>
  <span class="hljs-attribute">height</span>: <span class="hljs-number">250px</span>; <span class="hljs-comment">// height of the div as per your need</span>
}
</code></pre><div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://codepen.io/kpattalam/pen/mdmbBVP">https://codepen.io/kpattalam/pen/mdmbBVP</a></div>
<h3 id="browser-support">Browser support:</h3>
<p>This is supported in all major browsers except for firefox in desktop and android firefox in mobile.
For more details refer the MDN docs given below in the references section.</p>
<h3 id="references">References</h3>
<ul>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/widows">Widows  - MDN docs</a> </li>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/orphans">Orphans - MDN docs</a> </li>
</ul>
<p>Lets connect on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related chats.</p>
]]></content:encoded></item><item><title><![CDATA[4 Less used HTML tags and their uses]]></title><description><![CDATA[As part of this blog we are going to see 4 less used HTML tags during web development
1) Refresh the browser or redirect the page
Did you know that you can refresh the browser automatically every few seconds with just a single line of code?.
Yes, thi...]]></description><link>https://blog.kritikapattalam.com/4-less-used-html-tags-and-their-uses</link><guid isPermaLink="true">https://blog.kritikapattalam.com/4-less-used-html-tags-and-their-uses</guid><category><![CDATA[Web Development]]></category><category><![CDATA[HTML5]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Mon, 21 Jun 2021 23:21:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1624315750483/j_fL0H0aQ.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As part of this blog we are going to see 4 less used HTML tags during web development</p>
<h3 id="1-refresh-the-browser-or-redirect-the-page">1) Refresh the browser or redirect the page</h3>
<p>Did you know that you can refresh the browser automatically every few seconds with just a single line of code?.</p>
<p>Yes, this can be done using the  http-equiv="refresh" attribute on the meta tag.</p>
<pre><code><span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"refresh"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"30"</span>&gt;</span>
</code></pre><ul>
<li>content value for 30 here means , the page will refresh every 30 seconds. Enter the value you want and see the page refresh every few seconds. </li>
<li>if you add a url value followed by the integer, the page will redirect to the given url after the specified time.
eg: </li>
</ul>
<pre><code><span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"refresh"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"3;url=https://hashnode.com"</span>&gt;</span>
</code></pre><p><strong>Note: </strong>Refresh might have accessibility concerns so use with caution.*</p>
<h3 id="2-calculating-with-output-tag">2) Calculating with output tag</h3>
<p>Output tag is used to represent the result of a calculation. In the below example we can calculate sum of two input fields and display it in the output field directly.</p>
<ul>
<li><blockquote>
<p>for attribute eg: for="a b"
is used to tell the output tag, which fields are used for the manipulation. In the below code pen for attribute value is the id of the two input fields.</p>
</blockquote>
</li>
<li><blockquote>
<p>using the name attribute value, the results from the oninput javascript is populated into the output tag. eg: output tag name value is result hence in javascript you can pass the result after calculation using result.value</p>
</blockquote>
</li>
</ul>
<pre><code>&lt;form oninput="result.value=parseInt(a.value)+parseInt(b.value)"&gt;
    &lt;<span class="hljs-keyword">input</span> <span class="hljs-keyword">type</span>="range" id="a" <span class="hljs-keyword">value</span>="50"&gt; +
    &lt;<span class="hljs-keyword">input</span> <span class="hljs-keyword">type</span>="number" id="b" <span class="hljs-keyword">value</span>="100"&gt; =
    &lt;output <span class="hljs-type">name</span>="result" <span class="hljs-keyword">for</span>="a b"&gt;&lt;/output&gt;
&lt;/form&gt;
</code></pre><div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://codepen.io/kpattalam/pen/QWpRKez">https://codepen.io/kpattalam/pen/QWpRKez</a></div>
<h3 id="3-base-tag">3) base tag</h3>
<p>This tag is very useful when your entire webpage points to the same base URL</p>
<blockquote>
<p>The base HTML element specifies the base URL to use for all relative URLs in a document. There can be only one  element in a document.</p>
</blockquote>
<pre><code><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">base</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://kritika-pattalam.hashnode.dev/"</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</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">body</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"2-simple-ways-you-can-truncate-text-using-css"</span>&gt;</span>Click on this url<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
</code></pre><h4 id="how-to-use-it">How to use it ?.</h4>
<ul>
<li>In the below codepen, I have specified the base href attribute to my hashnode main blog page. eg: https://kritika-pattalam.hashnode.dev/</li>
<li>In the anchor tag instead of specifying the absolute URL, I have used the relative URL of my blog post eg : 2-simple-ways-you-can-truncate-text-using-css 
And the entire blog URL is : https://kritika-pattalam.hashnode.dev/2-simple-ways-you-can-truncate-text-using-css </li>
</ul>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://codepen.io/kpattalam/pen/dyvEOVZ">https://codepen.io/kpattalam/pen/dyvEOVZ</a></div>
<h3 id="4-template-tag-in-html-the-content-template-element">4) Template tag in HTML - The Content Template element</h3>
<ul>
<li>The HTML <strong>template</strong> tag permits you to declare pieces of HTML sections that can be cloned and embedded into the DOM using script. </li>
<li>The contents of the template tag are not added to the DOM on page load, they are only inserted based on some user interaction. Eg: Lets say there is an image inside the template tag, the image does not get downloaded until the template is cloned and inserted into the DOM structure.</li>
</ul>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://codepen.io/kpattalam/pen/vYxwgGb">https://codepen.io/kpattalam/pen/vYxwgGb</a></div>
<p>Leave me a comment below if you know about other rarely used HTML tags.</p>
<h3 id="references-mdn-docs">References - mdn docs</h3>
<ul>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base">Base Tag</a> </li>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta">http-equiv="refresh"</a></li>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/output">output tag</a></li>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template">template tag</a></li>
</ul>
<p>Lets connect on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related chats.</p>
]]></content:encoded></item><item><title><![CDATA[2 Simple ways you can truncate text using CSS]]></title><description><![CDATA[As part of this blog lets see two ways in which you can truncate a text using CSS
1) Truncate a single line text using ellipsis
.truncate-ellipsis {
  width: 350px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: bloc...]]></description><link>https://blog.kritikapattalam.com/2-simple-ways-you-can-truncate-text-using-css</link><guid isPermaLink="true">https://blog.kritikapattalam.com/2-simple-ways-you-can-truncate-text-using-css</guid><category><![CDATA[CSS]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[HTML5]]></category><category><![CDATA[General Programming]]></category><category><![CDATA[Frontend Development]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Tue, 15 Jun 2021 21:25:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1642629110896/55YxcrL3O.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As part of this blog lets see two ways in which you can truncate a text using CSS</p>
<h3 id="heading-1-truncate-a-single-line-text-using-ellipsis">1) Truncate a single line text using ellipsis</h3>
<pre><code><span class="hljs-selector-class">.truncate-ellipsis</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">350px</span>;
  <span class="hljs-attribute">white-space</span>: nowrap;
  <span class="hljs-attribute">overflow</span>: hidden;
  <span class="hljs-attribute">text-overflow</span>: ellipsis;
  <span class="hljs-attribute">display</span>: block;
}
</code></pre><p>With text-overflow , ellipsis can be applied to single line of text, provided the following conditions are met. [ For truncating after multiple lines, keep reading  😉 ].</p>
<ul>
<li>the element  must have <blockquote>
<p>width , max-width or flex-basis(if using flex)</p>
</blockquote>
</li>
<li>the element must have property <blockquote>
<p>word-wrap: nowrap  </p>
</blockquote>
</li>
<li><em>overflow</em> property should have <em>value other than visible</em> . <blockquote>
<p>eg: overflow: hidden;</p>
</blockquote>
</li>
<li>must have display value as <em>block, inline-block</em> or any other equivalent such as flex item etc.  display:inline will not work here.<blockquote>
<p>eg: display: inline-block;</p>
</blockquote>
</li>
</ul>
<p>Did you know that you can reverse the direction of the truncation using the CSS direction property? </p>
<pre><code>direction: rtl; //<span class="hljs-keyword">show</span> <span class="hljs-keyword">from</span> <span class="hljs-keyword">right</span> <span class="hljs-keyword">to</span> <span class="hljs-keyword">left</span>
</code></pre><p>The direction property will truncate the text in the start of the line and show the end of the paragraph instead. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623787455986/A4m1zQ3hmX.png" alt="Truncate text single line with direction right to left" /></p>
<h3 id="heading-2-truncate-text-after-multiple-lines-using-line-clamp">2) Truncate text after multiple lines using line-clamp</h3>
<pre><code><span class="hljs-selector-class">.truncate-line-clamp</span> {
  <span class="hljs-attribute">display</span>: -webkit-box;
  <span class="hljs-attribute">-webkit-line-clamp</span>: <span class="hljs-number">4</span>;
  <span class="hljs-attribute">-webkit-box-orient</span>: vertical; 
  <span class="hljs-attribute">width</span>: <span class="hljs-number">250px</span>;
  <span class="hljs-attribute">overflow</span>: hidden;
}
</code></pre><p>With line-clamp text can be truncated after multiple lines, whats even more interesting is you can truncate it by specifying the line number where you want to truncate it.
eg: -webkit-line-clamp: 3; will truncate start truncating the text from the third line.</p>
<p>Below are the list of conditions which should be met in order to make this work.</p>
<ul>
<li><em>display</em> property should be <em>webkit-box</em><blockquote>
<p>eg: display: -webkit-box;</p>
</blockquote>
</li>
<li>webkit-line-clamp value should be specified , value should be greater than 0.<blockquote>
<p>eg: webkit-line-clamp: 3;</p>
</blockquote>
</li>
<li>box-orient should be set to vertical <blockquote>
<p>eg: -webkit-box-orient: vertical;</p>
</blockquote>
</li>
<li><em>overflow</em> property should have <em>value hidden</em> . <blockquote>
<p>eg: overflow: hidden;</p>
</blockquote>
</li>
</ul>
<p><strong>Browser Compatability:</strong> webkit-line-clamp at the moment is not supported in IE.
For detailed information refer:  <a target="_blank" href="https://caniuse.com/?search=webkit-line-clamp">caniuse.com</a> </p>
<h3 id="heading-codepen">Codepen:</h3>
<p>The below codepen will show you a live preview of the above two methods will look.</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://codepen.io/kpattalam/pen/jOBXvyg">https://codepen.io/kpattalam/pen/jOBXvyg</a></div>
<h3 id="heading-references-mdn-docs">References - MDN docs</h3>
<ul>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-line-clamp">webkit-line-clamp</a> </li>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/white-space">white-space: nowrap</a></li>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow"> text-overflow</a></li>
</ul>
<p>Lets connect on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related chats.</p>
]]></content:encoded></item><item><title><![CDATA[10 Things you can do in Chrome Developer Tools]]></title><description><![CDATA[Here are 10 things you can do, if you want to edit CSS directly on the styles panel of the chrome dev tools while debugging.
1) Edit the Box Model directly
 When you inspect an element, if you look into the computed section of the styles tab you will...]]></description><link>https://blog.kritikapattalam.com/10-things-you-can-do-in-chrome-developer-tools</link><guid isPermaLink="true">https://blog.kritikapattalam.com/10-things-you-can-do-in-chrome-developer-tools</guid><category><![CDATA[Web Development]]></category><category><![CDATA[devtools]]></category><category><![CDATA[CSS]]></category><category><![CDATA[Google Chrome]]></category><category><![CDATA[General Programming]]></category><dc:creator><![CDATA[Kritika Pattalam Bharathkumar]]></dc:creator><pubDate>Wed, 09 Jun 2021 03:32:24 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1623209031601/aKiJx_jH0.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Here are 10 things you can do, if you want to edit CSS directly on the styles panel of the chrome dev tools while debugging.</p>
<h3 id="1-edit-the-box-model-directly">1) Edit the Box Model directly</h3>
<p> When you inspect an element, if you look into the computed section of the styles tab you will see the box model representation of the element.
Did you know that you can edit the values in the box model directly?. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623199744427/SWI5Z5mfr.gif" alt="box-model.gif" /></p>
<h3 id="2-color-palette-and-contrast-ratio">2) Color Palette &amp; Contrast Ratio</h3>
<p>When you see styles with color, you can click on the swatch which will open the color palette for you.  Open the contrast ratio , green ticks means the color is in the acceptable contrast ratio, red means it does not meet the standard. The colors within the curved white spectrum are the ones which have a better color contrast ratio, if you choose colors outside of it you will notice that the contrast ratio check mark turns to red. Try adjusting the colors accordingly using the color picker to see what works best. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623200985390/e32kv8YuK.gif" alt="color-contrast.gif" /></p>
<h3 id="3-changing-color-value-syntax-between-hex-rgb-and-hsa">3) Changing color value syntax between hex, rgb and hsa</h3>
<p>By default the color value seen in the styles tab is the one written in the css file, but this can be changed to show either hex or RGBA or HSA on the fly. This can be done by pressing Shift + clicking on the color value , it will change the values like seen in the below screen capture.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623201806216/Uc62rttfY.gif" alt="toggle-color-value.gif" /></p>
<h3 id="4-css-shadows">4) CSS shadows</h3>
<p>Most times we use text shadows or box shadows during development. When in doubt or still trying to figure out the best values to use, you can use the developer tools to test out what works for you best.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623202964554/mvjp3YUUz.gif" alt="shadow-effect.gif" /></p>
<h3 id="5-angle-clock">5) Angle Clock</h3>
<p>You would have used transform : rotate degree in css, by clicking on the icon next to the degree you can rotate the degree and see what works best for you.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623203504191/XeENcvuKU.gif" alt="rotate.gif" /></p>
<h3 id="6-grid-and-flex-layout">6) Grid &amp; Flex Layout</h3>
<p> By clicking on the layout section, now you can find all the list of elements on the page which use display: flex or grid property. When you check the checkbox the element gets highlighted with dotted lines</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623204639490/Y6JENLLOe.gif" alt="grid-flex-layout.gif" /></p>
<h3 id="7-flexbox-debugging-tool">7) Flexbox debugging tool</h3>
<p>Flexbox has variety of options, and if you would like to test them out , the new feature in dev tool has an icon next to the display:flex property using which you can try out all the options and see how the content/layout behaves.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623207402188/_pKsWc8F0.png" alt="Flexbox debugging tool" /></p>
<h3 id="8-badge-settings">8) Badge settings</h3>
<p>In the elements tab you can see the list of elements which use flex, grid, scroll-snap or ads. Right click on the elements(eg: div, p , span etc) and select badge settings. You can choose the badges you think will be most required for your debugging.
In the below screenshot, you can see that the moment i check scroll-snap , the badges showed up next to the div which had scroll snap enabled.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623206383468/CS-Aq_hkU.gif" alt="badge-settings.gif" /></p>
<h3 id="9-visualize-css-scroll-snap">9) Visualize CSS scroll snap</h3>
<p>CSS scroll snap lets you set the scroll offset position of a scroll container to be at a preferred snap position once the scroll operation is finished. Visualizing this can be difficult, and using the developer tool, you can see how/where your scroll position ends.</p>
<p>In the elements tab, you will find the list of elements which currently use scroll-snap. Click on it, and it will highlight the element to show you its position.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623205765379/8jLzQ0YgA.gif" alt="scroll-snap.gif" /></p>
<h3 id="10-grouping-css-properties-in-the-computed-pane">10) Grouping CSS properties in the Computed pane</h3>
<p>On the computed section of the styles pane usually we see all the styles which are applied on a particular element. By clicking on group they are grouped together by category and you can focus just on the ones which are required for your debugging.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623207325849/2Taaffme8.png" alt="ezgif.com-gif-maker (2).png" /></p>
<p>Let me know in the comments below your favorite among these and if there are others which you use. </p>
<p>Check out my other blog posts:</p>
<ul>
<li><a target="_blank" href="https://blog.kritikapattalam.com/create-a-github-profile-readme-with-widgets">Create a GitHub Profile README.md with widgets</a> </li>
<li><a target="_blank" href="https://blog.kritikapattalam.com/getting-started-with-javascript-ultimate-free-resources">Getting Started with JavaScript - Ultimate Free Resources</a> </li>
</ul>
<p>Lets connect on  <a target="_blank" href="https://twitter.com/KritikaPattalam">Twitter</a>  | <a target="_blank" href="https://www.linkedin.com/in/kritika-p-296739155/">LinkedIn</a> for more web development related chats.</p>
]]></content:encoded></item></channel></rss>