JavaScript Learning_Basic_02_Basic Data Types, Operators, and Expressions

1. Basic Data Types

  1. number type: Numeric type

    Common: Integer, Decimal

    var a = 1 ;
    

    Can be used but rarely used: scientific notation, binary (0b), octal (0), hexadecimal (0x)

  2. string type: string type

    Single or double quotation marks contain empty strings and empty strings.

    Single and double quotation marks are considered a string when they are parsed, as long as they are paired from left to right. When one string needs to be emphasized or another substring needs to be included, the substring's quotation marks should be distinguished from the outside quotation marks.

    var name = "Zhao LiYing" ;
    var name = 'zhaoliying' ;
    
    var name="";   //Empty String
    var name="  "; //Blank String
    
    name = "it's a dog" ; 
    name = "I love'Zhao Liying'" ;
    
  3. boolean type: boolean type

    true or false, Boolean only has these two assignments.

    var isMove = true ;
    isMove = false ;
    console.log(isMove) ;
    
    // At this point the printed value is false
    
  4. undefined type

    Defined variable has no assignment and undefined variable type is undefined.

    var age ;
    console.log(age) ;  
    
    // At this point the printed value is undefined
    

    Although undefined literally means undefined, it means that a variable is defined but not assigned. In this case (as in the code above), the variable contains undefined.

  5. null (empty type)

    Defined variables are assigned null, typically when an object is initialized or when an object is deleted.

    var obj = null ;
    console.log(obj) ;
    
    // The printed value is null
    

Detect data types (data type judgment)

Use typeof to type the data.

console.log(typeof num) ;    //number
console.log(typeof name) ;   //string
console.log(typeof isMove) ; //boolean
console.log(typeof age) ;    //undefined
console.log(typeof obj) ; //object  

// Because null is used to manipulate objects, the type comes out as object.
// An object is an object type.

Small difficulties

console.log(typeof typeof num) ; // string
console.log(typeof typeof typeof typeof isMove); // string

Operators and Expressions

Operator: The symbol that participates in the operation.

Expressions: Formulas consisting of variables or constants and operators. (Expressions are all valid)

  1. Arithmetic Operators

    +  -  *  /  % 
    

    '/ 'is a division sign and requires a quotient.

    '%'is the complement (modulus) sign and the remainder is required.

    The role of redundancy

    • You can get the remainder

    • You can get a range

      Principle: Any number balances 78, and the remainder is always between 0 and 77, including 0 and 77.

      Example: Let the remainder of a number always fall between 1 and 78.

      (num) % 78 + 1
      
  2. ++ and--

    The self-increasing and self-decreasing operator, which can only make up an expression with variables.

    // Take self-addition as an example
    
    var a = 3;
    console.log(a++ + 1);  // 4
    var b = 3;
    console.log(++b + 1);  // 5
    

    +, then assign, then add. (Assign the value of a to the expression, then let the value of a + 1)

    ++, add first, then assign. (Let a + 1, then assign a to the expression)

    train

    var a = 5 ;
    var b = 6 ;
    var c = 0 ;
        
    c = ++a + b++ ;  // 6 + 6 C = 12 after calculation a=6, b=7
    c = b + a++ ;    // 7 + 6 C = 13 after calculation a=7, b=7
    c = a + b++ ;    // 7 + 7 C = 14 after calculation a=7, b=8
    c = ++b + a ;    // 9 + 7 C = 16 after calculation a=7, b=9
    
  3. Assignment Operators

    =   +=  -=  *=  /=  %=
    

    "=": Assign the value to the right of the equals sign to the variable to the left of the equals sign. (Must be a variable to the left of the equals sign.)

    // compound assignment operators
    
    a += 6 ;  // a = a + 6 ;
    a -= 2 ;  // a = a - 2 ;
    ......
    
  4. Comparison operator (conditional operator) (result is boolean)

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

    "==": Decide if the values are consistent.

    "==": If both types and values are consistent, then they will be true. (Strict judgment, etc.)

    Detailed description:

    • console.log(1 == '1') ;  // true
      
      // If a number is compared to a numeric string, the numeric string is implicitly converted to a number and then compared.
      // ==Determines if the converted values are consistent.
      
    • console.log(1 === '1') ; // false
      
      // ===When judging numeric and numeric strings, the first step is to determine the type, the same type, and then whether the values are equal.
      
  5. Logical operators

    && ||!(AND, OR, NOT)

    Usually used for joining multiple conditional expressions.

    • &&: One false is false

    • ||: True is true

    • !:Not true is false, not false is true

      !No matter what you put before, the final result is a boolean value

    var a = 10 ;
    console.log(a > 8 && a < 9) ;  // The result is false
    

    Special Use of Logic Characters

    • " && "

      When used between two values, this is essentially a process:

      First, determine if &&is true before, and if so, assign the value to the logical expression.

      If the preceding value is false, the preceding value is assigned to the logical expression. (0 is false)

      a = 0 && 8 ;  // a=0
      a = 5 && 8 ;  // a=8
      
      var b = a > 8 && a < 9 ;  // b = false ;
      
    • " || "

      When used between two values, this is essentially a process:

      Determine if | is true before | and if so, assign the logical expression with the preceding values.

      If the preceding value is false, then the following value is assigned to the logical expression. (0 is false)

      a = 0 || 100 ; // a=100
      
      var b = a > 8 || a < 9 ;  // b = true ;
      

    train

    var a = 10 ;
    
    console.log(a > 8 && a < 9) ;         // false
    console.log(a > 8 && a < 100) ;       // true
    console.log(a < 0 || a > 8) ;         // true
    console.log(a >100 || a < 0) ;        // false
    console.log(a > 10/2 && a < 10 % 8) ; // false
    
    var a = 10 ;
    
    console.log(!(a > 0)) ;  // false
    console.log(!(a > 10/2+3) || a < 10 % 8 * 6) ; // true
    
  6. ternary operator

    Expression?Value: First execute the first expression (that is, the expression before'?') to see if it is true.

    • If the value of the first expression is true, use the value before the colon as the final result
    • If the value of the first expression is false, use the value after the colon as the final result
    var a = 6 ;
    var b = 7 ;
    var c = a < 0 ? ++b + a++ : --a+b ;		
    console.log(c, a, b) ;  // 12,5,7
    

Keywords: Javascript

Added by sirTemplar on Thu, 09 Sep 2021 20:34:43 +0300