<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ayush Sarodey</title>
    <description>The latest articles on DEV Community by Ayush Sarodey (@ayushsarode).</description>
    <link>https://dev.to/ayushsarode</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1178847%2F8ecbec41-f542-40ec-9072-5e78cff41eb3.jpg</url>
      <title>DEV Community: Ayush Sarodey</title>
      <link>https://dev.to/ayushsarode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ayushsarode"/>
    <language>en</language>
    <item>
      <title>JavaScript Callback Functions Explained: A Beginner's Guide</title>
      <dc:creator>Ayush Sarodey</dc:creator>
      <pubDate>Sun, 29 Oct 2023 13:50:53 +0000</pubDate>
      <link>https://dev.to/ayushsarode/javascript-callback-functions-explained-a-beginners-guide-9ao</link>
      <guid>https://dev.to/ayushsarode/javascript-callback-functions-explained-a-beginners-guide-9ao</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you're new to JavaScript, you might have heard the term 'Callback Function.' But what exactly is it? Why are Callback functions needed in JavaScript? Let's dive into it!&lt;/p&gt;

&lt;h3&gt;
  
  
  What exactly Callback Function is?
&lt;/h3&gt;

&lt;p&gt;A &lt;em&gt;&lt;strong&gt;Callback Function&lt;/strong&gt;&lt;/em&gt; is function that is passed as a argument to another function , and it's executed after the completion of that function. This "parent" function can then execute the callback function at a certain point in its code, often in response to an event or an &lt;em&gt;asynchronous operation&lt;/em&gt;. The primary purpose of a callback is to allow you to define custom behavior that should occur when a particular event or condition is met.&lt;/p&gt;

&lt;p&gt;Now, What is Asynchronous Operation? An &lt;em&gt;&lt;strong&gt;Asynchronous Operation&lt;/strong&gt;&lt;/em&gt; is a task or process that doesn't block or pause the main program's execution while it's running. Instead, it allows the program to continue doing other things while it waits for the asynchronous task to complete. &lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Let's illustrate this with a simple code example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function greet(name, callback) {
  console.log(`Hello, ${name}!`);
  callback();
}

function sayGoodbye() {
  console.log("Goodbye!");
}

greet("John Doe", sayGoodbye);

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

&lt;/div&gt;



&lt;p&gt;This code has two special functions: "greet" and "sayGoodbye." The "greet" function says hello to someone and then asks them to do something. In this case, it says hello to "John Doe" and asks him to say goodbye. The "sayGoodbye" function just says goodbye when it's told to. This code shows how in JavaScript, you can make one function tell another function to do something, which is useful for doing things in a specific order or handling events that happen at different times.&lt;/p&gt;

&lt;h3&gt;
  
  
  setTimeout Callback Function
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;setTimeout&lt;/em&gt; is a JavaScript function that allows you to schedule the execution of a specified callback function after a defined time delay, in milliseconds. This asynchronous feature is commonly used for tasks such as delaying the execution of code, creating timeouts for animations, or managing time-based events in web applications. When the designated delay elapses, the provided callback function is invoked, enabling you to perform actions or processes at a later time without blocking the main thread.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Syntax:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setTimeout(callback, delay);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;callback: A function that you want to execute after the specified delay.&lt;/li&gt;
&lt;li&gt;delay: The time to wait (in milliseconds) before executing the callback function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Basic Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function greet(name, message) {
  console.log(`${message}, ${name}!`);
}

setTimeout(greet, 2000, "John", "Hello"); 
// Calls greet("John", "Hello") after a 2-second delay

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

&lt;/div&gt;



&lt;p&gt;In this example, the greet function is called with "John" and "Hello" as arguments after a 2-second delay.&lt;/p&gt;

&lt;h3&gt;
  
  
  Error handling with callback function
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Error handling with callback functions&lt;/em&gt; is a common practice in asynchronous programming, especially in languages like JavaScript and Python. Callback functions are used to handle the results of asynchronous operations, and error handling is an essential part of ensuring that your code can effectively deal with unexpected problems, this is a crucial part of making it strong and dependable.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Syntax&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function callback(err, result) {
  if (err) {
    console.error("An error occurred:", err);
    // Handle the error, e.g., log it or notify someone
  } else {
    // Handle the result
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basic Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function divide(a, b, callback) {
  if (b === 0) {
    // Handle the error when dividing by zero
    callback(new Error("Division by zero is not allowed"), null);
  } else {
    // Perform the division and provide the result
    callback(null, a / b);
  }
}

// Usage
divide(10, 2, (err, result) =&amp;gt; {
  if (err) {
    console.error("Error:", err.message);
  } else {
    console.log("Result:", result);
  }
});

divide(10, 0, (err, result) =&amp;gt; {
  if (err) {
    console.error("Error:", err.message);
  } else {
    console.log("Result:", result);
  }
});

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

&lt;/div&gt;



&lt;p&gt;The given code defines a &lt;code&gt;divide&lt;/code&gt; function that takes two numbers, &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;, and a callback function. It checks if &lt;code&gt;b&lt;/code&gt; is zero and if so, invokes the callback with an error message, indicating division by zero is not allowed. If &lt;code&gt;b&lt;/code&gt; is non-zero, it calculates the division result &lt;code&gt;a / b&lt;/code&gt; and passes it to the callback along with a null error. The code then demonstrates the use of this function by calling it twice, once with valid inputs (10 and 2) and once with an invalid input (10 and 0), handling and logging errors or results appropriately in each case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;To sum it up, &lt;em&gt;callback functions&lt;/em&gt; are really important in modern programming. They help us write code that's flexible, efficient, and adaptable. So, when you come across a callback function, see it as a helpful tool to make your code look great, work well, and be responsive.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
