Add Two Numbers with JavaScript:
Number is a data type in JavaScript which represents numeric data.
Now let's try to add two numbers using JavaScript.
JavaScript uses the + symbol as an addition operator when placed between two numbers.
Example:
const myVar = 5 + 10;
myVar now has the value 15.
Subtract One Number from Another with JavaScript:
We can also subtract one number from another.
JavaScript uses the - symbol for subtraction.
Example
const myVar = 12 - 6;
myVar would have the value 6.
Multiply Two Numbers with JavaScript:
We can also multiply one number by another.
JavaScript uses the * symbol for multiplication of two numbers.
Example:
const myVar = 13 * 13;
myVar would have the value 169.
Divide One Number by Another with JavaScript:
We can also divide one number by another.
JavaScript uses the / symbol for division.
Example:
const myVar = 16 / 2;
myVar now has the value 8.
Increment a Number with JavaScript:
You can easily increment or add one to a variable with the ++ operator.
i++;
is the equivalent of
i = i + 1;
Note: The entire line becomes i++;, eliminating the need for the equal sign.
Decrement a Number with JavaScript:
You can easily decrement or decrease a variable by one with the -- operator.
i--;
is the equivalent of
i = i - 1;
Note: The entire line becomes i--;, eliminating the need for the equal sign.
We can store decimal numbers in variables too. Decimal numbers are sometimes referred to as floating point numbers or floats.
Not all real numbers can accurately be represented in floating point. This can lead to rounding errors.
Finding a Remainder in JavaScript:
The remainder operator % gives the remainder of the division of two numbers.
Example:
5 % 2 = 1 because
Math.floor(5 / 2) = 2 (Quotient)
2 * 2 = 4
5 - 4 = 1 (Remainder)
Usage
In mathematics, a number can be checked to be even or odd by checking the remainder of the division of the number by 2.
17 % 2 = 1 (17 is Odd)
48 % 2 = 0 (48 is Even)
Note: The remainder operator is sometimes incorrectly referred to as the modulus operator. It is very similar to modulus, but does not work properly with negative numbers.
No comments:
Post a Comment