Cold rice fried new - javascript base 2

Posted by ozzysworld on Wed, 08 May 2019 00:00:03 +0200

JavaScript Foundation 2

Learning objectives:

  1. Operators in JavaScript

Expressions and Statements

Expression

An expression can produce a value, possibly an operation, a function call, a literal quantity, and so on.Expressions can be placed anywhere you want.

For example: 5+6

Sentence

Statements can be understood as an action, and looping and judging statements are the most classic ones.A program consists of many statements.A statement, usually ending with; orIt is not recommended to omit.

Operator

Operator, also known as operator

Expressions generally consist of operators and operands (common expressions)

Arithmetic Operators

Add, subtract, multiply and divide inside math

// + - * / %
// Same as in math
var x = 5;
var y = 10;
console.log(x + y);
console.log(x - y);
console.log(x * y);
console.log(x / y);
console.log(x % y);

// Two more special
console.log(x/0); // Infinity
console.log(x % 0 ); // NaN

Unary operator

An operator with only one operand is called a unary operator

Operators that can carry two operands are called binary operators, so all the previous lessons are binary operators.

// ++ Self+1
// --Self-1
var num = 5;
console.log(++num);  // 6
console.log(--num);  // 5

++ num is first an expression, and since it is an expression, it has a return value.

The difference between the front and back is this return value

  • Front

Let the operand itself + 1 (-1) before returning the result of the operation.

var num  = 5;
console.log(++num);// 6;
console.log(num); // 6
var num1 = 7;
console.log(num + ++num1);//14   6+8
console.log(num1);// 8

console.log(--num1);// 7
  • Rear

Return the result of the operand first, then + 1 (-1) for the operand itself

var num = 5;
console.log(num++);// 5
console.log(num);//6
var num1  = 7;
console.log(num + num1++); // 6+7 13
console.log(num1);// 8
  • Consolidation exercises
var a = 1;var b= ++a + ++a;console.log(b);//2 + 3 b=5
var a = 1;var b= a++ + ++a;console.log(b);//1 + 3(++a before a became 2) b=4
var a = 1;var b = a++ + a++;console.log(b);// 1 + 2 b=3
var a = 1;var b = ++a + a++;console.log(b);// 2 + 2 b=4

Logical operators

Logical operators are also called Boolean operators.

  • && (Logical AND): Both operands are true, resulting in true, otherwise false.also
  • || (logical or): One of the two operands is true, resulting in true, otherwise false.
  • ! (Logical NOT): Reverse
// Logical Operators and &&or||Not!
var a = true;
var b = false;
console.log(a && b);
console.log(a || b);
console.log(!a)

When do I use logical operators?

When judgment or looping is needed in the future, a large number of logical operations will be used.

Implicit Conversion of Boolean Types
var num = 123;
console.log(Boolean(num));// Cast

// Implicit Conversion
// Case 0,'',NaN,undefined,null in case 5 converted to false
if(num){
    console.log('Conversion Successful');
}
var msg;
if(msg){
    console.log('true');
}else{
    console.log('false');
}
//Determine whether a variable exists
//console.log(typeof aaa === 'undefined');

// Implicit Conversion Using None
var str = 'Ha-ha';
var isOk = !!str; //  true, reverse false, reverse
console.log(isOk);

Relational Operators

The relational operator is also called the comparison operator

The return value used to compare the size of two numbers or the difference is a Boolean type

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

var a = 10;
var b = 5;
var c = '10';

// Compare the sizes of two numbers to return a Boolean value
console.log(a>b);
console.log(a<b);

// When they are equal,
// == !=
console.log(a==b);
console.log(a!=b);
//==Determines whether the values of variables are equal, regardless of type
//===Determine whether the types are the same before they are equal
//For strictness, always recommend ===
console.log(a==c);
// === !==
console.log(a===b);
console.log(a!==b);
// Returns true only if both values and data types are the same
console.log(a===c);

==Determines whether the values of variables are equal, regardless of type
===Determine whether the types are the same before they are equal
For strictness, always recommend ===

Assignment Operators

= ,+= ,-= , *= ,/= ,%=

var num = 5;
var num1 = num * 5; 
// Simplified grammar
num1++; // Self-increasing 1
num1 = num1 + 5; // Self-increasing 5
// Simplified grammar
num1 += 5; //  Equivalent to Above
// Same as
num1 -= 5;
num1 *= 5;
num1 /= 5;
num1 %= 5;

Operator precedence

Operator precedence

  1. () highest priority
  2. Unary operator ++ -!
  3. Arithmetic operators */% before + -
  4. Relational Operator > >= < <=
  5. Relational Operator==!=====!==
  6. Logical Operator First &&Then||
  7. Assignment Operators
// Exercise 1
4 >= 6 || 'people' != 'Avatar' && !(12 * 2 == 144) && true;
//Parse first step parentheses
(4 >= 6) || ('people' != 'Avatar') && (!(12 * 2 == 144)) && true;
//Step 2
(4 >= 6) || ('people' != 'Avatar') && (!(24 == 144)) && true;
//Step 3
(4 >= 6) || ('people' != 'Avatar') && (!false) && true;
//Step 4
(4 >= 6) || ('people' != 'Avatar') && true && true;
//Step 5
false || true && true && true;
//Step 6
false || true;
//Step 7
true;

// Exercise 2
var num = 10;
5 == num / 2 && (2 + 2 * num).toString() === '22';
//Parse first step parentheses
(5 == num / 2) && ((2 + 2 * num).toString() === '22');
//Step 2
(5 == 5) && ((2 + 20).toString() === '22');
//Step 3
true && (22.toString() === '22');
//Step 4
true && ('22' === '22');
//Step 5
true && true;
//Step 6
true;

Topics: Javascript