JavaScript Glossary - Lesson #20

JavaScript String .concat() Method

This method is used to join the calling string and the provided string arguments. It returns a new String object containing the calling string and the provided arguments . If no arguments are provided, it returns the original calling string.

js
""Hello"".concat("" World"")
// ""Hello World""
js
// New strings
const strA = "How old"
const strB = " are you?"

// Concatenate strings
let joinedString = strA.concat(strB)

// Display joined string
console.log(joinedString) // How old are you?

The concat() method:

  • Takes values as parameters
  • Appends specified parameters to parent String.
  • Returns a new String containing the provided arguments and the calling string.
  • If no argument is provided, it returns the original string.
js
oldString.concat(newString2, newString3,...newStringN)

newString2...newStringN These are values to be joined to the parent String. This can be a number values passed as arguments.

The method will return a String containing the parent String and the values specified as arguments. If no arguments are provided, it will return the calling string object.

The concat() method can be used to convert number arguments into a single string containing all the numbers in the same order as the argument.

The .includes() method can be used to search a list of comma separated values in a string.

js
// Create empty string
const string = ""

// Convert numbers to string 
const stringOfNumbers = string.concat(1,2,3,4,5)

// Display result
console.log(stringOfNumbers) // 12345

Want to improve your coding and design skills?

I'm continually researching the best practices and tools for coding.
Join 50,000+ developers looking to make cool stuff.

We value your privacy. 1-click unsubscribe.

Chris Sev

Chris Sev

Chris Sev is the co-founder of Better Dev. Coding better every day. Previously he created Scotch.io which was acquired.

Comments

What did you think of the article? Let us know!
(these comments are powered by GitHub issues and use 0 trackers)