xTable of Contents [1] unary plus [2] unary minus [3] increment [4] decrement [5] addition [6] subtraction [7] multiplication [8] division [9] seeking the remainder of the words
Arithmetic operations in JavaScript are primarily implemented by arithmetic operators, and this article describes the contents of arithmetic operators. Arithmetic operators include unary arithmetic operators and two-tuple arithmetic operators
Unary arithmetic operators
The unary arithmetic operator is used for a single operand and produces a new value. In JavaScript, unary operators have high precedence and are all right-associative (right-associative)
Unary arithmetic operators include unary addition (+), unary subtraction (-), increment (+ +), and decrement (--)
Unary Plus (+)
The unary plus operator is represented by a plus sign (+), preceded by a number, and has no effect on the value
var num = 25;
num = + num; // 25
When applying the unary plus operator to non-numeric values, the Number () transformation function is called to convert the value
var s1 = ‘01’;
var s2 = ‘1.1’;
var s3 = ‘z’;
var b = false;
var f = 1.1;
var o = {
valueOf: function () {
return -1;
}
};
s1 = + s1; // 1
s2 = + s2; // 1.1
s3 = + s3; // NaN
b = + b; // 0
f = + f; // 1.1
o = + o; //-1
Unary minus (-)
Unary minus operator is mainly used to represent negative numbers
var num = 25;
num = -num; //-25
When the unary minus operator is used for non-numeric values, it will use the Number () conversion function to convert the value, and then convert the resulting value to a negative number.
var s1 = ‘01’;
var s2 = ‘1.1’;
var s3 = ‘z’;
var b = false;
var f = 1.1;
var o = {
valueOf: function () {
return -1;
}
};
s1 = -s1; //-1
s2 = -s2; //-1.1
s3 = -s3; // NaN
b = -b; // 0
f = -f; //-1.1
o = -o; // 1
[Note] The unary addition and unary subtraction operators are mainly used for basic arithmetic operations and can also be used to convert data types
Increment (++)
Increment ++ operator increments (adds 1) the operand, and the operand is an lvalue (variable, array element, or object attribute). The operator converts the operand to a number through the Number () transformation function, then adds 1 to the number, and reassigns the value after the addition to the variable, numeric element, or object attribute
var age = 29;
++ age;
// equivalent to
var age = 29;
age = age +1;
The return value of the increment ++ operator depends on its position relative to the operand. When the operator is before the operand, it is called a pre-increment operator, which performs incremental calculation on the operand and returns the calculated value. When the operator is after the operand, it is called the post-increment operator. It performs incremental calculation on the operand, but returns the unincremented value.
Whether it is a pre-increment or a post-increment, this operator is usually used in a for loop to control the counter in the loop
var i = 1, j = ++ i; // i = 2 j = 2
var i = 1, j = i ++; // i = 2 j = 1
[Note] ++ x is not always exactly the same as x = x + 1, the ++ operator never performs string concatenation, it always converts the operand to a number and increments it by 1.
var x = ‘1’;
++ x; // 2
var x = ‘1’;
x = x + 1; // ‘11’
Decreasing (-)
Decrement--The operand of the operator is also an lvalue. It converts the operand to a number through the Number () transformation function, then subtracts 1, and reassigns the calculated value to the operand
Like the increment ++ operator, the decrement-the return value of the operator depends on its position relative to the operand. When the decrement operator is before the operand, the operand decrements by 1 and returns the value after decrementing by 1. When the decrement operator is after the operand, the operand decrements by 1 and returns the value before decrementing by 1.
var age = 29;
--age;
// equivalent to
var age = 29;
age = age-1;
The pre-incremental operator has the same priority as the executed statement, and the entire statement will be evaluated from left to right
var num1 = 2;
var num2 = 20;
var num3 = --num1 + num2; // 21
var num4 = num1 + num2; // 21
Post-incremental operators are only executed after the statement containing them is evaluated
var num1 = 2;
var num2 = 20;
var num3 = num1-- + num2; // 22
var num4 = num1 + num2; // 21
When involving the ++ and-operators, if it is used as a suffix expression, it should be on the same line as the expression. Otherwise, the end of the line will be filled with a semicolon, and ++ or-will be used as the prefix operator of the next line of code and parsed with it
x
-
y
Javascript parses it as:
x;-y;
And the original meaning of the code is:
x-; y;
Binary arithmetic operator
Binary arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/) and remainder (%)
Addition (+)
In most programming languages, addition is usually a simple numeric operator, but in ECMAScript, addition has a large number of special behaviors, not only for numerical addition, but also for string concatenation
Addition operation follows the following rules:
[1] If one of the operands is an object, the object is converted to the original value: the date object is converted by the toString () method, and the other objects are converted by the valueOf () method. Since most object valueOf () methods cannot return a primitive value, the conversion will be performed by toString () method
[Note] In addition to single-value arrays will be converted to numbers, other native objects will be converted to string form through toString () method
【2】 After the conversion of the object to the original value, if one of the operands is a string, the other operand will also be converted into a string, and the string is concatenated
[3] Otherwise, both operands will be converted to numbers or NaN for addition operation
// Single value array and valueOf () return value is a custom object that is converted to a numeric value
console.log (1 + []); // 1
var o = {
valueOf: function () {
return -1;
}
}
console.log (1 + o); // 0
// Other native objects are converted to strings
console.log (1 + {}); // ‘1 [object Object]‘
console.log (1 + [1,2]); // ‘11,2‘
console.log (1 + new Date ()); // ‘1Thu Jun 16 2016 10:27:13 GMT + 0800 (China Standard Time)’
console.log (1 + / 0 /); // ‘1/1/0 /‘
If arithmetic addition is performed, undefined is converted to NaN, null is converted to 0, false is converted to 0, true is converted to 1
console.log (undefined + undefined); // NaN
console.log (null + null); // 0
console.log (false + false); // 0
console.log (true + true); // 2
If string concatenation is performed, undefined is converted to ‘undefined’, null is converted to ‘null’, false is converted to ‘false’, and true is converted to ‘true’
console.log (‘‘ + undefined); // ‘undefined’
console.log (‘‘ + null); // ‘null‘
console.log (‘‘ + false); // ‘false’
console.log (‘‘ + true); // ‘true’
Therefore, using the characteristics of the plus operator, you can use ‘‘ + any type value to convert to a string
In the digital addition operation, the processing of Infinity and signed 0 is more special
Number.MAX_VALUE + Number.MAX_VALUE === Infinity; // true
Infinity + (-Infinity); // NaN
+0 + (-0) === +0; // true
Subtraction (-)
Compared to addition, subtraction is much simpler, and only involves the subtraction of numbers. Use the Number () conversion function to convert non-numeric types to numeric or NaN
console.log (1-{}); // NaN
console.log (1-[1,2]); // NaN
console.log (1-/ 0 /); // NaN
console.log (1-[]); // 1
There is a special addition to addition, when the Date object is used for addition operations toString () to convert to a string, and in other mathematical operations, including subtraction, multiplication, division, remainder and other operations, are used to convert Number () The function converts the time Date object to a number using valueOf ()
console.log (new Date () + 1); // ‘Thu Jun 16 2016 11:11:49 GMT + 0800 (China Standard Time) 1’
console.log (new Date ()-1); // 1466046941641
Undefined converted to NaN, null converted to 0, false converted to 0, true converted to 1
console.log (1-undefined); // NaN
console.log (1-null); // 1
console.log (1-false); // 1
console.log (1-true); // 0
multiplication(*)
The multiplication operator is represented by an asterisk (*), which is used to calculate the product of two numeric values. Non-numeric types are converted to numeric or NaN through the Number () conversion function
+ Infinity * 0; // NaN
-Infinity * 0; // NaN
Infinity * Non-zero value; // Infinity or -Infinity
Infinity * Infinity; // Infinity
division(/)
The division operator is represented by a slash (/), performing the operation of dividing the first operand by the second operand will also convert the non-numeric type to numeric or NaN through the Number () conversion function
Infinity / Infinity; // NaN
0/0; // NaN
Non-zero value / 0; // Infinity or -Infinity
Infinity / 0; // Infinity
Infinity / non-zero value; // Infinity
Modulus (%)
The modulo (remainder) operator is represented by a percent sign (%), which is the remainder of the first operand divided by the second operand
// r is the remainder, n is the dividend, d is the divisor,
// q is an integer, which is negative when n / d is negative and positive when n / d is positive, it should be as large as possible without exceeding the quotient of n and d
r = n-(d * q)
Modulus result is consistent with the sign of the first operand
console.log (5% 2); // 1
console.log (5% -2); // 1
console.log (-5% 2); //-1
console.log (-5% -2); //-1
The dividend is Infinity, or the divisor is 0, then the result of the modulo is NaN
Infinity% 0 = NaN
Infinity% Infinity = NaN
Infinity% non-zero value = NaN
Non-zero value% 0 = NaN
In most programming languages, the modulo operator only accepts integers as operands, and in ECMAScript, it also accepts floating-point operands, but because floating-point numbers are not exact values, it is not possible to get completely accurate results
console.log (6.5% 2.1); // 0.19999999999999973
References
[1] ES5 / Expression https://www.w3.org/html/ig/zh/wiki/ES5/expressions
[2] Ruan Yifeng Javascript Standard Reference Course-Operators of Basic Grammar http://javascript.ruanyifeng.com/grammar/operator.html
[3] W3School-Javascript Advanced Tutorial-ECMAScript Operator http://www.w3school.com.cn/js/pro_js_operators_multiplicative.asp
[4] Chapter 4 Expressions and Operators in the "Authoritative Guide to JavaScript (6th Edition)"
[5] Chapter 3 Basic Concepts of "Javascript Advanced Programming (3rd Edition)"
[6] Chapter 2 of "The Essence of JavaScript Language (Revised Edition)" Grammar
javascript operator-arithmetic operator