Computer >> Computer tutorials >  >> Programming >> Javascript

Are both addition and concatenation same in JavaScript?


Both addition and concatenation use the same + operator, but they are not same. Concatenation is used to concatenate i.e. add strings, whereas simple addition adds numbers.

Example

Let us see an example showing adding numbers and adding strings. Both show different results −

<!DOCTYPE html>
<html>
   <body>
      <script>
         var val1 = 30 + "25";
         document.write(val1+"<br>");
         
         var val2 = 30 + 25;
         document.write(val2);
      </script>
   </body>
</html>