{"id":4301,"date":"2022-03-04T07:01:47","date_gmt":"2022-03-04T07:01:47","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4301"},"modified":"2022-03-04T07:19:37","modified_gmt":"2022-03-04T07:19:37","slug":"javascript-string-interpolation","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/03\/04\/javascript-string-interpolation\/","title":{"rendered":"Javascript &#8211; String Interpolation"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>An alternative to using string concatenation is to use string interpolation. String interpolation allows us to put variables in the string value which are then replaced by their values at runtime.<\/p>\n\n\n\n<p>There are a couple of rules to follow. The string must be defined in backticks eg. `  . Strings which are defined using normal double quotes or single quotes do not handle string interpolation.<\/p>\n\n\n\n<p>The sample code below shows an example of this. Note that you can put expressions or even call functions within the template string delimiters.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n&lt;html&gt;\n\t&lt;head&gt;&lt;title&gt;String Interpolation&lt;\/title&gt;&lt;\/head&gt;\n\n\t&lt;script&gt;\n\t\tvar number = 20;\n\t\tvar cost = 5.00;\n\t\tvar price = 100.00;\n\t\tvar msg1 = &quot;The price of &quot; + number + &quot; objects at a cost of &quot; + cost + &quot; each comes to &quot; + price;\n\t\tdocument.write(msg1);\n\n\t\tvar msg2 = `The price of ${number} objects at a cost of ${cost} each comes to ${price}`;\n\t\tdocument.write(&quot;&lt;br&gt;&quot; + msg2);\n\n\t\tvar msg3 = `The price of ${number} objects at a cost of ${cost} each comes to ${cost * number}`;\n\t\tdocument.write(&quot;&lt;br&gt;&quot; + msg2);\n\n\n\t&lt;\/script&gt;\n\t&lt;body&gt;\n\t&lt;\/body&gt;\n&lt;\/html&gt;\n\n<\/pre><\/div>\n\n\n<p>The output is given below<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The price of 20 objects at a cost of 5 each comes to 100\n The price of 20 objects at a cost of 5 each comes to 100\n The price of 20 objects at a cost of 5 each comes to 100 <\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW An alternative to using string concatenation is to use string interpolation. String interpolation allows us to put variables in the string value which are <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/03\/04\/javascript-string-interpolation\/\" title=\"Javascript &#8211; String Interpolation\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2463,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-4301","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4301"}],"version-history":[{"count":1,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4301\/revisions"}],"predecessor-version":[{"id":4302,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4301\/revisions\/4302"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2463"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}