Organize class notes

Today's content is speaking operator

First, talking about the arithmetic operators, including + - * /% + -

++ - as a prefix, then, is to first add or subtract 1 again used; but as a suffix word is then used to subtract. Such as:

There num = 2;

var r = whether whether ++ +++ ++ + num;

console (r); where a first arithmetic process on the first use num 2, then plus 1 equals 3, plus 1 to the second num use, the second one is 4 num, num is the last one to use it is 4, the value of r is 10.

Then the computer calculates an error may occur, then, it can use variable = variable .toFixed (2); the code to be rounded, to set the values ​​in parentheses retained several decimal places.

% This symbol is the modulus, that is, take the remainder, then we can use this symbol to determine parity. Such as

var num = prompt ( "Enter a number");

consolo.log ( "even number?" + num% 2);

If the value is out of the other is an even number 0, 1 is an odd number.

Followed by relational operators

<  >  <=  >= == != === !==

Are greater than, less than or less than or equal to equal to not equal to the value and type of completely equal value and the type completely equal, most of these operators are used to determine the Boolean type.

size and number string is determined, when the character string type implicitly converted number is used for this transformation Number () function, so there will be a non-numeric characters to be NaN.

Any data when compared to NaN, the result necessarily false.

Analyzing between the string and comparing it with the unicode string code size of each character. Other words are relatively simple and do not bore you, you should pay attention to the above points.

Logical Operators

The logical operators && ||! (AND, OR, NOT)

&& is the two should meet

And a short circuit; a first long as the condition is false, then the entire expression are false, and the second condition is determined not to

|| is to meet on a trip

Short circuit or: as long as the first condition is true, the entire expression are true, and not to judge the second condition

! This is generally the case with the isNaN used because isNaN illogical sequence of normal, so it in turn.

Bitwise Operators

& Bitwise AND | Bitwise OR

You can only do a digital arithmetic, and want to convert binary numbers, making computing
& bitwise and the two sides becomes operand converting binary numbers each, as long as the corresponding time are 1, then the result of this bit is 1, otherwise the result bit 0

| Bitwise or both sides of the operand converted into binary numbers on each compare the two figures is a 1, then the result is 1 bit, otherwise 0

It can be used normally &

Determining a parity, to be higher than the efficiency of the mold
is determined with a digital bitwise done, the last one is even 0 1 odd

| 0 for the bit arithmetic rounding constant converts the data into binary numbers, decimals are converted to an integer.

<< Bitwise left shift

, A binary number, several moves to the left, to the right of 0 bits fill
the console.log (2 <<. 1); //. 4
the console.log (2 << 2); //. 8
the console.log (2. 3 << ); // 16
the console.log (<< 2. 4); // 32
// 0000 0000 0001 0000, that is, (a << n); // a-th power of n + 1;

>> bit binary number to the right to move rightward several, to the left 0 bits fill
the console.log (>>. 8. 1); //. 4
the console.log (>> 2. 8); // 2
the console.log (>>. 8. 3); //. 1
the console.log (>>. 8. 4); // 0

This (a >> n) // n-th power is a / 2, the result is less than 0 then 0.

 ^ Exclusive or both sides of the converted digital binary comparison, the figures for each, only one is 1, the result was that the bit is 1, otherwise 0

This can be used to exchange the values ​​of two variables

Num1 and num2 exchange value of
// ^ num1 = num1 num2;
num1 num2 = ^;
// ^ num1 num2 = num2;
num2 ^ = num1;
// ^ num1 = num1 num2;
num1 num2 = ^;

Assignment Operators

+=  -=  *=  /=  %=  &=  |=  ^= 

a + = b is equivalent to a = a + b, other empathy

  Unary operator

 ++ -!
 Binary operators
 + - * /% && || ^ & |> <
 ternary operator
requires three operands / Expression Expression 1 Expression 2: Expression 3;?
 Expression1: boolean operation result should be type
the expression 1 = true expression is executed 2
 expression 1 = false executing the expression 3
allows nested
var age = prompt ( "Please enter your age:");
if age> = 18 outputs, allowing a driver's license or output of age yet to come

var result = age> = 18 "allows the driver's license test":? "age yet to come";
console.log (the Result); * /


var a=100,b=12;
var max = a > b ? a : b;
console.log(max);

Judgment results, if the score> = 80 outstanding

> = 60 passing
<60 failed * /

var score = prompt ( "Enter the grade:");
var msg = Score> = 80 "excellent": score> = 60 "qualified": "fail";??
console.log (msg);

 

Guess you like

Origin www.cnblogs.com/yzxyzx/p/11228545.html