JavaScript appetizer

Time:2020-7-9

notes

Single line comment: // shortcut key: Ctrl +/    
Multi line comment: / * content * / shortcut key: Ctrl + Shift +/

variable

  • Declarative variable
var name;
  • assignment
name = 'peach';
  • initialize variable
var age=18;
  • Update variables
var age_age = 18;  
age_ Age = 20; // update
  • Declare multiple variables
var a=1, b=2, c=3;
  • Variable naming conventions
Rules:  
    -It consists of letters (a-za-z), numbers (0-9), and underscores (_ ), dollar sign ($), such as usrage, num01_ name  
    -Case sensitive. Var app; and VaR app; are two variables  
    -Cannot start with a number. 18 age is wrong  
    -Cannot be a keyword or reserved word. For example: VaR, for, while  
    -Variable names must be meaningful. MMD   BBD        nl   →     age  
    -Comply with hump nomenclature. The first letter is lowercase, and the first letter of the following word needs to be capitalized. myFirstName

data type

JavaScript appetizer

    Data type number
    • Data type number
    Var age = 21; // integer type    
    Var age = 21.223; // decimal type
    • Digit base
    The most common system is binary, octal, decimal, hexadecimal
    • Numeric range, maximum and minimum
    Number.MAX_ Value; // the maximum value: 1.7976931348623157e + 308    
    Number.MIN_ Value; // minimum value: 5e-32
    • Three special values of digital type
    Infinity // stands for infinity, greater than any number    
    -Infinity // stands for infinitesimal, less than any value    
    Nan // not a number
    String type string
    • String length
    Var strmsg: "I am a handsome and golden procedural ape! ";
    alert( strMsg.length ); // display 11
    • String splicing

    Multiple strings can be spliced with +. The splicing method is string + any type = new string after splicing

    Before splicing, any type added with the string will be converted into a string, and then spliced into a new string

    //1.1 string "add"
    alert('hello' + ' ' + 'world'); // hello world
    //1.2 numeric string "add"
    alert('100' + '100'); // 100100
    //1.3 numeric string + numeric value
    alert('11' + 12);     // 1112

    +Sum up the formula: add the values, connect the characters

    • String enhancement
    var age = 18;
    console.log ('I '+ age +'age'); // I'm 18 years old
    Boolean

    Boolean types have two values: true and false, where true is true (true) and false is false (false)

    When Boolean and numeric are added, the value of true is 1 and the value of false is 0

    console.log(true + 1);  // 2
    console.log(false + 1); // 1
    Undefined and null

    A variable that is not assigned after declaration has a default value of undefined

    var variable;
    console.log(variable);           // undefined
    console.log ('Hello '+ variable); // Hello undefined
    console.log(11 + variable);     // NaN
    console.log(true + variable);   //  NaN

    A declared variable is given a null value, and the value stored in it is empty

    var vari = null;
    console.log ('Hello '+ vari); // Hello null
    console.log(11 + vari);     // 11
    console.log(true + vari);   //  1

    Get variable data type

    • Test the data type of the variable

    Typeof can be used to get the data type of the test variable

    var num=1;
    console.log(typeof num); // number

    image

    Data type conversion

    Convert to string
    • toString()
    var num = 1;
    console.log(typeof num.toString());  // 1  string
    • String()
    var num1 = 1;
     console.log(typeof String(num1));  //  1  string
    • Plus concatenated string
    var num2 = 1;
    console.log ('I '+ num2); // me 1

    Tostring() and string() are used differently

    There are three conversion methods, and the third one is the plus sign concatenation string conversion, which is also called implicit conversion

    Convert to data type
    • parseInt
    console.log(parseInt('10'));  // 10
    • parseFloat
    console.log(parseFloat('10.123'));  // 10.123
    • Number
    console.log(Number('10.2'));  // 10.2
    • JS implicitly converted to a data type
    console.log('10' - 1);   //  9
    Convert to Boolean

    Values representing null and negative are converted to false, such as’ ‘, 0, Nan, null, undefined

    The rest is converted to true

    console.log(Boolean(0));  // false
    console.log(Boolean(12));  // true