Are both addition and concatenation same in JavaScript?

HTMLJavascriptProgramming Scripts

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>
raja
Published on 26-Feb-2018 09:42:10
Advertisements