{"id":972,"date":"2023-12-14T13:00:00","date_gmt":"2023-12-14T13:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=972"},"modified":"2023-12-15T06:11:26","modified_gmt":"2023-12-15T06:11:26","slug":"concatenate-strings-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/concatenate-strings-javascript\/","title":{"rendered":"Concatenate Strings in JavaScript (3 Easy Methods)"},"content":{"rendered":"\n<p>Various string operations can be used to manipulate them for different uses. One of them is Concatenation. In this article, we will learn to concatenate strings together. We will explore different techniques for concatenating strings in JavaScript, including the concat() method. Before that, let&#8217;s revise what strings are.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What are Strings in JavaScript?<\/strong><\/h2>\n\n\n\n<p>Strings in JavaScript represent a sequence of multiple characters. \u2018C\u2019 represents a single character and \u201cCat\u201d represents a string. In JavaScript, we can declare a string in two ways. We can declare a string in JavaScript by making a string literal using double quotes.<\/p>\n\n\n\n<p>The syntax for the same is given below:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a string literal\nlet a=&quot;FavTutor&quot;\n\n\/\/printing the string\nconsole.log(a);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>FavTutor<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3 Methods to Concatenate Strings in JavaScript<\/strong><\/h2>\n\n\n\n<p>String concatenation is a fundamental operation in JavaScript. It means combining or joining strings together to create a new string.  Let\u2019s check out various methods for how to do it in JavaScript:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Using the + Operator<\/strong><\/h3>\n\n\n\n<p>The first method of concatenating the string is by using the<strong> \u201c+\u201d<\/strong> operator. It is one of the simplest and most commonly used methods. \u201c+\u201d symbol in general terms represents additivity. So, we can concatenate the strings using it.\u00a0<\/p>\n\n\n\n<p>Here\u2019s an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring one string\nlet first = &quot;Fav&quot;;\n\n\/\/declaring another string\nlet last = &quot;Tutor&quot;;\n\n\/\/concatenating two strings\nlet full = first+last;\n\n\/\/printing the string\nconsole.log(full);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>FavTutor<\/code><\/pre>\n\n\n\n<p>In this example, we had two strings, first and last which are operated through the \u201c+\u201d operator and concatenated together. Thus, we have got the desired result.<\/p>\n\n\n\n<p>Even when using numbers as strings, the + operator just joins them together. Look at the below code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring one string\nlet first = &quot;3&quot;;\n\n\/\/declaring another string\nlet last = &quot;5&quot;;\n\n\/\/concatenating two strings\nlet full = first+last;\n\n\/\/printing the string\nconsole.log(full);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>35<\/code><\/pre>\n\n\n\n<p>We have obtained the desired result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Using the concat() Method<\/strong><\/h3>\n\n\n\n<p><strong>The concat() method is the best way for string concatenation in JavaScript. It allows you to join multiple strings together by calling the concat() method on one string and passing the other strings as arguments.<\/strong><\/p>\n\n\n\n<p>Let us see an example of how to use concat() method:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring one string\nlet first = &quot;Fav&quot;;\n\n\/\/declaring another string\nlet last = &quot;Tutor&quot;;\n\n\/\/concatenating two strings using concat()\nlet full = first.concat(last);\n\n\/\/printing the string\nconsole.log(full);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>FavTutor<\/code><\/pre>\n\n\n\n<p>In this example, we had two strings: first and last. To perform the concatenation operation, we used the concat() method which combines or joins both strings. We have got the desired result.<\/p>\n\n\n\n<p>Let\u2019s look at another example, where we do not declare the second string but use it directly within the concat() method:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring one string\nlet first = &quot;Fav&quot;;\n\n\/\/concatenating two strings using concat()\nlet full = first.concat(&quot;Tutor&quot;);\n\n\/\/printing the string\nconsole.log(full);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>FavTutor<\/code><\/pre>\n\n\n\n<p>Now, what if we use a number inside the concat() method? Let\u2019s understand with the help of an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring one string\nlet first = &quot;Fav&quot;;\n\n\/\/concatenating two strings using concat()\nlet full = first.concat(3);\n\n\/\/printing the string\nconsole.log(full);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Fav3<\/code><\/pre>\n\n\n\n<p>So, in this example, the number 3 is converted to a string and is concatenated to the first string to give us the desired result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Using Template Literal<\/strong><\/h3>\n\n\n\n<p>We can concatenate strings in JavaScript with the help of Template literals. It is a modern and flexible way to concatenate strings in JavaScript. We can embed the expressions and variables directly within a string using the backticks (`). Enclose the strings to be concatenated in the ${} curly braces within the backticks.\u00a0\u00a0<\/p>\n\n\n\n<p>Let us see an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring first string\nlet first=&quot;Fav&quot;;\n\n\/\/declaring the last string\nlet last=&quot;Tutor&quot;;\n\n\/\/concatenating using the (`) template literals\nlet full=`${first}${last}`;\n\n\/\/printing the concatenated string\nconsole.log(full);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>FavTutor<\/code><\/pre>\n\n\n\n<p>In this example, we have used the template literals to concatenate two string variables. We enclose the string variables within ${} within the backticks. Hence, we create a template literal that will evaluate this expression and give us the desired output.<\/p>\n\n\n\n<p>Sometimes you might a string in array form, so must know <a href=\"https:\/\/favtutor.com\/articles\/convert-array-to-string-javascript\/\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/articles\/convert-array-to-string-javascript\/\">how to convert an array to a string in JavaScript<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Which method is best for concatenation?<\/strong><\/h3>\n\n\n\n<p>Several different projects have several different requirements. We have to choose between the string concatenation methods according to our requirements.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>+ operator <\/strong>is generally used<strong> <\/strong>for simple concatenations or when working with a small number of strings.<\/li>\n\n\n\n<li><strong>concat() method <\/strong>is used when we are concatenating multiple strings or arrays of strings.<\/li>\n\n\n\n<li><strong>Template Literals <\/strong>are commonly used for complex string interpolations or when we are incorporating expressions or variables within a string.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we have explored various methods for string concatenation in JavaScript. We have used the \u201c+\u201d operator, the concat() method, and template literals to concatenate strings together. It is important to know these methods to use them in your projects efficiently.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We discussed how to concatenate two strings in JavaScript, including the concat() method, unary plus operation, and template literals.<\/p>\n","protected":false},"author":9,"featured_media":975,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[9],"tags":[11,38],"class_list":["post-972","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript","tag-javascript-strings"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=972"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/972\/revisions"}],"predecessor-version":[{"id":989,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/972\/revisions\/989"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/975"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}