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.