{"id":142,"date":"2026-05-05T11:20:54","date_gmt":"2026-05-05T11:20:54","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=142"},"modified":"2026-05-05T11:20:55","modified_gmt":"2026-05-05T11:20:55","slug":"callbacks","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=callbacks","title":{"rendered":"Callbacks"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<br>Callbacks are a fundamental concept in JavaScript that allow functions to be executed after another function has completed. They are widely used for handling asynchronous operations such as loading data, responding to user actions, and working with APIs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What is a Callback<br>A callback is a function passed as an argument to another function. This function is then executed later, usually after a specific task is finished. Callbacks help control the order of execution in JavaScript.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Simple Example<br>function greet(name, callback) {<br>console.log(&#8220;Hello &#8221; + name);<br>callback();<br>}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">function sayGoodbye() {<br>console.log(&#8220;Goodbye&#8221;);<br>}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">greet(&#8220;Ali&#8221;, sayGoodbye);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, sayGoodbye is a callback function that runs after the greet function finishes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Why Callbacks are Important<br>Callbacks allow JavaScript to handle tasks that take time, such as fetching data from a server. Without callbacks, code execution could become disorganized or delayed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Asynchronous Callbacks<br>JavaScript uses callbacks to manage asynchronous operations. This means tasks can run in the background without stopping the rest of the program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example with setTimeout<br>setTimeout(function() {<br>console.log(&#8220;This message appears after 2 seconds&#8221;);<br>}, 2000);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the function runs after a delay, showing how callbacks work with timing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Error Handling in Callbacks<br>Callbacks often include error handling by passing an error as the first argument.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<br>function fetchData(callback) {<br>let success = true;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">if (success) {<br>callback(null, &#8220;Data received&#8221;);<br>} else {<br>callback(&#8220;Error occurred&#8221;, null);<br>}<br>}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">fetchData(function(error, data) {<br>if (error) {<br>console.log(error);<br>} else {<br>console.log(data);<br>}<br>});<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Callback Hell<br>When multiple callbacks are nested inside each other, the code becomes difficult to read and manage. This situation is called callback hell.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Solution to Callback Hell<br>Modern JavaScript provides alternatives such as Promises and async or await to write cleaner and more readable code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Best Practices<br>Keep callback functions simple and focused<br>Use named functions instead of anonymous functions for clarity<br>Handle errors properly<br>Avoid deep nesting of callbacks<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion<br>Callbacks are essential for managing asynchronous behavior in JavaScript. Understanding callbacks helps developers build efficient and responsive web applications.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/javascript\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Advanced JavaScript > Asynchronous JavaScript > Callbacks<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777980016613\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777980016392\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":0,"template":"","class_list":["post-142","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Callbacks - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn JavaScript callbacks with simple examples, understand asynchronous programming, and improve your web development skills fast\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/javascript\/?lesson=callbacks\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Callbacks - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn JavaScript callbacks with simple examples, understand asynchronous programming, and improve your web development skills fast\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=callbacks\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-05T11:20:55+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=callbacks\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=callbacks\",\"name\":\"Callbacks - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-05T11:20:54+00:00\",\"dateModified\":\"2026-05-05T11:20:55+00:00\",\"description\":\"Learn JavaScript callbacks with simple examples, understand asynchronous programming, and improve your web development skills fast\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=callbacks#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=callbacks\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=callbacks#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced JavaScript > Asynchronous JavaScript > Callbacks\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/\",\"name\":\"Learn JavaScript with GIGZ.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Callbacks - Learn JavaScript with GIGZ.PK","description":"Learn JavaScript callbacks with simple examples, understand asynchronous programming, and improve your web development skills fast","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/javascript\/?lesson=callbacks","og_locale":"en_US","og_type":"article","og_title":"Callbacks - Learn JavaScript with GIGZ.PK","og_description":"Learn JavaScript callbacks with simple examples, understand asynchronous programming, and improve your web development skills fast","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=callbacks","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-05T11:20:55+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/javascript\/?lesson=callbacks","url":"https:\/\/gigz.pk\/javascript\/?lesson=callbacks","name":"Callbacks - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-05T11:20:54+00:00","dateModified":"2026-05-05T11:20:55+00:00","description":"Learn JavaScript callbacks with simple examples, understand asynchronous programming, and improve your web development skills fast","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=callbacks#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=callbacks"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=callbacks#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"Advanced JavaScript > Asynchronous JavaScript > Callbacks"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/javascript\/#website","url":"https:\/\/gigz.pk\/javascript\/","name":"Learn JavaScript with GIGZ.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/javascript\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}