Addition and Subtraction Operators
What you'll learn: How to use + and - to perform basic arithmetic on numbers in JavaScript.
The Core Idea
The addition (+) and subtraction (-) operators work just like they do in everyday math. They perform arithmetic on numbers, whether those numbers are whole integers or decimals (floating-point numbers).
Think of these operators as the most fundamental tools in your math toolkit—they're exactly what you'd expect from a calculator.
Using Addition (+)
The + operator adds two numbers together:
let sum = 5 + 3; // 8
let total = 10.5 + 2.3; // 12.8
let negative = -7 + 4; // -3
It works with integers, decimals, and negative numbers. The result matches what you'd calculate by hand.
Using Subtraction (-)
The - operator subtracts the right number from the left:
let difference = 10 - 4; // 6
let decimal = 7.5 - 2.1; // 5.4
let goNegative = 3 - 8; // -5
Order matters! 10 - 4 gives you 6, but 4 - 10 gives you -6.
Mixing Integers and Decimals
JavaScript seamlessly handles operations between integers and floating-point numbers:
let mixed = 5 + 2.5; // 7.5
let another = 10.75 - 3; // 7.75
You don't need to worry about data types—JavaScript automatically handles the math.
Multiple Operations
You can chain these operators together:
let result = 10 + 5 - 3; // 12
let calc = 2.5 + 1.5 - 0.5; // 3.5
Operations happen left-to-right when operators have the same precedence.
Key Takeaway: The + and - operators perform straightforward addition and subtraction on any numbers—integers, decimals, positive, or negative—just like basic arithmetic.