Re-consolidate JS series, are more basic things, can be checked to fill gaps, quickly read, this is the first.

Other JS reconsolidation series:

  • Re-consolidate JS (2) – JavaScript operators and expressions
  • Reconsolidate JS (3) — JavaScript statements

Basic JavaScript types

In programming, a data type is used to classify the value of a variable so that mathematical, relational, and logical operations on the value of a variable do not produce errors. For example: 2 * 2 is legal, 2 * John is not.

Basic types in JS are divided into primitive types and object types.

1. Original Type:

Primitive types (6 types) : Number, String, Boolean, Symbol, Null, Undefined.

1.1 value – Number

  1. Standard: No distinction is made between integers and floating-point numbers. All values are represented by floating-point numbers in the 64-bit floating-point format defined by IEEE 754 standards.
  2. The integerIs the scope:53 ~ 2-2 ^ ^ 53In decimal, in binary0bIs the prefix, followed by the value0 ~ 1Composition), octal (octal, to0oIs the prefix, followed by the value0 ~ 7Note: 0 prefix is not recommended), hexadecimal (in0xIs the prefix, followed by the value0 ~ 9anda~f(10 to 15) indicates. Such as:
/ / binary
0b11 / / legal
0b22 / / is illegal

/ / octal
0o67 / / legal
0o89 / / is illegal

// Hexadecimal
0xAf / / legal
0xjk / / is illegal
Copy the code
  1. Floating point NumbersThere must be at least one decimal point behind the decimal point. Floating-point calculations are subject to error and can be used for extremely large or small numberseRepresentation, for example:
// There is an error
0.1 + 0.2 = 0.30000000000000004

/ / e notation
3.14 e5 / / is 314000
Copy the code
  1. The special numerical, mainly from: global direct quantity (infiniteInfinityAnd the digitalNaN), properties of the Number object (Number.MAX_VALUE,Number.MIN_VALUEEtc.), Math object (Math.PI.Math.EAnd so on.

1.2 Character String – String

  1. Adopted standard: a sequence of zero or more 16-bit Unicode characters used to represent text.
  2. You can use either single or double quotation marks (they have the same effect, but must be paired). HTML strings often appear in JS code. Since double quotation marks are recommended for attribute values in HTML tags, single quotation marks are recommended for JS strings, for example:
let imgEl = '<img src="xxxx.jpg" title="my photo">'
Copy the code
  1. The index starts at 0 and is the number of characters in length.
let str = 'Hello World'
str.length / / = > 11
str[0] // => 'H'
Copy the code
  1. Strings cannot be wrapped directlyIf you want to split multiple lines, add a backslash at the end of each line\, the output is still a single line.
Let STR = 'multiple\ lines\ end' STR // => 'multiplelinesend'Copy the code
  1. Escape character: A backslash followed by a character indicates an escape character, such as\nRepresents a newline character. If the character following the slash has no special meaning, the slash is ignored. Common escape characters:
character meaning
\n A newline
\ \ slash
\ ' Single quotes, used in strings represented by single quotes
\" Double quotation marks, used in strings represented by double quotation marks
\xnn A character in hexadecimal code (n is 0 to F). For example,\x41Said, ‘A’
\unnnn A unicode character represented by a hexadecimal code, where n is 0 to F. For example,\u03a3Represents the Greek character ‘∑’
  1. Template Strings (new in ES6): Use back quotation marks (heavy notes,"`"), inside the variable usedThe ${}Wrap, you can wrap directly.
let name = 'cc'
name // => 'cc'
`Hello, ${name}` //=> 'Hello, cc' 
Copy the code

1.3 Boolean – Boolean

Used to mean true or false, on or off, etc.

  1. There are only two values:trueandfalse, usually used inIf/else statementsIf true executes the logic in the if statement, otherwise executes the logic in the else statement.
if( status === 'open') {
    console.log('Door is open');
} else {
    console.log('Door is closed');
}
Copy the code
  1. False value and true valueAny value in: JS can be converted to a Boolean value. A total of 6 values (Undefined, null, 0, -0, NaN,") is converted to false and all other values are true.

1.4 characters — Symbol (new in ES6)

Can be used as a non-string property of an object that is unique and immutable, for example:

let uniqueName = Symbol(a);let person = {}; 
person[uniqueName] = 'cc'; 
console.log(person[uniqueName]); // => cc
Copy the code

(Note: detailed explanation later)

1.5 Undefined

Undefined or nonexistent, with a single value undefined.

let abc;
console.log(abc); // => undefined
console.log(abcd); // An error was reported. Note the difference between unassigned and undeclared values
let obj = {};
console.log(obj.name) // => undefined
Copy the code

1.6 the Null

The Null type has only one value, Null. If you define a variable to represent an object, it is recommended to set it to NULL.

let obj = null;
obj = { name: 'cc'}
Copy the code

2. Object type – Object:

All are objects except values of primitive types. An object is a collection of key-value pairs. The value can be a raw value or an object. Such as:

// Author object: has the name attribute, which is 'cc', and the age attribute, which is 100
let author = {
    name: 'CC'.age: 100
}
Copy the code

2.1 Important special objects

Special objects: Function, Array Array, Date, re RegExp, Global, Error. (Note: Later detailed explanation)

3 Type Identification

You can use the typeof operator to detect the data typeof a variable, which returns a lowercase string value.

The value of the variable ABC Result of typeof ABC
let abc; 'undefined'
let abc = true; 'boolean'
let abc = 123; 'number'
let abc = 'abc'; 'string'
let abc = function () {}; 'function'
let abc = {}; / / [], null 'object'
let abc = Symbol(); 'symbol'

Note: Collated from netease public courses.