1. The operator
General classification:
Unary operatortypeof+ + - -! Binary operator + - * / % == >= <=! = > < | | && ternary operator? :Copy the code
Classification by function:
Arithmetic operators + - * / % logical operators && | |! Comparison operators (relationship) > < = = = = = > = assignment operator = + = - = * = an operator > > < < ~Copy the code
A numeric operation of type number
var num1 = 1;
var num2 = 2;
var num3 = num1 + num2;
console.log(num3);
var num4 = 15;
console.log(num4 % num2);
Copy the code
+ you can operate on strings in addition to operands and strings are concatenated
var str1 = "He";
var str2 = "llo";
console.log(str1 + str2);
Copy the code
Comparison operator
The result is a Boolean type
var num5 = 5;
var num6 = 6;
console.log(num6 > num5);
var num7 = 7;
// Error
// console.log(num7>num6>num5);
// Write it correctly
console.log(num7 > num6 && num7 > num5);
Copy the code
The assignment operator
Short form += -= /= *=
var sum=0;
sum+=2; // sum = sum+2;
Copy the code
Add and subtract ++ —
var num = 10;
console.log(num++); // assign first and then append
console.log(num);
console.log(++num);// add first and then assign
Copy the code
2. Ternary operators:
Conditions? The condition is established and executed: the condition is not established and executed
The assignment
var num = 7>3?7:3;
console.log(num);
Copy the code
Execute the statement
var b1= true?console.log(true) :console.log(false);
console.log(b1);
Copy the code
Prompt () gets user input
var num1 = prompt("Please enter a number.") *1; / / 10 01
num1>10? alert(num1):alert("0"+num1);
if(num1>10){
alert(num1);
}else{
alert("0"+num1);
}
Copy the code
Ternary operations are nested
var res=7>3?8>4?8:4:3;
console.log(res);
Copy the code
3. Operator priority:
Multiply and divide before addition and subtraction
var sum = 10 + 7 * 3 + 10;
console.log(sum);
Copy the code
After the first && | |
var res = 7 > 10 && 7 > 3 || 10 > 5 && 3 > 2;
console.log(res);
Copy the code
We can use () when we cannot know the exact priority
Operator priority formula (must remember)
The superlative () of the parenthesis members is the second in the list of all entries ++ --! Multiplication and division remaining three, plus or minus four * / % third + - fourth shift five, relation six >>> <<< shift > < >= <= relation is equal to or not equal to row seven ==! = = = =! = = logic eight, nine && assignment | | 7 = + = - = comma members, the lowest level of the same order of priorityCopy the code
thinking
How to calculate: ‘aa’ + 1 + 1-2 && 1-2 > 3 | | ‘aa + 2 > 3 | | (! 2) && 1 + +?
The calculation process is as follows:'aa'+1+1-2&&1-2>3||'aa'+2>3| | (!true) &&1++ // 2 --- bool
'aa'+1+1-2&&1-2>3||'aa'+2>3||false&&1++ / /! 2
'aa'+1+1-2&&1-2>3||'aa'+2>3||false&&2 / / 1 + +
'aa1'+1-2&&1-2>3||'aa'+2>3||false&&2 // 'aa'+1
'aa11'-2&&1-2>3||'aa'+2>3||false&&2 // 'aa1'+1
'aa11'-2&&1-2>3||'aa2'>3||false&&2 // 'aa'+2
NaN-2&&1-2>3||'aa2'>3||false&&2 //'aa11'-2
NaN &&1-2>3||'aa2'>3||false&&2 // NaN-2
NaN && -1 >3||'aa2'>3||false&&2 / / 1-2
false
Copy the code
The final result is: false;
The following are the steps to solve the problem:
'aa'+1+1
// 'aa1'+1
// 'aa11'
'aa'+1+3-2
// 'aa'+1+1
// 'aa11'
'aa'+1+3-2
// 'aa13'-2
// NaN - 2
// NaN
[1.2.3] - 1
// [1,2,3] --> '1,2,3' -1
/ / '1, 2, 3 - > NaN 1
[2] -1
/ / '2' - 1
/ / 2 to 1
/ / 1
Copy the code
Array object operations;
Implicitly to string and then to other types of operations
Equivalent to firing toString
var a = {
a:"123"
}
a+12;
// a --->'[object Object]' + 12
// 12---> '[object Object]' + '12'
//"[object Object]12"
Copy the code
Implicit transformation:
1: other types are transferred to number
bool undefined null string arrary object
true 1
false 0
undefined NaN
null 0
Copy the code
arrary object --> string --> number
All nans except pure numeric characters
2: other types are converted to strings
Put it in quotes' '
Copy the code
3: Other types go to Boolean
false : false 0 undefined null NaN ''
All others are true
(Operators are important to remember.)