JavaScript Arithmetic

As with algebra, you can do arithmetic with JavaScript variables, using operators such as = and +:
   var x = 5 + 2 + 3; (value of x is 10)

You can also add strings, but strings will be concatenated (added end-to-end):
   var x = "John" + " " + "Doe"; (value of x is John Doe)

As shown in the example below, if you add a number to a string, the number will be treated as string, and concatenated:
   var x = "5" + 2 + 3; (value of x is 523)

HTML file: Displayed by browser:
<body>
<h1>JavaScript Variables</h1>
<p>Add "5" + 2 + 3. and display the result:</p>
<p id="demo"></p>
<script>
   var x = "5" + 2 + 3;
   document.getElementById("demo").innerHTML = x;
</script>
</body>

JavaScript Variables

Add "5" + 2 + 3. and display the result:


BackTable of ContentsNext