{"id":3382,"date":"2021-02-10T18:38:04","date_gmt":"2021-02-10T23:38:04","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=3382"},"modified":"2021-02-10T18:39:10","modified_gmt":"2021-02-10T23:39:10","slug":"javascript-how-to-use-es6-template-literals","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2021\/02\/10\/javascript-how-to-use-es6-template-literals\/","title":{"rendered":"Javascript: How to use ES6 Template Literals"},"content":{"rendered":"\n<p>When using ES6 with a linter I got this suggestion from sonarqube to replace my standard string concatenation with an apparently preferred <code>Template Literal<\/code>.<\/p>\n\n\n\n<p>Here is the sonarqube message I got:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized is-style-default\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/02\/image-920x1024.png\" alt=\"\" class=\"wp-image-3383\" width=\"690\" height=\"768\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/02\/image-920x1024.png 920w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/02\/image-269x300.png 269w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/02\/image-768x855.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/02\/image-676x753.png 676w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2021\/02\/image.png 978w\" sizes=\"auto, (max-width: 690px) 100vw, 690px\" \/><\/figure>\n\n\n\n<p>The string concatenation that triggered this suggestion was:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">('0000' + hexID.toString(16).toUpperCase()).slice(-4)<\/pre>\n\n\n\n<p>The suggested fix using a Template Literal looks like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">`0000${hexID.toString(16).toUpperCase()}`.slice(-4)<\/pre>\n\n\n\n<p><u>Basically, a template literal has this syntax: <\/u><\/p>\n\n\n\n<p>backtick, <code>`<\/code><br>some string, <code>0000<\/code><br>dollar sign, <code>$<\/code><br>opening bracket, <code>{<\/code><br>string  &#8220;expression&#8221;, <code>hexID.toString(16).toUpperCase()<\/code><br>closing bracket, <code>}<\/code><br>and finally, closing backtick.<code>`<\/code> <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Here is a codepen that demonstrates the differences:<\/p>\n\n\n<p class='codepen'  data-height='350' data-theme-id='0' data-slug-hash='eYBdzMz' data-default-tab='js' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen ES6 Template Literals by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>For more information about Template Literals see:<br><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Template_literals\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Template_literals<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When using ES6 with a linter I got this suggestion from sonarqube to replace my standard string concatenation with an apparently preferred Template Literal. Here is the sonarqube message I got: The string concatenation that triggered this suggestion was: The suggested fix using a Template Literal looks like this: Basically, a template literal has this &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2021\/02\/10\/javascript-how-to-use-es6-template-literals\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Javascript: How to use ES6 Template Literals<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[223,45,224],"class_list":["post-3382","post","type-post","status-publish","format-standard","hentry","category-javascript-language","tag-es6","tag-javascript","tag-template-literals"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=3382"}],"version-history":[{"count":17,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3382\/revisions"}],"predecessor-version":[{"id":3400,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3382\/revisions\/3400"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=3382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=3382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=3382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}