{"id":617,"date":"2020-08-25T17:14:24","date_gmt":"2020-08-25T17:14:24","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=617"},"modified":"2020-09-01T19:09:46","modified_gmt":"2020-09-01T19:09:46","slug":"what-is-callback-in-javascript","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/what-is-callback-in-javascript\/","title":{"rendered":"What is Callback in JavaScript"},"content":{"rendered":"\n<p>A&nbsp;<strong>callback<\/strong>&nbsp;is a plain JavaScript function passed to some other function as an argument or option. In&nbsp;JavaScript, functions are objects. Because of this, a function can take another function as a parameter and calls it inside. This is why we call it a <strong>&#8220;callback<\/strong>&#8220;. Let&#8217;s understand this with an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var message = function() {  \n    console.log(\"This message will be shown after 1 second\");\n}\nsetTimeout(message, 1000); \/\/ message is callback function here<\/code><\/pre>\n\n\n\n<p>A callbacks could be anonymous function too. We can define a function directly inside another function, instead of calling it. Checkout the following example: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>setTimeout(function() {  \n    console.log(\"This message will be shown after 1 second\");\n}, 1000);<\/code><\/pre>\n\n\n\n<p>The output of the above two examples will be same.<\/p>\n\n\n\n<p><strong>Callback Using ES6 Arrow Function:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>setTimeout(() => { \n    console.log(\"This message will be shown after 1 second\");\n}, 1000);<\/code><\/pre>\n\n\n\n<p>Note: The <strong><code>setTimeout<\/code><\/strong> is JavaScript&#8217;s inbuilt function which calls a function or evaluates an expression after a given period of time (in milliseconds).<\/p>\n\n\n\n<p>Thank you for reading. If you found this article helpful, please leave your thoughts in the comment box below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;callback&nbsp;is a plain JavaScript function passed to some other function as an argument or option. In&nbsp;JavaScript, functions are objects. Because of this, a function can take another function as a&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[204,178,205,176,100],"class_list":["post-617","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-callback","tag-es6","tag-function","tag-javascript","tag-js-interview-question","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=617"}],"version-history":[{"count":9,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/617\/revisions"}],"predecessor-version":[{"id":635,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/617\/revisions\/635"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}