What is the string.concat() method in TypeScript?

Overview

Using the concat() method in TypeScript is similar to JavaScript. We can use this method to concatenate two or more strings on the go.

Syntax

string1.concat(string2, stringg3, ... stringN)
Syntax for concat() method in TypeScript

Parameters

  • string1: This is the string we want to concatenate with other strings.
  • string2, string3, ... stringN: These are the strings we want to concatenate with string1. It can be one or more.

Return value

It returns a string. This string is a concatenation of all the strings we joined using the concat() method.

Example

export {}
// create some strings.
let name:string = "Theodore"
let role:string = "Software Developer"
let nickName:string = "Kcee"
let hubby:string = "Coding"
// concatenate these strings
let fullString = name.concat(" is a ", role, " with a nickname ",
nickName, ". He loves ", hubby)
// log out the concatenated strings
console.log(fullString)

Explanation

  • Line 1: We export our file as a module.
  • Lines 4–7: We create some strings. These are the strings we want to concatenate.
  • Lines 10–11: We concatenate the strings with some other strings.
  • Line 14: We log out the concatenated string.

Free Resources