Skip to main content

JavaScript: String Operators

So in javascript, there is a string operator where we have to join strings in some circumstances. We call it concatenation. The Concatenation operator (+) joins up two strings assigned in a variable as a union to pair up the string for more critical detail.

So here in this article, we will see what happens.

  • When a string is concatenated with a number
  • When a string variable is passed in the console log
  • When a string uses assignment operator += 

1. String Concatenation with a Number

var name = 'Michael Jordan'
var number = 75

var numJoin = name + number
console.log(numJoin)

Output: Michael Jordan75

So joining a string with a number concatenates with it irrespective of its datatype.

2. String Variable In Console Log

var pName = 'Michael Jordan'
var sport = 'basketball' 

console.log(pName+ ' was a ' +sport+ ' player back then !')

Output: Michael Jordan was a basketball player back then!

So it concatenates typically with the existing print statement with the operator +.

3. String Uses Assignment Operator +=

var pName = 'Michael Jordan'
var sport = 'basketball' 
var merchSite = 'www.mjordan'
merchSite += '.com'
console.log(pName+ ' was a ' +sport+ ' player back then, so get his merchs by visiting ' +merchSite+'.')

Output: Michael Jordan was a basketball player back then, so get his merchs by visiting www.mjordan.com.

So this adds the string to the existing variable if you want to concatenate using += to a variable.

Tags
Submitted by arilio666 on September 29, 2021

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.