JavaScript operators and expressions

JavaScript operators and expressions


 What is the operator

  - means for assigning, comparison, arithmetic operation of performing symbol.

 What is the expression

   - statement by the operator, variables, and data thereof.

Arithmetic operators

  Arithmetic operators include: + - * /% (Remainder / modulo operation / take the remainder) + -    

<script>

    // 运算符 +  有字符串参与就变成字符串拼接
    var n1 = 3, n2 = "5";
    console.log(n1 + n2);  // 35
    console.log(n1 + parseInt(n2));  // 8

    // %取余,模运算  取余数
    console.log(5 % 2);  // 1
    console.log(12345 % 100);  // 45
    console.log(60 % 3);  // 0

    // 取整
    console.log(parseInt(5 / 2));

    // ++ 递,当前数值+1,   -- 递减,当前数字-1
    var a = 1;
    a++;  // a=a+1
    console.log(a);  // 2
    a++;  // a=a-1
    console.log(a)  // 1

    ++a;
    console.log(a);  // 2
    --a;
    console.log(a);  // 1

    //  ++ 和 -- 都可以在变量前或变量后,都为变量+1或-1.
    // 但是,表达式的返回值不同。
    // a++ ,a增一,表达式返回+1前的旧值。
    // ++a ,a增一,表达式返回+1后的新值。
    a = 0;
    var n = a++;
    console.log(a,n); // 1,0
    a = 0;
    var m = ++a ;
    console.log(a,m); // 1,1

    var b = 1;
    console.log(b++);  // 1  输出 +1 的新值
    console.log(++b);  // 3

    /*
    * 1. 所有表达式都是从左往右执行
    * 2.如果前一个表达式修改了变量,则会影响后续的表达式。
    */

    var n =5;
    console.log(n++ + ++n + n++);  //  19
        // n 的的值:   6  7  8
        // 表达式的值: 5  7   7
    console.log(++n + n++ + ++n);  // 29
        // n的值      9    10   11
        // 表达式的值  9    9    11

</script>

Assignment Operators

  Assignment operator comprising: a + = = - = = * / =% =

    =: Means for assigning a variable, the variable name = variable value;

    + =: N + = 3 corresponds to n = n + 3.

    - =: n- = 3 corresponds to n = n-3.

    * =: N * = 3 corresponds to n = n * 3.

    / =: N / = 3 corresponds to n = n / 3.

    % =: N% = 3 corresponds to n = n% 3.

 

Case Code

var a = 1, b = 2;
// a=a+b
a+=b;  //3
console.log(a);

var c = 3 ,d = 4;
c*=d;  //  c = c*d;
console.log(c)


var e = 5 , f = 6;
// e = e%f
e%=f;
console.log(e) // 5

var g= 7;
// g = g+1;
// g+=1;
g++;
console.log(g);

var str ="abc";
str =str +"def";  // "abcdef"
console.log(str);
str += "def"  //  "abcdefdef"
console.log(str);

    

 

Relational Operators

   Calculating a relationship between the magnitude relation comparison data.

   The relational operators there are about eight:> <> = <= == = == ===!!

  The relationship between the expression of the return value is a Boolean: true / false.

 

Code Cases

// 关系运算
var n1 = 10, n2 = 20, n3 = 20;
console.log(
    n1 < n2, // true
    n2 >= n3, // true
    n2 == n3, // true
    n2 != n3, // false
);
  There is an implicit conversion in relation operation, the default involved in everything to digital operation
// 在关系运算中存在隐式转换,默认一切转为数字在参与运算。
var n = 2, s = "3", b = true;
console.log(
    s > n, // true
    b < s, // true
    s - n == b, // true
    s >= b, // true
    s - n >= b, // true
);

  Compare two strings: a special case. Not converted to digital, but compared for each character Unicode number of large small.

console.log(
    "12" > "3", // "1" VS "3"  --> false
    "eric" < "jerry", // "e" VS "j"  --> true
    "scott" > "smith" , //  "c"  VS "m"  --> false
    "123" > "12" //  当前面字符一样大,比个数。 true
)

  Special Case 2: null and undefined

var n;  // undefined
var m = null;
console.log(
    n == null, // false
    m == undefined,  // true
    null == undefined   // true
);

  === congruent, first compare the data type is the same, then the comparison value is the same, equivalent without == implicit conversion.

    Examples of top pick

var n;  // undefined
var m = null;
console.log(
    n === null, // false
    m === undefined,  // false
    null === undefined,   // false
    "3" == 3, // true
    "3" === 3, // false
    "1" == true, // true
    "1" === true, // false
);

   NaN special cases not more than three, not less, not equal to any value.

    - NaN means "not a number", not a number two can not compare the value is false

var a = NaN;
console.log(
    a == NaN,  // false
    a > NaN,  // false
    a < NaN,  // false

    a != NaN,// true
    2 != NaN,  // true
    "abc" != NaN // true
);

  Use isNaN (num) to determine whether num is a number

    - If num is not or can not be converted to a digital value, it returns true.

    - If num is the number or may be converted to the implicit value, it returns false.

console.log(
    isNaN("abc"),  // true
    isNaN(3),  // false
    isNaN("3"),  // false
);

   - Traditionally, we use the opposite of  isNaN (NUM)!
     - If true numbers, but not for the false numbers.

console.log(
    !isNaN("abc"),  // false
    !isNaN(3),  // true
    !isNaN("3"),  // true
);

 logic operation

   Logic operation is the result of a comprehensive multiple expressions come to a final conclusion.

   There are three logical operators:

    - && (and / and)

    - || (or / or)

    -! (Non / no)

 

  Logical AND && 

    Logic and when all conditions are met is to return true, otherwise false.

         & & & & Cheap high-volume a good evaluation
           to true to true to true ==> to true
           to true to true false ==> false

 

  Logical OR || 

    Or logic, as long as it satisfies the condition returns true, all the conditions are not met returns false.

      距离   ||   价格
      true       true   ==> true
      true      false  ==>    true
        false     false     ==>    false

 

  Logical NOT!

    Non-logic, if the reverse is true return false results, if it is false returns true.

      !true==> false

      !false ==> true    

      !isNaN() ==> isNaN()

 

  Case Code

var n = 10, m = 20;
console.log(

    // 逻辑与
    n > 5 && n < 20,// true
    n > 5 && n < 8, // false
    n > 5 && n < m && n < 10, // true && ture && false ==> false

    // 逻辑或
    n < m || n > 20,  // true || false ==> true
    n > 5 || n < 8,  // true || false ==> true
    n > 5 || n < m || n < 10,  // true

    // 逻辑非
    !(n + m) == 30, // !true ==> false

);

Operator precedence

  The following table all the operators are arranged in descending order of priority different.

     

 Short-circuit logic

  If the previous condition can already draw a final conclusion, then the following conditions are not executed.

 

    -   &&  If the previous condition is true, then the following conditions will continue.

    - If the previous condition is false, then the following condition is not performed, the results of direct return false.

 

    -    ||    If the previous condition is true, then the following condition is not performed, directly returns true.

    - If the previous condition is false, then the following conditions continue.

 

Short-circuit logic Case Code

var n = 10;

console.log(
    n >= 10 && m == 2 // true && 未声明报错   ==> 未声明报错
);
console.log(
    n >= 10 || m == 2 // true && 未声明报错 (但未执行)  ==> true
);
console.log(
    n < 10 && m == 2 // false && 未声明报错(但未执行)   ==> false
);
console.log(
    n < 10 || m == 2 // false && 未声明报错   ==> 未声明报错
);

Use logic and simple short-circuit branch, only to meet certain conditions, will do something

Case Code

// 利用逻辑与的短路实现简单的分支,只有满足了某个条件,才会做某事。
// 请用户输入自己的薪水,如果薪水小于6000,则涨薪20%。
var salary = parseFloat(prompt("请输入您的薪水"));
// 判断用户的薪水是否小于6000,小于则涨薪20%。
(salary<6000) && (salary = salary*(1.2));
console.log("您张新后的薪水为:"+salary);

  Suppose we fill in the 3000 bomb box

    

  Suppose we fill in the 8000 bomb box

    

 

Short circuit or logic implemented by using the default value of the effect.

  If the first value is valid, then the priority value of 1, if the first value is invalid, then a value of 2.
 

Case Code

// 利用逻辑或的短路实现默认值效果,如果第一个值有效,则优先使用值1,如果第一个值无效,则使用值2.
//请用户输入自己的座右铭,如果用户输入了,就用用户自己输入的,如果游湖没输入,就输出“用户很懒,什么都没留下。”

var input = prompt("请输入您的座右铭");
var msg = input || "用户很懒,什么也没留下。";  // 存储个人说明的内容。
console.log(msg);

  Assuming that the input "Facing Death" in the bomb box, click OK

    

  Assuming no typing in the bomb box, click OK

    

Conditional Operations

  Conditional operations, also known as three head operation, ternary operator

  Conditional operator can achieve a simple branch

  Conditional operator:?

    -   conditional expression 1: Expression 2?

    - If the condition is true, the expression is executed 1

    - If the condition is false, execution expression 2

 

 Case Code

// 条件运算符
// 长出两个数中的最大值.
var a = 10, b = 20;
var max;
a>b ? max = a:max =b;
console.log(max);  // 20

var a = 50, b = 20;
var max;
a>b ? max = a:max =b;
// max = a>b?a:b;  // 简写
console.log(max);  // 50

 

Case: Please enter the user's own salary, if the salary is less than 6000, the salary increase of 20%. Otherwise salary increase of 10%

// 请用户输入自己的薪水,如果薪水小于6000,则涨薪20%。否则涨薪10%
var salary = parseFloat(prompt("请输入您的薪水"));
// 判断用户的薪水是否小于6000,小于则涨薪20%。
// (salary<6000) && (salary = salary*(1.2));
salary<6000?(salary*=1.2):(salary*=1.1);
console.log("您张新后的薪水为:"+salary);

  Suppose shells input box 5000

  

  Suppose shells input box 6000

  

Bit computing

   Bit operations, special operations for decimal part of a special simplified.

    ( 1) left and right

      -  << left , the current value of the binary number, n bits left.

        - m << n, the m n-bit left shift, corresponding to m * 2n.

      - >> 右移,将当前数值的二进制,右移n位。

        - m>>n,将m右移n位,相当于m/2n。

    (2)下取整

        m^0 或 m|0。 例如: 7.8^0   ==>  7  

/*左移和右移
      1     1   1   1   1  1  1  1
      128  64  32  16  8  4  2   1

      6 -> 110
      00000110
      00110000
      00000011
      00000001
      00000000

    */
    var n=6;
    console.log(n<<3);//n*2*2*2  -> 48
    console.log(n>>1);//n/2  -> 3
    console.log(n>>2);// ->1
    console.log(n>>3);// ->0


    //下取整 m^0  m|0
    var m=55.55;
    console.log(m^0);//55
    console.log(m|0);//55

  完成!

 

发布了118 篇原创文章 · 获赞 123 · 访问量 6万+

Guess you like

Origin blog.csdn.net/weixin_42776111/article/details/104574445