Arithmetic operation
1. Add +
A, spell list
Concept: Manipulating different types of data and strings concatenates strings.
1, if the character is added, will “string”
result = "123" + "456";
console.log("result=" + result); // result=123456
console.log("Result type :" + typeof(result)); // Result type :string
Copy the code
2. Any type and character added will be converted to a string and then assembled
var result = "Hello" + 123; Result = "Hello" +"123"; // result = "Hello" +"123";
console.log("result=" + result); // result=Hello123
console.log("Result type :" + typeof(result)); // Result type :string
Copy the code
⚠️ Note: the result of the concatenation has nothing to do with the expression of the concatenation of the two types but rather with the expression of the string itself
🚀T- Tests whether the influence factor of the concatenation string result is related to the string form or concatenation form.
1) Change the stitching form
// There is no obvious space
result = "Hello" + 123;
// Clear space
result = "Hello" + 123;
/ / no Spaces
result = "Hello"+123;
Copy the code
The result is always result=Hello123
2) Change the string itself
result = "Hello " + 123;
Copy the code
Results the result = Hello 123
Based on this feature
So you can use this method to convert other data types to strings
result = "Hello" + "";
Copy the code
⚠️ Note: Order of addition operations, from left to right
2. The subtraction –
Except for the string in addition, all operations convert other data types to the Number data type
3. * multiplication
1. Ground rules
1, undefined to number equals NaN
var result = 2*undefined;
console.log("result=" + result); // result=NaN
console.log("Result type =" + typeof(result)); // Result type ==number
Copy the code
2. Null converts number to 0
result = 2 * null ;
console.log("result=" + result); // result=0
console.log("Result type =" + typeof(result)); // Result type =number
Copy the code
Any value that does the – * / operation becomes the number data type. You can use this feature to convert other data types to the number data type.
4. The division /
. slightly
(after)
The operator
1. Unary operators
– sign For non-number values, the addition of a – sign becomes a number value
If a =true ;
a = -a ;
console.log(typeof a); / / output number
consle.log( a ); / / output 1
Copy the code
You can use + on any other data type, where + is the unary operator, to turn it into the number data type, just like the number () function
2. Logical operators
1,! non
Concept: take the negative of a non-Boolean value, first change the non-Boolean value to a Boolean value type and then take the negative. You can convert any data type to a Boolean type, just as the Boolean () function does, but implicitly
2, && and
Concept: Return false whenever there is a false (both 1 and 0 are 1 and 0 are 0).
Understanding: As with love, as long as one person is not true, it is not true love, both are true love.
3, | | or
Concept: Return true as long as one of them is true(both 0 and 1 are 0)
Understanding: as affection, as long as there is a person to love you, all for affection
(after)
3. The non-Boolean or and operator
Method: convert to Boolean and then evaluate, and return the original value.
Two cases
1. && operation
If the first value is true, the following values are returned:
If the first value of 2&&3 is 2, it is true. According to the principle, if 1 is 1 and 0 is 0, the result can be judged only by looking at the last number in both cases.
a = 2&&3 / / 3
Copy the code
If the first is false, the first value is returned directly:
If the first value of 0&&3 is 0, it is false. According to the principle, if 1 is 1, any 0 is 0, regardless of whether the following value is true or false, the result is false, so you can judge the result by looking at the first value. This phenomenon is also known as “short-circuiting”.
a = 0&&2; / / 0
Copy the code
2. | | operation
If the first value is true, return the first value [short circuit]
If the first value is false, the second value is returned
Summary:
- Am& : the short-circuit value is false, so return the first value if the first value is false.
- | | operation: the value of the short-circuit is true, so the first value is true, it returns the first value
(after)
4. The assignment operator
Copy operations: + =, -=, *= /, / =, % =
5. Relational operators
Temporary slightly…