{"id":639,"date":"2024-07-30T19:40:25","date_gmt":"2024-07-30T17:40:25","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=639"},"modified":"2024-07-30T19:40:25","modified_gmt":"2024-07-30T17:40:25","slug":"004-template-strings","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/03-tipos-de-datos-en-javascript\/004-template-strings\/","title":{"rendered":"004. Template Strings"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">En este cap\u00edtulo vamos a ver 2 caracter\u00edsticas de las cadenas de texto:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Concatenaci\u00f3n<\/li>\n\n\n\n<li>Interpolaci\u00f3n<\/li>\n<\/ul>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Concatenaci\u00f3n<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Para <strong>concatenar<\/strong> una cadena de texto con otra <strong>cadena<\/strong>, o con una <strong>variable<\/strong>, lo \u00fanico que hay que hacer es utilizar el s\u00edmbolo de la <strong>suma<\/strong> (+), de la siguiente manera.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &lt;script&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; let nombre = \"Francisco\";\n&nbsp; &nbsp; &nbsp; &nbsp; let apellido = \"Paredes\";\n&nbsp; &nbsp; &nbsp; &nbsp; let saludo = \"Hola mi nombre es \" + nombre + \" \" + apellido;\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(saludo);\n&nbsp; &nbsp; &lt;\/script&gt;\n\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Interpolaci\u00f3n de variables<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Interpolar<\/strong> significa meter dentro de una cadena de texto el valor din\u00e1micamente de una <strong>variable<\/strong>, para ello tenemos que hacer uso de una caracter\u00edstica denominada <strong>Template String<\/strong>, de la siguiente manera. Para ello utilizamos el <strong>acento invertido<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; &nbsp; let saludo2 = `Hola mi nombre es ${nombre} ${apellido}`;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">El ejercicio completo con <strong>concatenaci\u00f3n<\/strong> e <strong>interpolaci\u00f3n<\/strong> queda de la siguiente manera.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n&nbsp; &nbsp; &lt;meta charset=\"UTF-8\"&gt;\n&nbsp; &nbsp; &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n&nbsp; &nbsp; &lt;title&gt;Template Strings&lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n&nbsp; &nbsp; &lt;h1&gt;Template Strings&lt;\/h1&gt;\n\n&nbsp; &nbsp; &lt;script&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; let nombre = \"Francisco\";\n&nbsp; &nbsp; &nbsp; &nbsp; let apellido = \"Paredes\";\n&nbsp; &nbsp; &nbsp; &nbsp; let saludo = \"Hola mi nombre es \" + nombre + \" \" + apellido;\n&nbsp; &nbsp; &nbsp; &nbsp; let saludo2 = `Hola mi nombre es ${nombre} ${apellido}`;\n\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(saludo);\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(saludo2);\n&nbsp; &nbsp; &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Veamos otro ejm de <strong>interpolaci\u00f3n<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"es\"&gt;\n&lt;head&gt;\n&nbsp; &nbsp; &lt;meta charset=\"UTF-8\"&gt;\n&nbsp; &nbsp; &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n&nbsp; &nbsp; &lt;title&gt;Template Strings 2&lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n&nbsp; &nbsp; &lt;h1&gt;Template Strings 2&lt;\/h1&gt;\n\n&nbsp; &nbsp; &lt;script&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; let ul = `\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ul&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;li&gt;Primavera&lt;\/li&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;li&gt;Verano&lt;\/li&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;li&gt;Oto\u00f1o&lt;\/li&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;li&gt;Invierno&lt;\/li&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/ul&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; `;\n\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(ul);\n&nbsp; &nbsp; &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este cap\u00edtulo vamos a ver 2 caracter\u00edsticas de las cadenas de texto:<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":628,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","footnotes":""},"class_list":["post-639","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"En este cap\u00edtulo vamos a ver 2 caracter\u00edsticas de las cadenas de texto:","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=639"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/639\/revisions"}],"predecessor-version":[{"id":641,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/639\/revisions\/641"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/628"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}