<?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 Kelvin Sarvaiya on Medium]]></title>
        <description><![CDATA[Stories by Kelvin Sarvaiya on Medium]]></description>
        <link>https://medium.com/@kelvin.sarvaiya?source=rss-b06d19d4d0d0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*YhMWjmy0g4AGfJnl0xmN7A.jpeg</url>
            <title>Stories by Kelvin Sarvaiya on Medium</title>
            <link>https://medium.com/@kelvin.sarvaiya?source=rss-b06d19d4d0d0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:40:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kelvin.sarvaiya/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[Understanding Functions in JavaScript: A Beginner’s Guide]]></title>
            <link>https://medium.com/@kelvin.sarvaiya/understanding-functions-in-javascript-a-beginners-guide-3a6d26cb2df1?source=rss-b06d19d4d0d0------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a6d26cb2df1</guid>
            <category><![CDATA[arrow-functions]]></category>
            <category><![CDATA[basic-javascritp]]></category>
            <category><![CDATA[functions-in-javascript]]></category>
            <category><![CDATA[es6-in-depth]]></category>
            <dc:creator><![CDATA[Kelvin Sarvaiya]]></dc:creator>
            <pubDate>Wed, 06 Nov 2024 12:02:29 GMT</pubDate>
            <atom:updated>2024-11-06T12:02:29.393Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J-b9Qhst_4XqsaAKgStH3A.jpeg" /></figure><p>Functions are one of the fundamental building blocks in JavaScript programming. Whether you’re building a simple script or a complex web application, functions help you organize your code, make it reusable, and keep it maintainable. In this blog post, we’ll dive into the core concepts of functions in JavaScript, explore how they work, and look at some of the key features that make JavaScript functions unique.</p><p><strong>What is a Function?</strong></p><p>A <strong>function</strong> in JavaScript is a block of reusable code designed to perform a particular task. Functions allow you to avoid repeating the same code multiple times, making your code cleaner and easier to maintain.</p><p>In JavaScript, a function can take <strong>inputs</strong> (called <strong>parameters</strong>), perform operations, and then return a result (called the <strong>return value</strong>).</p><p>Here’s the basic structure of a function in JavaScript:</p><pre>function functionName(parameter1, parameter2) {<br>  // Code to execute<br>  return result;<br>}</pre><blockquote><strong>Example: A Simple Function</strong></blockquote><p>Let’s start with a basic example of a function that adds two numbers together:</p><pre>function add(a, b) {<br>  return a + b;<br>}<br><br>let sum = add(5, 2);<br>console.log(sum); // Output: 7</pre><p>In this example:</p><ul><li>The function add() takes two parameters a and b.</li><li>The function returns the sum of a and b.</li><li>We then call the function with the values 5 and 2, and log the result.</li></ul><blockquote><strong>Function Declaration vs Function Expression</strong></blockquote><p>1. <strong>Function Declaration</strong></p><p>A <strong>function declaration</strong> is the traditional way to define a function in JavaScript. It follows this syntax:</p><pre>function functionName(parameters) {<br>  // Code to execute<br>}</pre><p><strong>Example:</strong></p><pre>function welcome(name) {<br>  console.log(&quot;Welcome, &quot; + name);<br>}<br><br>welcome(&quot;Kelvin&quot;); // Output: Welcome, Kelvin</pre><p>Function declarations are <strong>hoisted</strong>, meaning they are moved to the top of their scope during the compilation phase, so you can call the function before it’s defined in the code.</p><p>2. <strong>Function Expression</strong></p><p>A <strong>function expression</strong> involves defining a function as part of an expression. It can be assigned to a variable or passed as an argument to another function.</p><p><strong>Example:</strong></p><pre>const welcome = function(name) {<br>  console.log(&quot;Welcome, &quot; + name);<br>};<br><br>welcome(&quot;Abhi&quot;); // Output: Welcome, Abhi</pre><p>Function expressions are <strong>not hoisted</strong>, meaning you must define them before calling them in your code.</p><blockquote><strong>Arrow Functions (ES6)</strong></blockquote><p>With the introduction of ES6, JavaScript introduced a shorter syntax for writing functions called <strong>arrow functions</strong>. Arrow functions are particularly useful for writing concise, one-line functions and are a popular feature in modern JavaScript.</p><p>The syntax for an arrow function looks like this:</p><pre>const functionName = (parameter1, parameter2) =&gt; {<br>  // Code to execute<br>};</pre><p><strong>Example:<br></strong>Here’s how the add function would look as an arrow function:</p><pre>const add = (a, b) =&gt; a + b;<br><br>let result = add(4, 5);<br>console.log(result); // Output: 9</pre><p>Arrow functions have a few important characteristics:</p><ul><li>They are more concise and eliminate the need for the function keyword.</li><li>They do <strong>not</strong> have their own this keyword (we’ll discuss this later), making them useful in certain scenarios, like callbacks and event handlers.</li></ul><blockquote><strong>Parameters and Arguments</strong></blockquote><p>In JavaScript, functions can accept <strong>parameters</strong> (inputs), which allow you to pass information into the function. However, you can also use <strong>default parameters</strong> and the <strong>arguments object</strong> to make your functions even more flexible.</p><ol><li><strong>Default Parameters</strong></li></ol><p>JavaScript allows you to assign default values to parameters. This is useful when you want to ensure a parameter has a value, even if the caller doesn’t pass one.</p><pre>function welcome(name = &quot;Guest&quot;) {<br>  console.log(&quot;Welcome, &quot; + name);<br>}<br><br>welcome(); // Output: Welcome, Guest<br>welcome(&quot;Kelvin&quot;); // Output: Welcome, Kelvin</pre><p>In this example, the name parameter has a default value of &quot;Guest&quot;. If no argument is provided when calling the function, it will default to &quot;Guest&quot;.</p><p><strong>2. The Arguments Object</strong></p><p>Every JavaScript function has access to an <strong>arguments</strong> object, which contains all the arguments passed to the function. This is especially useful when you don’t know the exact number of arguments ahead of time.</p><p><strong>Example:</strong></p><pre>function sum() {<br>  let total = 0;<br>  for (let i = 0; i &lt; arguments.length; i++) {<br>    total += arguments[i];<br>  }<br>  return total;<br>}<br><br>console.log(sum(1, 2, 3)); // Output: 6<br>console.log(sum(4, 5, 6, 7)); // Output: 22</pre><p>In this example, we use the arguments object to loop through all the passed arguments and calculate their sum</p><blockquote><strong>Returning Values from Functions</strong></blockquote><p>Functions in JavaScript can return values, which can then be used in other parts of your program. You can use the return keyword to specify the value that a function should return.</p><p><strong>Example:</strong></p><pre>function multiply(a, b) {<br>  return a * b;<br>}<br><br>let result = multiply(4, 5);<br>console.log(result); // Output: 20</pre><p>Once a return statement is executed, the function stops running, and the value is sent back to the caller.</p><blockquote><strong>Scope and </strong><strong>this Keyword</strong></blockquote><p>Understanding <strong>scope</strong> and the <strong>this</strong> keyword is crucial for mastering functions in JavaScript.</p><ol><li><strong>Scope</strong></li></ol><p>In JavaScript, <strong>scope</strong> refers to the context in which variables are accessible. Variables defined inside a function are local to that function, while variables defined outside are global.</p><pre>let x = 10;<br><br>function display() {<br>  let y = 20;<br>  console.log(x, y);<br>}<br><br>display(); // Output: 10 20<br>console.log(y); // Error: y is not defined</pre><p>2. <strong>The </strong><strong>this Keyword</strong></p><p>The this keyword refers to the context in which a function is called. In regular functions, this typically refers to the object that called the function, but in arrow functions, it retains the value of this from the surrounding context.</p><p><strong>Example with regular function:</strong></p><pre>function Person(name) {<br>  this.name = name;<br>  this.welcome = function() {<br>    console.log(&quot;Welcome, &quot; + this.name);<br>  };<br>}<br><br>const person1 = new Person(&quot;Kelvin&quot;);<br>person1.welcome(); // Output: Welcome, Kelvin</pre><p><strong>Example with arrow function:</strong></p><pre>const person2 = {<br>  name: &quot;Abhi&quot;,<br>  welcome: () =&gt; {<br>    console.log(&quot;Welcome, &quot; + this.name); // &quot;this&quot; will refer to the surrounding context (not person2)<br>  }<br>};<br><br>person2.welcome(); // Output: Welcome, undefined</pre><p>In arrow functions, this is lexically bound to the surrounding context, which means it doesn&#39;t have its own this. This behavior is useful in certain scenarios, like when passing functions as callbacks.</p><blockquote><strong>Conclusion</strong></blockquote><p>Functions are a vital part of JavaScript and understanding them thoroughly will make you a more effective and efficient developer. In this blog, we’ve covered:</p><ul><li>What functions are and how they work</li><li>Different ways to declare functions (declarations, expressions, and arrow functions)</li><li>Parameters, default parameters, and the arguments object</li><li>Returning values from functions</li><li>Scope and the this keyword</li></ul><p>As you continue to work with JavaScript, you’ll encounter more advanced topics like <strong>callback functions</strong>, <strong>higher-order functions</strong>, and <strong>closures</strong>. But mastering the basics of functions will give you the solid foundation you need to move forward in your JavaScript journey.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a6d26cb2df1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Variables and Data Types in JavaScript]]></title>
            <link>https://medium.com/@kelvin.sarvaiya/variables-and-data-types-in-javascript-26511606a507?source=rss-b06d19d4d0d0------2</link>
            <guid isPermaLink="false">https://medium.com/p/26511606a507</guid>
            <category><![CDATA[job-interview-tips]]></category>
            <category><![CDATA[datatypes-in-javascript]]></category>
            <category><![CDATA[basics]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[data-type]]></category>
            <dc:creator><![CDATA[Kelvin Sarvaiya]]></dc:creator>
            <pubDate>Wed, 23 Oct 2024 12:26:59 GMT</pubDate>
            <atom:updated>2024-10-23T12:26:59.366Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BGZvbophgz35rv3JW_vk-A.jpeg" /></figure><h3>Understanding Variables</h3><p>Variables are like containers in JavaScript that store values. Think of them as labels you attach to information. These labels allow you to reference and manipulate the data throughout your code.</p><p><strong>Purpose of Variables:</strong></p><ul><li><strong>Storing data:</strong> Variables hold values that can be used later in your program.</li><li><strong>Flexibility:</strong> They make your code more dynamic, allowing you to change values without modifying the entire code.</li><li><strong>Readability:</strong> Well-named variables improve code readability and understanding.</li></ul><h3>Data Types in JavaScript</h3><p>JavaScript supports a variety of data types, each with its own characteristics:</p><h4>1. Numbers</h4><ul><li><strong>Integers:</strong> Whole numbers (e.g., 10, -5, 0).</li><li><strong>Floating-point numbers:</strong> Decimal numbers (e.g., 3.14, 2.718).</li></ul><pre>let age = 30; // Integer<br>let pi = 3.14159; // Floating-point number</pre><h4>2. Strings</h4><ul><li>Sequences of characters enclosed in quotes (single or double).</li></ul><pre>let name = &quot;Kelvin&quot;;<br>let message = &#39;Hello, world!&#39;;</pre><h4>3. Booleans</h4><ul><li>Represent true or false values.</li></ul><pre>let isSunny = true;<br>let isRaining = false;</pre><h4>4. Objects</h4><ul><li>Collections of key-value pairs.</li></ul><pre>let person = {<br>  name: &quot;Kelvin&quot;,<br>  age: 25,<br>  city: &quot;Ahmedabad&quot;<br>};</pre><h4>5. Arrays</h4><ul><li>Ordered collections of values.</li></ul><pre>let fruits = [&quot;apple&quot;, &quot;kiwi&quot;, &quot;orange&quot;];</pre><h4>6. null</h4><ul><li>Represents the absence of a value.</li></ul><pre>let emptyValue = null;</pre><h4>7. undefined</h4><ul><li>Indicates a variable has been declared but not assigned a value.</li></ul><pre>let myVariable; // undefined</pre><h3>Variable Declaration and Assignment</h3><p>To create a variable, you need to declare it using the let, const, or var keywords. Then, you can assign a value to it using the assignment operator (=).</p><pre>// Using let<br>let x = 10;<br><br>// Using const (cannot be reassigned)<br>const PI = 3.14159;<br><br>// Using var (avoid using in modern JavaScript)<br>var y = &quot;Hello&quot;;</pre><p>Example:</p><pre>let firstName = &quot;Kelvin&quot;;<br>let lastName = &quot;Sarvaiya&quot;;<br>let age = 25;<br>let isStudent = false;<br><br>console.log(&quot;Name:&quot;, firstName + &quot; &quot; + lastName);<br>console.log(&quot;Age:&quot;, age);<br>console.log(&quot;Is Student:&quot;, isStudent);</pre><p>By understanding variables and data types, you lay the foundation for building more complex JavaScript applications.</p><p><strong>Thank you.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26511606a507" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is an Interface?]]></title>
            <link>https://medium.com/@kelvin.sarvaiya/what-is-an-interface-8da243751cea?source=rss-b06d19d4d0d0------2</link>
            <guid isPermaLink="false">https://medium.com/p/8da243751cea</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[reuse]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[interfaces]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Kelvin Sarvaiya]]></dc:creator>
            <pubDate>Sat, 28 Sep 2024 07:37:59 GMT</pubDate>
            <atom:updated>2024-09-28T07:37:59.957Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ar_IfvAnWan0V4rvdxXHCQ.jpeg" /></figure><p>In TypeScript, an interface acts like a blueprint for a class. It outlines the properties and methods that the class must implement, which helps ensure type safety and makes the code easier to maintain. Using interfaces allows for reusable code structures and promotes better organization within your codebase.</p><p><strong>Creating an Interface<br></strong>To define an interface, you use the interface keyword followed by the interface name and a list of members (properties and methods). Here&#39;s a simple example:</p><pre>interface Person {<br>    name: string;<br>    weight: number;<br>    greet(): void;<br>}</pre><p>In this example, the Person interface defines three members:</p><ul><li>name: A string property representing the person&#39;s name.</li><li>weight: A number property representing the person&#39;s weight.</li><li>greet(): A method that takes no parameters and returns nothing (void).</li></ul><p><strong>Implementing an Interface</strong></p><p>A class can implement an interface by using the implements keyword. When a class implements an interface, it must provide implementations for all the members defined in the interface.</p><pre>class Employee implements Person {<br>    name: string;<br>    weight: number;<br><br>    constructor(name: string, weight: number) {<br>        this.name = name;<br>        this.weight = weight;<br>    }<br><br>    greet() {<br>        console.log(&quot;Hello, my name is &quot; + this.name);   <br>    }<br>}</pre><p>In this example, the Employee class implements the Person interface. It provides implementations for the name, weight, and greet() members, ensuring that it adheres to the contract defined by the interface.</p><p><strong>Benefits of Using Interfaces</strong></p><ul><li><strong>Type Safety:</strong> Interfaces help enforce type safety by ensuring that classes adhere to a specific structure.</li><li><strong>Code Reusability:</strong> Interfaces can be used to create reusable code structures that can be shared across different parts of your application.</li><li><strong>Code Organization:</strong> Interfaces can help improve code organization by defining clear contracts between different parts of your application.</li><li><strong>Polymorphism:</strong> Interfaces enable polymorphism, allowing you to treat objects of different types in a uniform way.</li></ul><p><strong>Additional Considerations</strong></p><ul><li><strong>Optional Properties:</strong> You can make properties in an interface optional by using the ? symbol.</li><li><strong>Readonly Properties:</strong> You can make properties in an interface read-only by using the readonly keyword.</li><li><strong>Extending Interfaces:</strong> You can extend one interface with another using the extends keyword.</li></ul><p>By understanding and effectively using interfaces, you can write more robust, maintainable, and type-safe TypeScript code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8da243751cea" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>