{"id":24337,"date":"2015-07-21T21:01:29","date_gmt":"2015-07-21T13:01:29","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=24337"},"modified":"2017-10-31T19:52:45","modified_gmt":"2017-10-31T11:52:45","slug":"javascript-functions","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/javascript-functions\/","title":{"rendered":"JavaScript Functions"},"content":{"rendered":"<p>JavaScript functions are capable of more than just merely enclosing a bunch of codes while waiting for the call to execute. Functions have evolved over time leading to new definitions, execution methods and syntaxes. This post will cover some of the present roles JavaScript functions have played so far.<\/p>\n<p>Knowing the different ways of expressing and defining functions opens up the possibility of <strong>implementing a logic in a more optimal way<\/strong> in JavaScript. Also, you may be able to answer the interview questions more easily.<\/p>\n<h2>Function Expressions<\/h2>\n<p>When you simply state a function with <code>function<\/code> keyword , optional parameters and body of code, it\u2019s a <strong>function <em>declaration<\/em><\/strong>.<\/p>\n<p>Put that declaration in a JavaScript expression (like in an assignment or arithmetic expression), it becomes a <strong>function <em>expression<\/em><\/strong>.<\/p>\n<pre>\/\/ Function declaration\r\nfunction function_name() {};\r\n\/\/ Function expression\r\nvar function_name = function() {};\r\n<\/pre>\n<p>All JavaScript declarations are hoisted (moved up in the scope) during evaluation. Hence writing a function call before the function declaration is okay (since the declaration will be moved up anyway).<\/p>\n<pre>function_name();\/\/function call[WORKS]\r\nfunction function_name(){};\r\n<\/pre>\n<p>Function expressions however aren\u2019t hoisted since the functions become part of the expressions and are not stand-alone declarations.<\/p>\n<pre>function_name();\/\/function call[WON'T WORK]\r\nvar function_name = function(){};\r\n<\/pre>\n<h2>Immediately Invoked Function Expression (IIFE)<\/h2>\n<p>It\u2019s a function expression, the code of which gets executed immediately (only once when it\u2019s evaluated). You can create one by simply adding <code>()<\/code> (syntax used for calling a function) right after a function expression. They can be anonymous (no name to call it with).<\/p>\n<p>Below are the two most common syntaxes to create IIFE:<\/p>\n<pre>(function optional_function_name() {\r\n  \/\/body\r\n}());\r\n<\/pre>\n<p>and<\/p>\n<pre>(function optional_function_name() {\r\n  \/\/body\r\n})();\r\n<\/pre>\n<p>The parenthesis around the function declaration converts it to an expression and then adding <code>()<\/code> after it calls the function. You can use other ways to create IIFE for as long as you add <code>()<\/code> after a function expression (like below), but the preferred methods are the above two.<\/p>\n<pre>\/\/ Some of the ways to create IIFEs\r\n!function() { \/* ... *\/ }();\r\n+function() { \/* ... *\/ }();\r\nnew function() { \/* ... *\/ };\r\n<\/pre>\n<p>IIFE is ideal for writing code that needs to execute only once, namespacing, creating closures, creating private variables and more. Below is an example of IIFE use.<\/p>\n<pre>var page_language = (function () {\r\n  var lang;\r\n  \/\/ Code to get the language of the page\r\n  return lang;\r\n})();\r\n<\/pre>\n<p>The code to get the page\u2019s language executes only once (preferably after the page loads). The result is stored in <code>page_language<\/code> for later use.<\/p>\n<h2>Methods<\/h2>\n<p>When a function is an object\u2019s property, it is called method. Since a function is also an object, a function inside another function is also a method. Below is an example for a method inside object.<\/p>\n<pre>var calc = {\r\n  add : function(a,b){return a+b},\r\n  sub : function(a,b){return a-b}  \r\n}\r\nconsole.log(calc.add(1,2)); \/\/3\r\nconsole.log(calc.sub(80,2)); \/\/78\r\n<\/pre>\n<p>The <code>add<\/code> and <code>sub<\/code> functions are methods of <code>calc<\/code> object.<\/p>\n<p>Now for a function within function example:<\/p>\n<pre>function add(a){\r\n  return function(b){return a+b;}\r\n}\r\nconsole.log(add(1)(2)); \/\/ Output is 3\r\n<\/pre>\n<p>The returned anonymous function is a method of function <code>add<\/code>.<\/p>\n<p>Note: Since parameter (<code>a<\/code>) of function <code>add<\/code> in the above example is available for the following function invoke, this type of process is called <strong>currying<\/strong>.<\/p>\n<h2>Constructors<\/h2>\n<p>When you add <code>new<\/code> keyword before a function and call it, it becomes a constructor that creates instances. Below is an example where constructors are used to create instances of <code>Fruit<\/code> and values are added to each <code>Fruit<\/code>\u2018s properties.<\/p>\n<pre>function Fruit(){   \r\n  var name, family; \/\/ Scientific name & family\r\n  this.getName = function(){return name;};\r\n  this.setName = function(value){name=value};   \r\n  this.getFamily = function(){return family;};\r\n  this.setFamily = function(value){family=value}; \r\n}\r\n\r\nvar apple = new Fruit();\r\napple.setName(\"Malus domestica\");\r\napple.setFamily(\"Rosaceae\");\r\n\r\nvar orange = new Fruit();\r\norange.setName (\"Citrus \u00c3- sinensis\");\r\norange.setFamily (\"Rutaceae\");\r\n\r\nconsole.log(orange.getName()); \/\/ \"Citrus \u00c3- sinensis\"\r\nconsole.log(apple.getName()); \/\/ \"Malus domestica\"\r\nconsole.log(orange.getFamily()); \/\/ \"Rutaceae\"\r\n<\/pre>\n<h2>Arrow Functions (ES6 Standard) [Only in Firefox]<\/h2>\n<p>A new function definition from ES6 Standard provides a shorter syntax for function expression. The syntax is<\/p>\n<pre>() =&gt; { \/* body *\/ }\r\n<\/pre>\n<p>This sample function:<\/p>\n<pre>var sing = function(){\r\n  console.log('singing...')\r\n};\r\n<\/pre>\n<p>is the same as:<\/p>\n<pre>var sing = () =&gt; {\r\n  console.log('singing...')\r\n};\r\n<\/pre>\n<p>Arrow functions are anonymous and does not have its own <code>this<\/code> value, <code>this<\/code> inside it will be same as <code>this<\/code> in the enclosing code. Also, you cannot change it to a constructor with <code>new<\/code> keyword.<\/p>\n<p>They are useful for when you want <code>this<\/code> inside a function to be the same as outside and its shorter syntax makes code for writing function within function concise (like below)<\/p>\n<pre>setInterval(function () {\r\n  console.log('message')\r\n}, 1000);\r\n<\/pre>\n<p>into<\/p>\n<pre>setInterval(() =&gt; console.log('message'), 1000);\r\n<\/pre>\n<h2>Generator Functions (ES6 Standard) [Only in Firefox]<\/h2>\n<p>Another new function definition from ES6 Standard is Generator Function. Generator functions are capable of halting and continuing its execution. Its syntax is:<\/p>\n<pre>function* function_name(){}\r\n<\/pre>\n<p>or<\/p>\n<pre>function *function_name(){}\r\n<\/pre>\n<p>Generator functions create iterators. The iterator\u2019s <code>next <\/code>method is then used to execute the code inside the generator function until the <code>yield<\/code> keyword is reached. After that, the iterated value identified by the <code>yield<\/code> keyword is returned by the generator function and the execution is halted.<\/p>\n<p>The generator function again executes when the <code>next<\/code> method is called until the next <code>yield<\/code> keyword is reached. Once all of the <code>yield<\/code> expressions are executed, the yielded value returns <code>undefined<\/code>.<\/p>\n<p>Below is a simple example:<\/p>\n<pre>function *generator_func(count) {\r\n  for(var i=0;i&lt;count;i++){\r\n    yield i+1;\r\n  }\r\n}\r\n\r\nvar itr = generator_func(4); \r\nconsole.log(itr.next()); \/\/Object { value: 1, done: false }\r\nconsole.log(itr.next()); \/\/Object { value: 2, done: false }\r\nconsole.log(itr.next()); \/\/Object { value: 3, done: false }\r\nconsole.log(itr.next()); \/\/Object { value: 4, done: false }\r\nconsole.log(itr.next()); \/\/Object { value: undefined, done: true }\r\nconsole.log(itr.next()); \/\/Object { value: undefined, done: true }\r\n<\/pre>\n<p>Here\u2019s another example:<\/p>\n<pre>function *randomIncrement(i) {\r\n yield i + 3;\r\n yield i + 5;\r\n yield i + 10;\r\n yield i + 6;\r\n}\r\nvar itr = randomIncrement(4);\r\nconsole.log(itr.next().value); \/\/7\r\nconsole.log(itr.next().value); \/\/9\r\nconsole.log(itr.next().value); \/\/14\r\n<\/pre>\n<p>There\u2019s also a <code>yield*<\/code> expression which passes the value to another generator function<\/p>\n<pre>function *fruits(fruit) {\r\n yield* veggies(fruit); \r\n yield \"Grapes\";\r\n}\r\n\r\nfunction *veggies(fruit){\r\n yield fruit + \" and Spinach\";\r\n yield fruit + \" and Broccoli\";\r\n yield fruit + \" and Cucumber\";\r\n}\r\n\r\nvar itr = fruits(\"Apple\");\r\nconsole.log(itr.next().value); \/\/\"Apple and Spinach\"\r\nconsole.log(itr.next().value); \/\/\"Apple and Broccoli\"\r\nconsole.log(itr.next().value); \/\/\"Apple and Cucumber\"\r\nconsole.log(itr.next().value); \/\/\"Grapes\"\r\nconsole.log(itr.next().value); \/\/undefined\r\n<\/pre>\n<p>Generator functions are useful if you want to go through values one by one at your preferred point in the code by pausing it, rather than in one go like in looping through an array.<\/p>\n<h2>Conclusion<\/h2>\n<p>I\u2019ve included a list of references below, where you will find links to references and articles that go in-depth on different topics separately. <strong>Both the ES6 standard functions will work only in Firefox at the moment<\/strong>.<\/p>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.ecma-international.org\/ecma-262\/6.0\/#sec-ecmascript-language-functions-and-classes\">ECMAScript Language: Functions and Classes<\/a><\/li>\n<li><a href=\"https:\/\/benalman.com\/news\/2010\/11\/immediately-invoked-function-expression\/\">Immediately-Invoked Function Expression (IIFE)<\/a><\/li>\n<li><a href=\"https:\/\/davidwalsh.name\/es6-generators\">The Basics Of ES6 Generators<\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Functions\/Arrow_functions\">Arrow Functions<\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/function*\">Function<\/a><em> \u2013 <\/em>Mozilla Developer Network <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/function*\"><\/a> <\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>JavaScript functions are capable of more than just merely enclosing a bunch of codes while waiting for the call to execute. Functions have evolved over time leading to new definitions, execution methods and syntaxes. This post will cover some of the present roles JavaScript functions have played so far. Knowing the different ways of expressing&hellip;<\/p>\n","protected":false},"author":145,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3392],"tags":[4117],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Different Roles of JavaScript Functions<\/title>\n<meta name=\"description\" content=\"JavaScript functions are capable of more than just merely enclosing a bunch of codes while waiting for the call to execute. Functions have evolved over\" \/>\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.hongkiat.com\/blog\/javascript-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Functions\" \/>\n<meta property=\"og:description\" content=\"JavaScript functions are capable of more than just merely enclosing a bunch of codes while waiting for the call to execute. Functions have evolved over\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/javascript-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2015-07-21T13:01:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-10-31T11:52:45+00:00\" \/>\n<meta name=\"author\" content=\"Preethi Ranjit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Preethi Ranjit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/javascript-functions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/javascript-functions\\\/\"},\"author\":{\"name\":\"Preethi Ranjit\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\"},\"headline\":\"JavaScript Functions\",\"datePublished\":\"2015-07-21T13:01:29+00:00\",\"dateModified\":\"2017-10-31T11:52:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/javascript-functions\\\/\"},\"wordCount\":761,\"commentCount\":15,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"keywords\":[\"Javascripts\"],\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/javascript-functions\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/javascript-functions\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/javascript-functions\\\/\",\"name\":\"Different Roles of JavaScript Functions\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"datePublished\":\"2015-07-21T13:01:29+00:00\",\"dateModified\":\"2017-10-31T11:52:45+00:00\",\"description\":\"JavaScript functions are capable of more than just merely enclosing a bunch of codes while waiting for the call to execute. Functions have evolved over\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/javascript-functions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/javascript-functions\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/javascript-functions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\",\"name\":\"Preethi Ranjit\",\"description\":\"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/preethi\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Different Roles of JavaScript Functions","description":"JavaScript functions are capable of more than just merely enclosing a bunch of codes while waiting for the call to execute. Functions have evolved over","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.hongkiat.com\/blog\/javascript-functions\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Functions","og_description":"JavaScript functions are capable of more than just merely enclosing a bunch of codes while waiting for the call to execute. Functions have evolved over","og_url":"https:\/\/www.hongkiat.com\/blog\/javascript-functions\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2015-07-21T13:01:29+00:00","article_modified_time":"2017-10-31T11:52:45+00:00","author":"Preethi Ranjit","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Preethi Ranjit","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/javascript-functions\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/javascript-functions\/"},"author":{"name":"Preethi Ranjit","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3"},"headline":"JavaScript Functions","datePublished":"2015-07-21T13:01:29+00:00","dateModified":"2017-10-31T11:52:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/javascript-functions\/"},"wordCount":761,"commentCount":15,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"keywords":["Javascripts"],"articleSection":["Coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/javascript-functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/javascript-functions\/","url":"https:\/\/www.hongkiat.com\/blog\/javascript-functions\/","name":"Different Roles of JavaScript Functions","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"datePublished":"2015-07-21T13:01:29+00:00","dateModified":"2017-10-31T11:52:45+00:00","description":"JavaScript functions are capable of more than just merely enclosing a bunch of codes while waiting for the call to execute. Functions have evolved over","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/javascript-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/javascript-functions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/javascript-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript Functions"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3","name":"Preethi Ranjit","description":"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.","url":"https:\/\/www.hongkiat.com\/blog\/author\/preethi\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-6kx","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/24337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=24337"}],"version-history":[{"count":1,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/24337\/revisions"}],"predecessor-version":[{"id":24338,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/24337\/revisions\/24338"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=24337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=24337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=24337"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=24337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}