I. Seven data types of JS

  1. Number: integers and decimals
  2. String: Text wrapped in ” or “”
  3. Boolean: Two special values for true or false. There are only two values, true and false
  4. Symbol: Generates unique values
  5. Null: indicates a null value
  6. Undefined: undefined or non-existent
  7. Object: A collection of values

(2) Number

1. Numerical representation

Numerical values can be expressed in a variety of ways. They can be expressed directly in literal form, such as decimal, binary, hexadecimal, or in scientific notation. Examples are as follows:

  • Decimal value: 23 = 23
  • Binary (prefix 0b or 0b) : 0b10 = 2
  • Octal (prefixed with 0o or 00, or preceded by 0 and used only with eight Arabic digits 0-7) : 011 = 9
  • Hexadecimal (prefix 0x or 0x) : 0x11 = 17
  • Scientific notation: 1.23e2 = 123

2. JavaScript numerical special case

By default, JavaScript automatically converts octal, hexadecimal, and binary to decimal.

There are two special cases where JavaScript automatically converts numeric values to scientific notation

(1) There are more than 21 digits before the decimal point

1234567890123456789012 / / 1.2345678901234568 e+21Copy the code

(2) There are more than five zeros after the decimal point

0.0000003 / / 3 e - 7Copy the code

3. NaN

NaN is a JavaScript special value that means Not a Number. NaN is Not equal to any value, including itself. NaN is mostly used when parsing strings into numbers is wrong, as in

5 - 'x'  // NaN 
Copy the code

4. Infinity

Infinity means Infinity, two scenarios. One is that the positive number is too large or the negative number is too small to represent; The other way is if you divide by 0, you get Infinity.

String (string)

1. Empty string

“Or “”, length is 0

2. Space string

“Or” “, length is 1

3. Multi-line strings

Enclose with two back quotes (‘), as in

var s = `1234
6789`
Copy the code

4. Escape of strings

The backslash (\) is also called an escape character because it has special meaning in a string and is used to indicate special characters. Special characters that need to be escaped with backslashes are mainly the following.

  • \0: null (\u0000)
  • \ B: Back Key (\u0008)
  • \ F: Page feed (\u000C)
  • \ N: Line break (\u000A)
  • \ R: Enter key (\u000D)
  • \ T: TAB (\u0009)
  • \ V: Vertical TAB (\u000B)
  • ‘: single quotation marks (\u0027)
  • “: double quotation marks (\u0022)
  • \ : Backslash (\u005C)

Boolean, null and undefined

1. Boolean (Boolean)

And operation and or operation: the former one is false, the latter one is true

2. Null and undefined

A value of the null type has only null, and a value of the undefined type has only undefined.

Difference: undefined means that the variable is not assigned, and undefined is NaN when converted to a value. Null means that the value is now empty and 0 when converted to a value.

5. Object

1. The concept

Simply put, an object is a set of key-value pairs, which is a disordered composite data set. Objects are complex types, and the other six are simple types (basic types), and objects are made up of other simple types.

2. Key name

All key names of objects are strings (ES6 introduces the Symbol value as a key name), so it can be quoted or not quoted. However, if the key name does not meet the criteria for identifying a name (for example, the first character is a number, or it contains a space or operator) and is not a number, it must be quoted or an error will be reported. #### Read Read the properties of an object. There are two methods, one using the dot operator and the other using the square bracket operator.

var person = { name: 'Q' }; // Read person. Name preson['name']Copy the code

The assignment

var person = {}; person.name = 'm'; preson['name'] = 'n'; / / assignmentCopy the code

Delete: delete command

If you use the delete command to delete an attribute, both the key and value are deleted. If you use the delete command defined as undefined, only the value is deleted, but the key remains.

Verify the presence of the attribute: in operator

The IN operator checks whether an object contains an attribute (a key name, not a key value) and returns true if it does, false otherwise. It has a string on the left, representing the property name, and an object on the right

var obj = { n: 1 };
'n' in obj // true
'toString' in obj // true
Copy the code

Traversal properties: for… In circulation

var person = {name: 'm', age: 12}; For (var key in person) {console.log (key)} for (var key in person) {console.log (person.[key])} // Iterate over value for (var Key in person) {console.log (key, person.[key])} // Iterate over key and valueCopy the code

Typeof determines the data type

The Typeof operator can return a value data type

typeof 123 // "number"
typeof '123' // "string"
typeof false // "boolean"
Copy the code

String returns string, Boolean returns Boolean, undefined returns undefined, object returns object; Where function returns function and NULL returns object (historical cause).

typeof null // "object"
Copy the code

The resources

Ruan Yifeng JavaScript tutorial