{"id":7198,"date":"2021-07-30T12:56:48","date_gmt":"2021-07-30T12:56:48","guid":{"rendered":"https:\/\/www.scmgalaxy.com\/tutorials\/?p=7198"},"modified":"2022-04-13T16:30:47","modified_gmt":"2022-04-13T16:30:47","slug":"how-to-write-function-in-javascript","status":"publish","type":"post","link":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-write-function-in-javascript\/","title":{"rendered":"How to Write Function in JavaScript?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Function<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Function are subprograms which are used to compute a value or perform a task.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Type of Function<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>&#8211;<\/strong> <strong>Library or Built-in functions <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">  Ex: &#8211; valueOf( ) , write( ), alert( ) etc<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>&#8211; User-defined functions<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating and Calling a Function<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Creating a Function<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax: &#8211;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">function function_name( )<br>{<br>Block of statement;<br>}<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/802abaf469d88a83f72f42f782c53508.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Calling a Function<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax: &#8211;<br>function_name( );<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ex: &#8211;<br>display( );<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Rules of <strong>Function<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Function name only starts with a letter, an underscore ( _ ).<\/li><li>Function name cannot start with a number.<\/li><li>Do not use reserved keywords. e.g. else, if etc.<\/li><li>Function names are case-sensitive in JavaScript.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Function Call Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The code inside a function isn\u201ft executed until that function is called.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/44b9a36db3935a45075af60b432f6fe9.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Function with Parameters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax: &#8211;<br>function function_name (parameter1, parameter2, \u2026.)<br>{<br>Block of statement;<br>}<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/f4b2f252018704f3b391618c1447a6f4.js\"><\/script>\n\n\n\n<ul class=\"wp-block-list\"><li>JavaScript function definitions do not specify data types for parameters.<\/li><li>JavaScript functions do not perform type checking on the passed arguments.<\/li><li>JavaScript functions do not check the number of arguments received.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Call Function with Parameter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax: &#8211;<br>function function_name (para1, para2, \u2026.)<br>{<br>Block of statement;<br>}<br>Syntax:-<br>function_name(argument1, argument2);<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/b12dd07ada07e67e83d1408b2c282269.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Function Argument Missing<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If a function is called with missing arguments, the missing values are set to undefined.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/ec4fb9dc2ceafb3d375b5e894c345f29.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Arguments Object<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The arguments object contains an array of the arguments used when the function was called.<br>This object contains an entry for each argument passed to the function, the first entry&#8217;s index starting at 0. The argument&#8217;s object is not an Array. It is similar to an Array but does not have any Array properties except length.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/3db52f5e9f1a3facba70da8d707f7749.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Many Function Arguments<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If a function is called with too many arguments, these arguments can be reached using the arguments object which is a built-in.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/8633149b98995bd061ab2bf000d92ce8.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>Function Function are subprograms which are used to compute a value or perform a task. Type of Function &#8211; Library [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[16],"tags":[3052,5095,5143,4031],"class_list":["post-7198","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-arguments","tag-javascript","tag-javascript-function","tag-parameters"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Write Function in JavaScript? - scmGalaxy<\/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:\/\/www.scmgalaxy.com\/tutorials\/how-to-write-function-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Write Function in JavaScript? - scmGalaxy\" \/>\n<meta property=\"og:description\" content=\"Function Function are subprograms which are used to compute a value or perform a task. Type of Function &#8211; Library [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-write-function-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"scmGalaxy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-30T12:56:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-13T16:30:47+00:00\" \/>\n<meta name=\"author\" content=\"rajeshkumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rajeshkumar\" \/>\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:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-write-function-in-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-write-function-in-javascript\\\/\"},\"author\":{\"name\":\"rajeshkumar\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\"},\"headline\":\"How to Write Function in JavaScript?\",\"datePublished\":\"2021-07-30T12:56:48+00:00\",\"dateModified\":\"2022-04-13T16:30:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-write-function-in-javascript\\\/\"},\"wordCount\":277,\"commentCount\":0,\"keywords\":[\"Arguments\",\"Javascript\",\"javascript function\",\"Parameters\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-write-function-in-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-write-function-in-javascript\\\/\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-write-function-in-javascript\\\/\",\"name\":\"How to Write Function in JavaScript? - scmGalaxy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\"},\"datePublished\":\"2021-07-30T12:56:48+00:00\",\"dateModified\":\"2022-04-13T16:30:47+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-write-function-in-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-write-function-in-javascript\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-to-write-function-in-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Write Function in JavaScript?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\",\"name\":\"scmGalaxy\",\"description\":\"A Platform for Software Engineers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\",\"name\":\"rajeshkumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"caption\":\"rajeshkumar\"},\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/author\\\/rajeshkumar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Write Function in JavaScript? - scmGalaxy","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:\/\/www.scmgalaxy.com\/tutorials\/how-to-write-function-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"How to Write Function in JavaScript? - scmGalaxy","og_description":"Function Function are subprograms which are used to compute a value or perform a task. Type of Function &#8211; Library [&hellip;]","og_url":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-write-function-in-javascript\/","og_site_name":"scmGalaxy","article_published_time":"2021-07-30T12:56:48+00:00","article_modified_time":"2022-04-13T16:30:47+00:00","author":"rajeshkumar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rajeshkumar","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-write-function-in-javascript\/#article","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-write-function-in-javascript\/"},"author":{"name":"rajeshkumar","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361"},"headline":"How to Write Function in JavaScript?","datePublished":"2021-07-30T12:56:48+00:00","dateModified":"2022-04-13T16:30:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-write-function-in-javascript\/"},"wordCount":277,"commentCount":0,"keywords":["Arguments","Javascript","javascript function","Parameters"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.scmgalaxy.com\/tutorials\/how-to-write-function-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-write-function-in-javascript\/","url":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-write-function-in-javascript\/","name":"How to Write Function in JavaScript? - scmGalaxy","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website"},"datePublished":"2021-07-30T12:56:48+00:00","dateModified":"2022-04-13T16:30:47+00:00","author":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361"},"breadcrumb":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-write-function-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.scmgalaxy.com\/tutorials\/how-to-write-function-in-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-to-write-function-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.scmgalaxy.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"How to Write Function in JavaScript?"}]},{"@type":"WebSite","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website","url":"https:\/\/www.scmgalaxy.com\/tutorials\/","name":"scmGalaxy","description":"A Platform for Software Engineers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.scmgalaxy.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361","name":"rajeshkumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","caption":"rajeshkumar"},"url":"https:\/\/www.scmgalaxy.com\/tutorials\/author\/rajeshkumar\/"}]}},"_links":{"self":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/comments?post=7198"}],"version-history":[{"count":1,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7198\/revisions"}],"predecessor-version":[{"id":7208,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7198\/revisions\/7208"}],"wp:attachment":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media?parent=7198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/categories?post=7198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/tags?post=7198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}