Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
What is Addition Operator (+) in JavaScript?
The addition operator (+) in JavaScript is used to add two numeric operands together. It performs mathematical addition and returns the sum of the values.
Syntax
result = operand1 + operand2;
Example
You can try to run the following code to work with the addition operator:
<html>
<body>
<script>
var a = 33;
var b = 10;
document.write("a + b = ");
result = a + b;
document.write(result);
</script>
</body>
</html>
a + b = 43
String Concatenation vs Numeric Addition
The + operator also performs string concatenation when used with strings:
<html>
<body>
<script>
var num1 = 5;
var num2 = 10;
var str1 = "Hello ";
var str2 = "World";
document.write("Numeric addition: " + (num1 + num2) + "<br>");
document.write("String concatenation: " + str1 + str2);
</script>
</body>
</html>
Numeric addition: 15 String concatenation: Hello World
Key Points
- With numbers: performs mathematical addition
- With strings: performs concatenation
- Mixed types: converts numbers to strings and concatenates
- Returns the sum for numeric operations
Conclusion
The addition operator (+) is fundamental in JavaScript for both numeric addition and string concatenation. Understanding its dual behavior with different data types is essential for effective JavaScript programming.
Advertisements
