JavaScript String Operators

The + operator can also be used to add (concatenate) strings. When used on strings, the + operator is called the concatenation operator.

HTML file: Displayed by browser:
<body>
<h1>Using + Operator</h1>
<p>The operator + concatenates (adds) strings.</p>
<p id="demo"></p>
<script>
txt1 = "John";
txt2 = "Doe";
txt3 = txt1 + " " + txt2;
document.getElementById("demo").innerHTML = txt3;
</script>
</body>

Using + Operator

The operator + concatenates (adds) strings.

The += assignment operator can also be used to add (concatenate) strings:

HTML file: Displayed by browser:
<body>
<h1>Using += Assignment Operator</h1>
<p>The assignment operator += can concatenate strings as well.</p>
<p id="string"></p>
<script>
txt4 = "What a very ";
txt4 += "nice day";
document.getElementById("string").innerHTML = txt4;
</script>
</body>

Using += Assignment Operator

The assignment operator += can concatenate strings as well.


BackTable of ContentsNext