3 Example(s) of JavaScript String operators


Description :

Example of String Operator " " concatenates two strings


JavaScript String operators Example - 1
<!DOCTYPE html>
<html>
 <head>
 <title>Welcome to LearnKode - A code learning platform</title>
</head>
  <body>
<p>The + operator concatenates (adds) strings.</p>
<p id="sample"></p>
<script>
var txt1 = "Michael";
var txt2 = "Voughan";
document.getElementById("sample").innerHTML = txt1 + " " + txt2;
</script>
      </body>
</html>

Output

Description :

+= string operator concatenate two strings


JavaScript String operators Example - 2
<!DOCTYPE html>
<html>
 <head>
 <title>Welcome to LearnKode - A code learning platform</title>
</head>
  <body>
<p>The assignment operator += can concatenate strings.</p>
<p id="sample"></p>
<script>
txt1 = "What a";
txt1 += " good day";
document.getElementById("sample").innerHTML = txt1;
</script>
      </body>
</html>

Output

Description :

String Operator "+" return 10, 55 and Hello5 based on values provided.


JavaScript String operators Example - 3
<!DOCTYPE html>
<html>
 <head>
 <title>Welcome to LearnKode - A code learning platform</title>
</head>
  <body>
<p id="sample"></p>

<script>
var x = 5 + 5;
var y = "5" + 5;
var z = "Hello" + 5;
document.getElementById("sample").innerHTML =
x + "<br>" + y + "<br>" + z;
</script>
      </body>
</html>

Output