Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026
The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.
These methods create new strings by combining or modifying content. Remember, strings are immutable—these methods return new strings.
concat()
Concatenates strings together:
'Flavio'.concat(' ').concat('Copes') //'Flavio Copes'
You can specify multiple arguments:
'Flavio'.concat(' ', 'Copes') //'Flavio Copes'
Joining Strings with + Operator
The simplest and fastest way to join strings is using the + operator:
const name = 'Flavio'
const surname = 'Copes'
const fullname = name + ' ' + surname //'Flavio Copes'
Or use += to append to an existing string:
let name = 'Flavio'
name += ' Copes' //'Flavio Copes'
The + operator is generally recommended over concat() for simplicity and performance.
replace()
Finds the first occurrence of a pattern and replaces it. Returns a new string:
'JavaScript'.replace('Java', 'Type') //'TypeScript'
You can use a regular expression as the pattern:
'JavaScript'.replace(/Java/, 'Type') //'TypeScript'
replace() only replaces the first match unless you use the global flag (/g):
'JavaScript JavaX'.replace(/Java/g, 'Type') //'TypeScript TypeX'
The second parameter can be a function that receives the match details:
'JavaScript'.replace(/Java/, (match, index, originalString) => {
console.log(match, index, originalString)
return 'Test'
}) //TestScript
With capturing groups, the captured values are passed as additional arguments:
'2015-01-02'.replace(
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/,
(match, year, month, day, index, originalString) => {
console.log(match, year, month, day, index, originalString)
return 'Test'
}
) //Test
Replacing All Occurrences
Using a regular expression with the global flag:
const phrase = 'I love my dog! Dogs are great'
phrase.replace(/dog/g, 'cat') //"I love my cat! Dogs are great"
For case-insensitive replacement, add the i flag:
phrase.replace(/dog/gi, 'cat') //"I love my cat! cats are great"
If the string contains special regex characters, escape them:
const escapeRegExp = (string) => {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
Alternative: split and join
const phrase = 'I love my dog! Dogs are great'
phrase.split('dog').join('cat') //"I love my cat! Dogs are great"
repeat()
Repeats a string the specified number of times:
'Ho'.repeat(3) //'HoHoHo'
Returns an empty string if the parameter is 0 or omitted. Throws a RangeError if the parameter is negative.
Introduced in ES2015.