JS operators - arithmetic operators, logic operators, assignment operators, relational operators, conditional operator

1, the basic concept of the operator

  • Operator is also called operator
  • It can operate on one or more values ​​by the operator and acquires the operation result
  • For example: typeof operator is, a value may be obtained of the type
  • The type of the value it returns a string
  • number string boolean undefined object

2, arithmetic operators

  • Arithmetic operators
  • When the value of type Number of non calculates, will then convert these values ​​to the arithmetic Number, and NaN values ​​do any operation had NaN
  • + (Plus sign):
  • You may be adding two values ​​and returns the result
  • If the addition operation of two strings, the string will fight will make two strings together into a string and returns
  • Any string value and do adder, are first converted to a string, then the string and string operations do fight
  • - (minus): may be subtraction of two values ​​and returns the result
  • * (Multiplication): multiplication may be two values
  • / (Division number): may be division of the two values
  • % (Remainder): a modulo operation (take the remainder)

2.1 Plus

var a = 123;		
var result = typeof a;
result = a + 1;  // 拼串
result = 456 + 789;  //数值的加法运算
result = true + 1;  // 先对true进行数据转换,然后再进行加法运算
result = true + false;  // 先对true和false都进行数据转换,然后再进行加法运算
result = 2 + null;
result = 2 + NaN;
result = "你好" + "大帅哥";  //拼串
var str = "锄禾日当午," +
          "汗滴禾下土," +
          "谁知盘中餐," +
          "粒粒皆辛苦";
result = 123 + "1";  // 拼串
result = true + "hello";  // 拼串

Minus 2.2

result = 100 - 5;
result = 100 - true; // 先对true进行数据转换,然后再进行加法运算
result = 100 - "1";  // 99  先对字符串“1”进行数据转换,然后进行运算

2.3 multiplication sign

result = 2 * 2;  // 4
result = 2 * "8"; // 16
result = 2 * undefined; // 0
result = 2 * null; // 0

2.4 Divide

result = 2 / 2;  // 1
result = 2 / "8"; // 0.25
  • to sum up:
  • Do any value - * / when the operation will be automatically converted to Number
    • We can use this feature to do implicit type conversion
    • A by-0 * 1/1 to convert it to Number
    • Principles and Number () function, like, easier to use

2.5 modulo (remainder number)

result = 9 % 3;  // 0
result = 9 % 4;  // 1
result = 9 % true;  // 0
result = 9 % "5";  // 4

Third, unary operators

  • Unary operators, only one operand
  • Positive sign will not have any effect on digital
  • Minus a negative sign can be negated digital
  • For the type of value of the non Number, it will be converted to Number, then in operation;
  • May be used for a different data type +, to convert it to Number, the same principles and its Number () function.
var a = 123;
a = -a;
a = true;  // 1
a = "18";  // 18
a = +a;  // 可以将true及字符串“18”转换为对应的数值
console.log(typeof a);
var result = 1 + +"2" + 3;

Fourth, the increment decrement

  • Increment ++
    • Can increase the variable incremented by 1 in its own basis
    • For subsequent increment a variable, the value of the original variable is incremented immediately 1
    • Increment divided into two: after ++ (a ++) and a front ++ (++ a)
    • Whether it is a ++ or ++ a, will cause the immediate value of the original variable is incremented by one
  • And except that a ++ a ++ a different value
    • A ++ a value equal to the value of the original variable (value before increment)
    • ++ a value equal to the new value (from the increased value)
  • Decrement -
    • It can be made variable by itself from Save Save 1 on the basis of
    • After - (a-) and a front - (-a): decremented divided into two
    • Whether a-- or --a will cause the immediate value of the original variable from minus 1
  • The difference is that different values ​​of a-- and --a
    • a-- variable is the original value (the value before decrementing)
    • -a variable is the new value (the value after decrement)
var num = 10;		
console.log(num--);  // 10
console.log(num--); // 9

var num = 10;		
console.log(--num);	  // 9

console.log("num = "+num);

Five logical operators

  • JS provides us with three kinds of logical operators, three operators can do the following three things:

    • For boolean types of data operations directly;
    • With non-Boolean types will first convert it to a Boolean value, then operation, and returns the original value;
    • For JS statement , the first statement JS determination execution result, and then determines whether to perform the second based on the results.
  • ! NOT operation

    • ! Value can be used for a non-operational
    • Value calculation is called a non- Boolean value negate operation , true becomes false, false becomes true
    • If a value of twice negated, it will not change
    • If the elements of the non-Boolean value, then converts it to a Boolean value, and then negated
    • So we can use this feature to convert to a different data type to a Boolean value;
    • May be taken twice a trans to any data type, to be converted to a Boolean value, principle and Boolean () function as
  • && AND operation

    • && can symbol values ​​on both sides of the operation and return the result
    • Operation rule : two values as long as there is a false value returns false,
    • Only when both values are true, will return true, the JS in "and" belonging to the short-circuit operation and , if the first value is false, will not see the second value.
  • || OR

    • || operators can sign on both sides of the value and returns the result ORed
    • ** operation rule: ** two values ​​as long as there is a true, returns true
    • If the two values ​​are false, it returns false, the JS in "or" belonging or short, if the first value is true, the second value is not checked.

5.1! NOT operation (other types of data can be transferred Boolean)

var a = false;
a = !a; //对a进行非运算
//console.log("a = "+a);
var b = 10;
b = !!b; //对B进行数据类型转换
//如果两个值都是true则返回true
var result = true && true;
//只要有一个false,就返回false
result = true && false;  //第一个值为true,会检查第二个值
result = false && true;  //第一个值为false,不会检查第二个值
result = false && false;

5.3 || OR (encountered condition is true, termination)

//两个都是false,则返回false
result = false || false;
//只有有一个true,就返回true
result = false || true ; //第一个值为false,则会检查第二个值
result = true || false;  //第一个值为true,则不再检查第二个值
result = true || true ;

5.4 Non-Boolean value

result = 0 && 2;  // 0
result = 2 && 0;   // 2
//false && false
result = NaN && 0;  // NAN
result = 0 && NaN;  // 0

//如果第一个值为true,则直接返回第一个值
result = 2 || NaN;  // 2
result = 2 || 0;  // 2
//如果第一个值为false,则直接返回第二个值
result = NaN || 1;  // NAN
result = NaN || 0;  // NAN

Sixth, the assignment operator

  • = (Equal sign) // right value symbols can be assigned to the left of the Symbol
  • + = (Equal sign) // a + = 5 is equivalent to a = a + 5
  • - = (equal sign minus) // a - = 5 is equivalent to a = a - 5
  • * = (Equal sign multiplication) // a * = 5 is equivalent to a = a * 5
  • / = (Equal sign other) // a / = 5 is equivalent to a = a / 5
  • % = (Equal sign Remainder) // a% = 5 is equivalent to a = a% 5

Seven, relational operators

  • You may compare the size relationship between the two values by relational operators, if the relation holds it returns true, if the relationship is not established it returns false.
  • > Greater than sign
    • Determining whether the value is greater than the value of the symbol to the left of the right
    • If the relationship is established, returns true, if the relationship is not established returns false
  • > = Greater than or equal
    • Determining whether the value is greater than or equal symbol to the left of the right value
  • <Less
  • <= Less than or equal
  • Non-value
    • When compared for non-numeric, then converts it to a digital comparison
    • If both values ​​are strings of symbols, it will not be converted to a digital comparator, the comparator will encode each Unicode character strings;
    • Can use the charCodeAt () method to query the corresponding Unicode encoding.
console.log(1 > true); //false
console.log(1 >= true); //true
console.log(1 > "0"); //true
console.log(10 > null); //true
//任何值和NaN做任何比较都是false
console.log(10 <= "hello"); //false
console.log(true > false); //true
console.log("1" < "5"); //true

Eight, equality operators

  • == equality operators: equality operator to compare the two values ​​are equal,
    • The same data type and equal returns true, otherwise returns false;
    • If a different type of the value, type conversion is automatically performed, to convert it to the same type, then the comparison.
  • ! = Not equal: unequal used to determine whether two values ​​are not equal Returns true if not equal, false otherwise
    • Use! = Not equal operator do
    • Will not equal the variable automatic type conversions, the conversion is equal to if it will return false
  • === congruent (can be used for the type judgment)
    • Used to determine whether two congruent values ​​that are equal and similar, except that it does not do automatic type conversions, if the two values ​​of different type, directly returns false.
  • ! == strict inequality (can be used to make the type of judgment)
    • Used to determine whether the two values ​​and incomplete, unequal, and the like, except that it does not do automatic type conversions, if the two values ​​of different type, directly returns true.
console.log(10 != 5);   //true
console.log(10 != 10);   //false
console.log("abcd" != "abcd");   //false
console.log("1" != 1);  //false
console.log("123" === 123);  //false
console.log(null === undefined);   //false
console.log(1 !== "1");   //true

** Note: ** can isNaN () function to determine whether the value is a NaN, NaN if the value is true is returned, false otherwise

IX conditional operator

  • Conditional operator is also called ternary operator
  • grammar:
    • ? Conditional expressions Statement 1: Statement 2;
  • Execution of the process:
    • Conditional operator, when executed, first, the conditional expression is evaluated, if the value is true, execute a statement, and returns the result. If the value is false, the statement 2 is executed, and returns the result.
    • If the condition expression evaluates to a non-Boolean value, which will then be converted to Boolean operations.

Syntax example:

false?alert("语句1"):alert("语句2");
var a = 300;
var b = 143;
var c = 50;
a > b ? alert("a大"):alert("b大");
// 获取a和b中的最大值
var max = a > b ? a : b;
// 获取a b c 中的大值
max = max > c ? max : c;
false?alert("语句1"):alert("语句2");
var a = 300;
var b = 143;
var c = 50;
a > b ? alert("a大"):alert("b大");
// 获取a和b中的最大值
var max = a > b ? a : b;
// 获取a b c 中的大值
max = max > c ? max : c;
Published 20 original articles · won praise 11 · views 1736

Guess you like

Origin blog.csdn.net/qq_16221009/article/details/103989947