<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Raghav Chandra VS on Medium]]></title>
        <description><![CDATA[Stories by Raghav Chandra VS on Medium]]></description>
        <link>https://medium.com/@raghavchandravs?source=rss-9796a68c67b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*t-Q27ZXg1nWrhPPP</url>
            <title>Stories by Raghav Chandra VS on Medium</title>
            <link>https://medium.com/@raghavchandravs?source=rss-9796a68c67b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 11:12:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@raghavchandravs/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The JavaScript and JavaScript DOM]]></title>
            <link>https://medium.com/@raghavchandravs/the-javascript-and-javascript-dom-578a198770b8?source=rss-9796a68c67b------2</link>
            <guid isPermaLink="false">https://medium.com/p/578a198770b8</guid>
            <dc:creator><![CDATA[Raghav Chandra VS]]></dc:creator>
            <pubDate>Sun, 12 Sep 2021 15:25:55 GMT</pubDate>
            <atom:updated>2021-09-12T15:25:55.526Z</atom:updated>
            <content:encoded><![CDATA[<p>This blog is based on the JavaScript Courses on Udacity which I had taken to get a better understanding of JavaScript.</p><h3>What is JavaScript ?</h3><p><strong>JavaScript </strong>often abbreviated as <strong>JS</strong>, is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm. It has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-class functions.</p><p>Besides <strong>HTML</strong> and <strong>CSS</strong>, JavaScript is one of the core technologies of the World Wide Web. Most web browsers have a dedicated JavaScript engine to execute the code on the user’s device.</p><h3><strong>Important JavaScript CheatSheet</strong></h3><p>This section covers a few JavaScript Concepts and in built functions.</p><h4><strong>Parameters vs. Arguments</strong></h4><p>At first, it can be a bit tricky to know when something is either a parameter or an argument. The key difference is in where they show up in the code. A<strong> </strong>parameter<strong> </strong>is always going to be a variable name and appears in the function declaration. On the other hand, an argument is always going to be a value (i.e. any of the JavaScript data types — a number, a string, a boolean, etc.) and will always appear in the code when the function is called or invoked.</p><p><strong>function</strong> <strong>findAverage</strong>(x, y)//parameter {</p><p><strong>var</strong> answer = (a+b) / 2;</p><p><strong>return</strong> answer;</p><p>}</p><p><strong>var</strong> avg = findAverage(5, 9); // arguments</p><h4><strong>Hoisting</strong></h4><ul><li>JavaScript hoists function declarations and variable declarations to the top of the current scope.</li><li>Variable <em>assignments</em> are not hoisted.</li><li>Declare functions and variables at the top of your scripts, so the syntax and behavior are consistent with each other.</li></ul><h3><strong>Splice</strong></h3><ul><li><strong>Following is the syntax of </strong><strong>splice() method</strong>: arrayName.splice(arg1, arg2, item1, ....., itemX); where,</li><li>· arg1 = Mandatory argument. Specifies the starting index position to add/remove items. You can use a negative value to specify the position from the end of the array e.g., -1 specifies the last element.</li><li>· arg2 = Optional argument. Specifies the count of elements to be removed. If set to 0, no items will be removed.</li><li>· item1, ....., itemX are the items to be added at index position arg1</li><li>splice() method returns the item(s) that were removed.</li></ul><h3>Split and Join</h3><p>Here’s the situation from real life. We are writing a messaging app, and the person enters the comma-delimited list of receivers: John, Pete, Mary. But for us an array of names would be much more comfortable than a single string. How to get it?</p><p>The str.split(delim) method does exactly that. It splits the string into an array by the given delimiter delim.</p><p>In the example below, we split by a comma followed by space:</p><pre>let names = &#39;Bilbo, Gandalf, Nazgul&#39;;</pre><pre>let arr = names.split(&#39;, &#39;);</pre><pre>for (let name of arr) {</pre><pre>alert( `A message to ${name}.` ); // A message to Bilbo  (and other names)</pre><pre>}</pre><p>The split method has an optional second numeric argument – a limit on the array length. If it is provided, then the extra elements are ignored. In practice it is rarely used though:</p><pre>let arr = &#39;Bilbo, Gandalf, Nazgul, Saruman&#39;.split(&#39;, &#39;, 2);</pre><pre>alert(arr); // Bilbo, Gandalf</pre><p><strong>Split into letters</strong></p><p>The call to split(s) with an empty s would split the string into an array of letters:</p><pre>let str = &quot;test&quot;;</pre><pre>alert( str.split(&#39;&#39;) ); // t,e,s,t</pre><p>The call arr.join(glue) does the reverse to split. It creates a string of arr items joined by glue between them.</p><p>For instance:</p><pre>let arr = [&#39;Bilbo&#39;, &#39;Gandalf&#39;, &#39;Nazgul&#39;];</pre><pre>let str = arr.join(&#39;;&#39;); // glue the array into a string using ;</pre><pre>alert( str ); // Bilbo;Gandalf;Nazgul</pre><h3>JavaScript DOM</h3><p>The Document method <strong>getElementById()</strong> returns an Element object representing the element whose id property matches the specified string.Similarly .getElementsByClassName() ,.getElementsByTagName() return class and tags objects respectively.</p><p>Important***</p><p><em>The difference between </em><em>.getElementById() compared with both </em><em>.getElementsByClassName()and </em><em>.getElementsByTagName() <br>is that </em><em>.getElementsByClassName() and </em><em>.getElementsByTagName() have an extra &quot;s&quot; in their name.</em></p><h3>The querySelector Method</h3><p>We can use the .querySelector() method to select elements just like we do with CSS. We use the .querySelector() method and pass it a string that&#39;s just like a CSS selector:</p><pre><em>// find and return the element with an ID of &quot;header&quot;</em></pre><pre>document.querySelector(&#39;#header&#39;);</pre><pre><em>// find and return the first element with the class &quot;header&quot;</em></pre><pre>document.querySelector(&#39;.header&#39;);</pre><pre><em>// find and return the first &lt;header&gt; element</em></pre><pre>document.querySelector(&#39;header&#39;);</pre><p><strong>An Element’s Inner HTML</strong></p><p>Every element inherits properties and methods from the Element Interface (remember this from the previous lesson!). This means that every element has an .innerHTML property. This property, as it’s rightly named, represents the markup of the element’s content. We can use this property to:</p><ul><li>get an element’s (and all of its descendants!) HTML content</li><li>set an element’s HTML content</li><li>.createElement() to create new elements</li><li>.appendChild() to add a child element to a parent element as its last child</li><li>.createTextNode() to create a text node.</li><li>.insertAdjacentHTML() to put HTML text anywhere around an element</li></ul><h3>Removing a Child Element</h3><p>We can use the .removeChild() method to...wait for it...remove a child element. Basically, this is exactly the opposite of the .appendChild() method. So just like the .appendChild() method, the .removeChild() method requires:</p><ul><li>a parent element</li><li>the child element that will be removed</li></ul><pre>&lt;parent-element&gt;.removeChild(&lt;child-to-remove&gt;);</pre><p>Here’s the .removeChild() documentation page on MDN: <a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild">removeChild docs</a></p><p>We can remove the child element directly with the .remove() method:</p><p><strong>const</strong> mainHeading = document.querySelector(‘h1’);</p><p>mainHeading.remove();</p><h3>Style Page Content</h3><p>.style.property syntax is perfect for setting one style at a time, but it’s not great for controlling multiple styles.</p><p>We can use the .style.cssText property to set multiple CSS styles at once.</p><p>Another way to set styles for an element is to bypass the .style.&lt;property&gt; and .style.cssText properties altogether and use the .setAttribute() method:</p><pre><strong>const</strong> mainHeading = document.querySelector(&#39;h1&#39;);</pre><pre>mainHeading.setAttribute(&#39;style&#39;, &#39;color: blue; background-color: orange; font-size: 3.5em;&#39;);</pre><p>The setAttribute() method is not <em>just</em> for styling page elements. You can use this method to set <em>any</em> attribute for an element. If you want to give an element an ID, you can do that!:</p><h3><strong>EVENTS</strong></h3><p>the .addEventListener() method will let us <em>listen for</em> events and respond to them.</p><p>Here are some pseudo-code to explain how to set an event listener:</p><p>&lt;event-target&gt;.addEventListener(&lt;event-to-listen-for&gt;, &lt;function-to-run-when-an-event-happens&gt;);</p><p>So an event listener needs three things:</p><ol><li>an event target — this is called the <strong>target(on what should it act)</strong></li><li>the type of event to listen for — this is called the <strong>type</strong></li><li>a function to run when the event occurs — this is called the <strong>listener (work assigned)</strong></li></ol><p>There are three different phases during the lifecycle of an event.</p><p>They are:</p><ul><li>the <strong>capturing</strong> phase</li><li>the <strong>at target</strong> phase</li><li>and the <strong>bubbling</strong> phase</li></ul><h3>REPLACE ALL</h3><p>The <strong>replaceAll()</strong> method returns a new string with all matches of a pattern replaced by a replacement.</p><h3>CHAR AT and TYPEOF</h3><p>The charAt() method returns the character at a specified index in a string.</p><p>The index of the first character is 0, the second character is 1, and so on.</p><p>The index of the last character in a string is <em>string</em>.length-1, the second last character is <em>string</em>.length-2.</p><p><strong>typeof</strong> returns a data type of a variable.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=578a198770b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>