Performing mathematical operations on numbers in JavaScript involves using fundamental operators such as:
- Addition Operator (+): This operator combines two numbers. For example:
- Subtraction Operator (-): This operator subtracts one number from another:
- Multiplication Operator (*): This operator multiplies two numbers:
- Division Operator (/): This operator divides one number by another:
- Remainder Operator (%): This operator gives the remainder of a division:
The JavaScript interpreter reads expressions from left to right. You can use parentheses to group and prioritize operations, just like in math: .
In JavaScript, the + operator is used for both addition and concatenation. When you use it with numbers, it adds them; with strings, it concatenates them.
The term NaN is a special value that indicates a result is "Not a Number". This happens when we try to do math with something that isn't a number, like a non-numeric string:
The method converts a value to a string and returns the first integer:
In JavaScript, if a calculation results in a number larger than the maximum possible value, it returns Infinity:
And if a calculation results in a number smaller than the minimum possible value, it returns -Infinity: