<?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: Reilly Shofner</title>
    <description>The latest articles on DEV Community by Reilly Shofner (@reilly-s).</description>
    <link>https://dev.to/reilly-s</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%2F2004942%2F02d5cc1d-a55c-482f-a4ae-d023e3827d65.png</url>
      <title>DEV Community: Reilly Shofner</title>
      <link>https://dev.to/reilly-s</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/reilly-s"/>
    <language>en</language>
    <item>
      <title>Understanding Callbacks in JavaScript - From A Beginner For Beginners</title>
      <dc:creator>Reilly Shofner</dc:creator>
      <pubDate>Tue, 04 Feb 2025 23:08:24 +0000</pubDate>
      <link>https://dev.to/reilly-s/understanding-callbacks-in-javascript-from-a-beginner-for-beginners-8ec</link>
      <guid>https://dev.to/reilly-s/understanding-callbacks-in-javascript-from-a-beginner-for-beginners-8ec</guid>
      <description>&lt;p&gt;Have you ever struggled to understand a concept while learning JavaScript? As a software engineering student at Flatiron School, I’ve been there too. In this post, I’ll break down a JavaScript feature that initially caused me a lot of confusion, callback functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Callbacks
&lt;/h2&gt;

&lt;p&gt;Callbacks are a fundamental concept in JavaScript that can seem confusing at first, but they're actually quite straightforward once you grasp the basics. In this article, I will explore what callbacks are, why they're important, and how they are used in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Callback?
&lt;/h2&gt;

&lt;p&gt;When I first started learning JavaScript, I found it confusing that people referred to callback functions as both callbacks and callback functions. Understanding the technical vocabulary in programming can be overwhelming at times, especially if your brand new to coding. It’s important to remember they are the same thing so as to not cause any unnecessary confusion. In JavaScript, a callback function is simply a function passed as an argument to another function to be executed later. I like to think of it as a placeholder that represents a reference to a function to be executed later.&lt;/p&gt;

&lt;p&gt;Here's a simple example to display this concept:&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();
}

greet("Reilly", function() {
    console.log("Greeting completed."); //OUTPUT: Hello, Reilly!
});                               //CALLBACK OUTPUT: Greeting completed.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, greet is the main function, and the second argument we pass to it is our callback function. The greet function does its thing (printing a greeting) and then calls our callback function which in turn notifies the console "Greeting completed".&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Do We Need Callbacks?
&lt;/h2&gt;

&lt;p&gt;Callbacks are particularly useful in JavaScript because they allow us to write asynchronous code. Asynchronous code is code that doesn't necessarily run in sequence. Callbacks allow your code to keep running while it waits for something else to happen, like downloading data from the internet or reading a file from disk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Callbacks
&lt;/h2&gt;

&lt;p&gt;There are two main types of callbacks: synchronous and asynchronous.&lt;/p&gt;

&lt;p&gt;-Synchronous code definition: Tasks are executed one after the other. Each task waits for the previous one to complete before it starts.&lt;/p&gt;

&lt;p&gt;-Asynchronous code definition: Tasks are executed independently, without waiting for the previous task to finish. This is useful for tasks like fetching data from a server or waiting for a timer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Synchronous Callbacks
&lt;/h2&gt;

&lt;p&gt;Synchronous callbacks are executed immediately within the calling function. JavaScript goes down your stack of code reading it in a sort of chronological order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function doSomethingWithNumber(num, callback) {
    callback(num); // Call the callback function with the number
}

function checkNumber(num) {
    if (num === 2 || num === 4) {
        console.log(num); // Logs 2 or 4
    }
}

doSomethingWithNumber(2, checkNumber); // Output: 2
doSomethingWithNumber(4, checkNumber); // Output: 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the doSomethingWithNumber function takes a number(num) and a callback. We use checkNumber for the callback and is executed with the number as the argument. the checkNumber callback checks to see if the (num) is equal to 2 or 4 and if so it logs it to the console. The callback function is executed immediately when its called and run in sequence making this a synchronous callback.&lt;/p&gt;

&lt;h2&gt;
  
  
  Asynchronous callbacks
&lt;/h2&gt;

&lt;p&gt;Asynchronous callbacks are typically used for operations that take time, like network requests or file operations. They allow your code to continue running while waiting for such tasks to complete, without blocking other parts of the program from running and executing. This way, multiple functions can run at the same time, and the order in which they finish doesn’t necessarily follow the order they were started.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fetchData(callback) {
    setTimeout(() =&amp;gt; {
        const data = "Data from server";
        callback(data);  // callback is executed after 2 seconds
    }, 2000);
}

console.log("Requesting data...");
fetchData((data) =&amp;gt; {
    console.log(data); // This logs "Data from server" after 2 seconds
});
console.log("Request sent...");

//OUTPUT ORDER 1."Requesting data... 2. "Request sent..." 3. "Data from server"


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

&lt;/div&gt;



&lt;p&gt;The fetchData() function starts but doesn't block the program. It waits for 2 seconds before invoking the callback with "Data from server", which is logged afterward. This makes it Asynchronous. First the console.log "Requesting data..." is read and output it then sees the callback for fetchData but has a setTimeout making it Asynchronous since it is not the next thing output, the next console.log "Request sent..." is immediately read and output, and finally after the 2 second timeout "Data from server" is output. &lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices for Using Callbacks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Keep it Simple: Avoid nesting too many callbacks. This can lead to what's known as "callback hell," which makes your code hard to read and maintain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use Named Functions: Instead of anonymous functions, use named functions for your callbacks. This makes your code more readable and easier to debug.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;I hope this explanation of Callback functions from a beginner to a beginner proves helpful to anyone seeking clarity on the topic, especially those who may have found more technical jargon or complex examples difficult to understand.&lt;/p&gt;

&lt;p&gt;Sources:&lt;br&gt;
&lt;a href="https://base.flatironschool.com/" rel="noopener noreferrer"&gt;https://base.flatironschool.com/&lt;/a&gt; &lt;br&gt;
&lt;a href="https://www.w3schools.com/js/js_callback.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/js/js_callback.asp&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Callback_function" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Glossary/Callback_function&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=i2SPq-nb3NQ" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=i2SPq-nb3NQ&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=NOlOw03qBfw" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=NOlOw03qBfw&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>callbacks</category>
    </item>
    <item>
      <title>My Journey with AI-Assisted JavaScript Learning</title>
      <dc:creator>Reilly Shofner</dc:creator>
      <pubDate>Tue, 17 Sep 2024 23:43:58 +0000</pubDate>
      <link>https://dev.to/reilly-s/my-journey-with-ai-assisted-javascript-learning-38dm</link>
      <guid>https://dev.to/reilly-s/my-journey-with-ai-assisted-javascript-learning-38dm</guid>
      <description>&lt;p&gt;As a beginner diving into the world of web development, I was both excited and intimidated by the prospect of learning JavaScript. However, I soon discovered that artificial intelligence (AI) could be a valuable tool in my learning journey.&lt;/p&gt;

&lt;p&gt;Discovering AI-Powered Learning Tools&lt;/p&gt;

&lt;p&gt;My exploration of AI-assisted learning began with my school's AI chatbot, designed to assist students in learning and debugging code. This tool felt similar to having a personal tutor at my fingertips, available anytime and providing real-time guidance. This proved to be extremely useful due to time zone differences and my schedule. I found myself using the AI more and more when human tutors were unavailable.&lt;/p&gt;

&lt;p&gt;Imagine having a virtual mentor who can:&lt;br&gt;
-Assess your code to find errors you might have missed&lt;br&gt;
-Provide different and simplified methods of explanation&lt;br&gt;
-Personally guide you through each step needed to reach an answer or &lt;br&gt;
 solution&lt;br&gt;
-Highlight and fix messy or unnecessary code&lt;/p&gt;

&lt;p&gt;This adaptive learning environment kept me engaged and motivated. Even when struggling or lost, having something that could break down the information for me to digest in several different ways made me feel like I was always on the verge of finding a solution and gaining a better understanding.&lt;/p&gt;

&lt;p&gt;Overcoming Abstract Concepts&lt;/p&gt;

&lt;p&gt;JavaScript has many abstract concepts that can be challenging for beginners to grasp. AI assistants help bridge this gap by providing relatable analogies, generating visual representations of code execution, offering step-by-step breakdowns of algorithms, and suggesting real-world use cases for concepts you're learning. By making these connections and offering code breakdowns with thorough explanations, AI assistants helped solidify my understanding of JavaScript fundamentals.&lt;/p&gt;

&lt;p&gt;The Negatives and Limitations&lt;/p&gt;

&lt;p&gt;Towards the end of my school's JavaScript course, I encountered some limitations with the AI chatbot. It struggled to fully comprehend the questions and code snippets I provided, occasionally leading to false information and misleading problem-solving approaches. To prevent others from facing similar issues, I recommend searching reliable sources first when seeking answers to problems. You can then use AI tools to break down code you're writing if you need clarification. Additionally, I strongly advise against relying on copy-pasting code from AI sources. Instead, type out the code yourself. This practice builds muscle memory and encourages deeper thinking about each line of code, ultimately providing a better learning retention.&lt;/p&gt;

&lt;p&gt;Embracing AI-Assisted Learning&lt;/p&gt;

&lt;p&gt;While AI has made significant strides in learning and teaching various programming languages, I've learned that it's meant to complement traditional learning methods, not replace them entirely. My personal recommendation for anyone wondering which AI to use to aid them in learning software engineering is to use Phind. It has been the most accurate and detailed AI I have used so far, even with their free membership. They also offer a premium membership with their most advanced model. Having AI tools in combination with hands-on coding, online resources, and community engagement, I feel well-equipped to tackle the exciting world of JavaScript and Software Engineering!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
