String Methods

String Concatenation

🙋 Need help? Ask an expert now!

String is an immutable type, meaning the value cannot be changed. Therefore, each time you change the value of a string, a new string is created. Concatenating a string can be achieved using the '+' operator or the concat() method. String concatenation is one of the most used operations with strings, but overusing it will overflow the memory as each concatenation allocates new memory.

var str1 = "Hello";
var str2 = " ";
var str3 = "World!";

console.log(str1 + str2 + str3);
console.log(str1.concat(str2).concat(str3));
Edit Me on GitHub!

Application Question

Consider the following code segment:

function containsArt(strOne, strTwo, strThree){
              var str = strOne + strTwo + strThree;
              return str.indexOf("art") != -1;
            }
            

The method containsArt() returns true if at least one of the three strings (strOne, strTwo or strThree) contains the substring "art". Otherwise, the method should return false. Which of the following method call demonstrates that the method DOES NOT work as intended?