{"id":147354,"date":"2018-10-15T04:13:52","date_gmt":"2018-10-15T04:13:52","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=147354"},"modified":"2018-10-15T06:02:47","modified_gmt":"2018-10-15T06:02:47","slug":"declaring-function-in-javascript","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/","title":{"rendered":"Declaring Function ( in JavaScript )"},"content":{"rendered":"<p><strong>What is Function?<\/strong><\/p>\n<p>Function are the small chunks of modules designed to work for a specific task or purpose. Functions that we define for single purpose are easy to debug and use.These functions when defined\u00a0for single\u00a0purpose can be used again in future. Most programming languages come with a prewritten set of <span style=\"text-decoration: underline\">functions<\/span> that are kept in a <span style=\"text-decoration: underline\">library<\/span>. You can also write your own functions to perform specialized tasks.<\/p>\n<p>You can see below I have defined how these functions can be declared in javascript below :-<\/p>\n<p><strong>Regular Function<\/strong><\/p>\n<p>A function is declared with function keyword, function name and parameters.<br \/>\nHere `function` is a keyword used to declare function, then followed by function name ( myFunc ) and inside parenthesis we have variables (parameter_1, parameter_2) whose value will be passed while declaration.<\/p>\n<pre class=\"brush:js\">\/\/ get sum of 2 variables\r\nfunction myFunc(parameter_1, parameter_2) {\r\nlet parameter = parameter_1 + parameter_2;\r\nreturn parameter;\r\n}\r\n\/\/ function calling using function name\r\nmyFunc();\r\n<\/pre>\n<p><strong>Anonymous function<\/strong><\/p>\n<p>A function that has no name of itself and are declared with function keyword are called Anonymous function.<br \/>\nThese functions can be stored in a variable and are always invoked (called) using the variable name in which function is stored.<\/p>\n<pre class=\"brush:js\">\/\/ declaration of function and assigning it to a variable\r\nlet x = function (a, b) {return a * b};\r\n\/\/ function called using variable name\r\nx(6, 4);<\/pre>\n<p><strong>Function() Constructor<\/strong><\/p>\n<p>These are the functions that we can declare with a built-in JavaScript function constructor.<br \/>\nWe have to use Function keyword to declare a function in this type of function.<\/p>\n<pre class=\"brush:js\">\/\/ get sum of two variables\/parameters from function\r\nlet myFunc = new Function(\"parameter_1\", \"parameter_2\", \"return parameter_1 + parameter_2\");\r\n\/\/ function calling using variable name\r\nlet x = myFunc(6, 4);<\/pre>\n<p><strong>Self-Invoking Function<\/strong><\/p>\n<p>When you want to invoke a function without being called then you can use Self-Invoking Functions as they are invoked (started) automatically, without being called.<br \/>\nThe only requirement is that it should be ended with ().<\/p>\n<pre class=\"brush:js\">\/\/ function calling itself\r\n(function () {\r\nvar x = \"Hello World!!\";\r\n})();\r\n\r\nor\r\n\r\n\/\/ function calling itself\r\n(function () {\r\nvar x = \"Hello World!!\";\r\n}());\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>That\u2019s all for today on how to declare functions in JavaScript, if you still have issues feel free to comment below for any query.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Function? Function are the small chunks of modules designed to work for a specific task or purpose. Functions that we define for single purpose are easy to debug and use.These functions when defined\u00a0for single\u00a0purpose can be used again in future. Most programming languages come with a prewritten set of functions that are kept <a href=\"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":225,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[198],"tags":[2064,7667],"class_list":["post-147354","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript","tag-types-of-function"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Declaring Function ( in JavaScript ) - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Declaring Function ( in JavaScript ) - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"What is Function? Function are the small chunks of modules designed to work for a specific task or purpose. Functions that we define for single purpose are easy to debug and use.These functions when defined\u00a0for single\u00a0purpose can be used again in future. Most programming languages come with a prewritten set of functions that are kept [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-10-15T04:13:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-10-15T06:02:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Anant Negi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Anant Negi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/\"},\"author\":{\"name\":\"Anant Negi\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/0cc15fd2dc52701b5d9599e2d2495db9\"},\"headline\":\"Declaring Function ( in JavaScript )\",\"datePublished\":\"2018-10-15T04:13:52+00:00\",\"dateModified\":\"2018-10-15T06:02:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/\"},\"wordCount\":272,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"JavaScript\",\"types of function\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/\",\"url\":\"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/\",\"name\":\"Declaring Function ( in JavaScript ) - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2018-10-15T04:13:52+00:00\",\"dateModified\":\"2018-10-15T06:02:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Declaring Function ( in JavaScript )\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/0cc15fd2dc52701b5d9599e2d2495db9\",\"name\":\"Anant Negi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/df5d3b06dfdfe65852f190a5beaf4fbcfa9079ccb2ef54e42d3d3b2e84218c5b?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/df5d3b06dfdfe65852f190a5beaf4fbcfa9079ccb2ef54e42d3d3b2e84218c5b?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Anant Negi\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/anantnegi-oc198\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Declaring Function ( in JavaScript ) - Webkul Blog","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:\/\/webkul.com\/blog\/declaring-function-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Declaring Function ( in JavaScript ) - Webkul Blog","og_description":"What is Function? Function are the small chunks of modules designed to work for a specific task or purpose. Functions that we define for single purpose are easy to debug and use.These functions when defined\u00a0for single\u00a0purpose can be used again in future. Most programming languages come with a prewritten set of functions that are kept [...]","og_url":"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2018-10-15T04:13:52+00:00","article_modified_time":"2018-10-15T06:02:47+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Anant Negi","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Anant Negi","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/"},"author":{"name":"Anant Negi","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/0cc15fd2dc52701b5d9599e2d2495db9"},"headline":"Declaring Function ( in JavaScript )","datePublished":"2018-10-15T04:13:52+00:00","dateModified":"2018-10-15T06:02:47+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/"},"wordCount":272,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["JavaScript","types of function"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/","url":"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/","name":"Declaring Function ( in JavaScript ) - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2018-10-15T04:13:52+00:00","dateModified":"2018-10-15T06:02:47+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/declaring-function-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Declaring Function ( in JavaScript )"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/0cc15fd2dc52701b5d9599e2d2495db9","name":"Anant Negi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/df5d3b06dfdfe65852f190a5beaf4fbcfa9079ccb2ef54e42d3d3b2e84218c5b?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/df5d3b06dfdfe65852f190a5beaf4fbcfa9079ccb2ef54e42d3d3b2e84218c5b?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Anant Negi"},"url":"https:\/\/webkul.com\/blog\/author\/anantnegi-oc198\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/147354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/225"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=147354"}],"version-history":[{"count":15,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/147354\/revisions"}],"predecessor-version":[{"id":147668,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/147354\/revisions\/147668"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=147354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=147354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=147354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}