Data type (personal use note)

type of data

Previously stated: The following content is the zero-based author's learning and memory content, most of which are extracted from LINK , and there are some parts that I have organized and added. If you can also help beginners, I also hope to directly support the author of the above link.

Number

  • It does not distinguish between integers and floating-point numbers, and is uniformly represented by Number.
    Including:
    integer//123
    floating-point number// 123.4
    scientific notation
    // 1.234e3 negative number// -2
    NaN //Not a Number
    Infinity //Infinity, 2/0

  • Regarding the operation rules
    , four arithmetic operations can be performed directly, and the priority of the rules is consistent with mathematics

    1+2; //3
    (1+2)* 5/2; //7.5
    2/0; //Infinity
    0/0; //NaN
    10%3; //1
    10.5%3*5; //7.5
    (The remainder operation has the same priority as multiplication and division)

String (string)

  • Any text enclosed in single quotation marks ('') or double quotation marks (“ ”) is composed of zero or more characters (letters, numbers, punctuation marks or spaces) (if the string contains single or double quotation marks, Use escape character identification)

  • Escape character escape:
    \n Table line break
    \t Tab character
    \\ Table
    \x## ASCII character hexadecimal (\x41 is equivalent to'A')
    \u#### Table Unicode character (Unicode)

  • Multiline string
    Normally, multi-line characters are connected by'\n' in a new line.
    Another method is added to the ES6 standard, which is to use backquotes (`...`):

    `Multi
    -line
    character
    character
    string`

  • Template string
    Usually multiple strings are linked with '+':

    var a='A';
    var b='B';
    var S=a+b; //'AB'

    ES6 standard new template string representation:

    var a='A';
    var b='B';
    var s=${A},${B};//'A,B'

  • String manipulation

    Common operations:

    • Measuring string length

      var s='Hello!!';
      s.length;//8

    • Get the character at a specified position (similar to taking an element from an array, the index starts from 0)

      var s='Hello !';
      s[0]; // 'H'
      s[6]; // ' '
      s[7]; // '!'

      The string is immutable , there will be no error if the index is assigned, and there will be no effect at the same time

      var s='Hello !';
      s[0]='X';
      alert(s);//s[0]的值仍然为‘X’

      // alert();is the output content (pop-up window)

      • The following method does not change the original content of the string, but directly returns a new string
    • toUpperCase
      Change a string to uppercase, only valid for letters

      var s='abc123!#¥%';
      s.toUpperCase();// 返回'ABC123!#¥%'

    • toLowerCase
      Change a string to all lowercase, same usage as the former

      var s='ABC123!#¥%';
      s.toUpperCase();// 返回'abc123!#¥%'

    • indexOf
      Search for the index of the first character in the specified string

      var s='abc123!#¥%';
      s.indexOf('abc');//返回 0
      s.indexOf('123');//返回 3

    • substring
      Returns the substring of the specified index range

      var s='abc123!#¥%';
      s.substring(0,5);// 从0开始到4(左闭右开),返回'abc12'
      s.substring(5);// 从索引5开始到结束,返回'3!#¥%'

Boolean (Boolean value)

  • There are only two values, namely true and false

  • About Boolean operations

    • && and operation

      true && true; // true
      true && false; // false
      true && false && true; // false

    • || OR operation

      false || false; // false
      false || true; // true
      false || true || false; //true

    • ! Non-operation (unary operator)
      ! true; // false
      ! false; // true
      ! (2> 5); // true

Boolean value is often used in conditional judgment, and its value is expressed as the choice of if and else

(It is still to be tried on the priority of comparison operations below and below)

About comparison operations
  • Comparison operators:> ,>= ,==

    In addition, '==' can automatically convert the data type and then compare (may get weird results)

    '==='The comparison will not convert the data type, if inconsistent, it will directly return'false'

  • NaN === NaN; // false

    isNaN(NaN); // true (the only way to judge)

  • Use the absolute value of the difference to compare with a certain threshold when comparing floating-point numbers

null & undefined

  • Null means "empty" value; undefined means "undefined"

  • The two are similar and almost indistinguishable. In most cases, "null" should be used

  • "Null" can be used to empty the object

eg: var a=null; //clear a

  • undefined is used to determine whether the function parameters are passed (value transfer, pointer transfer, transfer reference), when the index is out of range, it will fill in the undefined place with this

Array

  • It is a set of ordered combinations (the data set under object is unordered ), which can contain any data type

  • Two ways to create an array

    1. Use directly [ ]include elements to achieve (the elements are separated by ",")

      [ 1,2,3.14,‘hello’,null,true,undefined]

    2. Use the Array () function to achieve

      new Array(1,2,3); // Created an array [1,2,3]

  • Index access of array(Starting value is 0)

    eg:
    var arr=[1,2,3,4];
    arr[0]; //返回 1
    arr[1]; //返回 2

    • The corresponding element can be modified to a new value by index

      var arr=[1,2,3,4];
      arr[0]=99;
      arr;//arr变为[99,2,3,4]

      Most other programming languages ​​do not allow to directly change the size of the array, and JS will not produce any errors, so it is not recommended to modify its size directly to ensure that the index will not be crossed when accessing the index ** (including the direct access to the length property of the Array mentioned later) modify)**

  • length
    Get the length of Array

    var arr=[ 1,2,3.14,'hello',null,true,undefined];
    arr.length;//7

    • Assigning a new value directly to length will cause the Array size to change:

      var arr=[ 1,2,3];
      arr.length;//3

      arr.length=6;//相当于改变arr的长度
      arr;//arr变为[1,2,3,undefined,undefined,undefined]

      arr.length=2;//
      arr;//arr变为[1,2]

  • indexOf
    Similar to string, it is used to search the position of a specified element and return the index value

    var arr=[ 1,2,3.14,'hello',null,true,undefined];
    arr.indexOf(1);//返回元素1的索引 0
    arr.indexOf(3.14);//返回元素3.14的索引 2
    arr.indexOf('hello');//返回元素hello的索引 3
    arr.indexOf('1');//返回 -1,未找到元素'1'
    (直接输入hello会报错)

  • slice

    Similar to substring() of string, it is used to intercept some elements of Array , and then return a new Array

    var arr=[1,2,3,4,5,6,7,8,9];
    arr.slice(0,3);//从0开始到2(左闭右开),返回[1,2,3]
    arr.slice(3);//从3开始取到最后,返回[4,5,6,7,8,9]

    If no parameters are passed, all elements will be intercepted and output, and an Array can be copied from this:

    var arr=[1,2,3,4,5,6,7,8,9];
    var copy=arr.slice();
    copy==arr;//false*

    • The last sentence of the above code leads to whether the data between the arrays are equal (comparison with the same elements)

      ​ Javascript cannot directly use the equal sign to judge whether the arrays are equal, no matter which statement is used ==or ===, the output result will be'false'
      ​ To judge whether the array is the same, you need to convert the array to a string and then compare, no matter which one you use The result of all equal comparison signs is true

      var arr=[1,2,3,4,5,6,7,8,9];
      var copy=arr.slice();
      arr.toString()==copy.toString();//true
      arr.toString()===copy.toString();//true

      ​ If you compare whether the elements in the two arrays are exactly the same (the order of the elements is not necessarily), sort the arrays and then perform the above steps

      var arr1=[1,2,3,4,5,6,7,8,9];
      var arr2=[1,2,3,5,4,6,7,8,9];
      arr1.sort().toString()==arr2.sort().toString();//true
      arr1.sort().toString()===arr2.sort().toString();//true

      ​ It will be mentioned later on toString and sort

  • push and pop
    push() add several elements to the end of the Array, output the new length of the Array
    pop() delete the last element of the Array, and output the deleted element value

    var arr=[1,2];
    arr.push('A','B');//返回Array新长度(4)
    arr;//[1,2,'A','B']
    arr.pop();//返回'B'
    arr;//[1,2,'A']

    arr.pop();arr.pop();arr.pop();//连续pop 3次只会输出最后一个删掉的值1
    arr.pop();//返回undefined
    arr;//[]

  • unshift和shift
    unshift() add several elements to the head of the Array, output the new length of
    the Array, shift() delete the first element of the Array, and output the deleted element value

    var arr=[1,2];
    arr.unshift('A','B');//返回Array新长度(4)
    arr;//['A','B',1,2]
    arr.shift();//返回'A'
    arr;//['B',1,2]

    //连续使用与对空数组操作同上

  • sort

    Sort the Array and directly modify the position of its elements, and use the default sort when calling directly (you need to use functions to sort in the order you specify)

    var arr=['A','C','B'];
    arr.sort();
    arr;//['A','B','C']

  • reverse
    Whole reverse Array

    var arr=['one','three','two'];
    arr.reverse;
    arr;//['two','three','one']

  • splice

    The universal method to modify Array

    var arr=['A','B','C','D','E','F'];

    arr.splice(2,3,'a','b');//From the index 2 starts deleting elements 3 , and then adding an element 'a', 'b', the output in the form of an array element is removed
    arr;//['A','B','a','b','F']

    /*若不删除元素,则输入'0‘
    若不添加只删除,则不输入字符串*/

  • concat
    Connect the current Array with another Array (or any number of elements with Array) and return a new Array (without modifying the current Array)

    var arr=[1,2,3];
    var arrr=arr.concat([5,6,7],'a');
    arrr;//[1,2,3,5,6,7,'a']

    Arrays can be used directly in concat, such as:
    var arrrr=arr.concat(arr);
    arrrr;//[1,2,3,1,2,3]

  • join
    Connect all elements in the current Array with the string specified by the input (if the element is not a string, it will be automatically converted to a string)

    var arr=[1,2,3,'a'];
    arr.join(+);//'1+2+3+a'

  • Multidimensional Arrays
    If the elements in an array are Array, the array is a multi-dimensional array

    var arr=[1,2,3,[111,222,333]];

    On how to obtain the value of the internal array of the multidimensional array:

    var arr=[1,2,3,[111,222,333]];
    var x=arr[3][2];//[3]先取最外层数组中的数组元素,[2]取内层数组中的元素
    console.log(x);//333

Object

  • Is an unordered collection of key-values

    The keys of the objects are all string types, and the values ​​are not limited to data types

  • Definition of object properties:

    eg:

    var person = {
    name : 'Bob',
    age : 20 ,
    tags : ['1',2,'3‘] ,
    ‘has-Car’: true,
    zipcode:null
    };

    • The key-value pairs are declared in the form of xxx.xxx.In .the operator complete access to the property, it can also be used xxx [ 'xxx'] to access
      The key-value pair declarations are separated by',', and the last key-value pair is not added at the end.'You
      can use object properties to define an array, but the array is out of order.
      If the property name contains special characters, the property name must be changed in single bracket enclosed
      Call the array in the object propertiesThe method is the same as the ordinary array call, such as:
      person.tags[0]; // “1”
      person.tags[1]; // 2
  • Access to non-existent properties will return undefined

  • JavaScript objects are dynamic properties, you canAdd and delete attributes freely

    var xiaoming={name:'小明'};
    xiaoming.age;//undefined
    xiaoming.age=18;//增加一个age属性
    delete xiaoming.age;//删除age属性

    If you delete a non-existent attribute, no error will be reported//true will be returned (partial test result)
    Insert picture description here

  • Check whether an object has a certain attribute

    var xiaoming={name:'小明'};
    'name' in xiaoming;//true
    'age' in xiaoming;//false
    'toString' in xiaoming;//true*

    Regarding the'toSrting' in the last sentence is the attribute inherited by Xiaoming, that is, the attribute is not originally owned by Xiaoming.
    "Because toString is defined in the object object, and all objects will eventually point to object on the prototype chain, so Xiaoming also has this attribute"
    Determine whether an attribute is owned by an object itself

    var xiaoming={name:'小明'};
    xiaoming.hasOwnProperty('name');//true
    xiaoming.hasOwnProperty('toString');//false

variable

  • Variable name: It is composed of uppercase and lowercase English, numbers, $ and _, and cannot start with a number, and cannot use JS keywords as variable names.

    Declare variables, such as:
    var a; // a=undefined

    var $b = 1;

    var s_007 = '007s'; // s_007为字符串

    var A = true ;

    var n= null ; //n的值为“空”

Variable declaration can be done multiple times, but only one'var' declaration:

var a=123;
a='123';

(Display variable console.log(x);content: )

  • About strict mode

    It is used to fix the defect of js "multiple use of global variables declared without var can lead to errors in mutual influence". In this mode, variables declared without var are automatically forced to pass var declaration.

    • Usage:'use strict' // Browsers that do not support strict will report an error'ReferenceError'

Guess you like

Origin blog.csdn.net/qq_51686247/article/details/109167811