Its easy to hold, it is not easy to find trillion;
It is fragile, it is slightly easy to disperse. For it is not, treatment is not chaos. The folded wood is born at the very end; Nine layers of Taiwan, from the base soil; A journey of a thousand miles begins with a single step. For the defeat of the implementation of the loss. It is the sage who does nothing without failure, without persistence without loss. What the people do is often defeated by a few successes. He that finishes well is sure of failure. Is the saint desire not desire, not expensive rare goods; Learn not to learn, answer what everyone has done. To supplement the nature and dare not do. — Chapter 64 of Tao Te Ching.
JS/TS Primitive data Type
- Boolean type
- String type
- Numeric types
- null
- undefined
- Es6 Symbol data type
Boolean type: Boolean
George Boole (November 2, 1815.2 ~ 1864) was born in Lincoln, England on November 2, 1815. One of the most important mathematicians of the 19th century, published The Mathematical Analysis of Logic, the first of his many contributions to symbolic logic. In 1854, he published A Study of the Laws of Thought, which is his most famous work. In this book Boole introduced the Boolean algebra that now bears his name.
// js
let bool = false;
let boolr = true;
// ts
let boolts: boolean = false;
let boolrts: boolean = true;Copy the code
Boolean packaging type:
A wrapper type is created using a constructor that wraps an object around a primitive type.
let boolWrap = new Boolean(fasle);
Copy the code
String type
Character String: A string is a string of characters consisting of numbers, letters, and underscores.
// js
let str = 'this is string';
// ts
let strTs: string = 'this is ts string';
Copy the code
String wrapper type
let strWrap = new String(false)
Copy the code
The essence of a string wrapper type is an object that wraps a string.
Numeric types
Numeric types are used to represent quantitative values for calculation
let a = 123;
// ts
let b: number = 123;
Copy the code
Type of digital packaging:
let num = new Number(123)
Copy the code
The string wrapper type is an object that wraps the number type.
null
The Null type usually means empty, nothing.
// js
let xi = null;
// ts
let xii: null = null;
Copy the code
Undefined type
Undefined means undefined, unlike null, which is null. Undefined variables will output undefined or an error if they are accessed.
// js
let b = undefined;
//ts
let c: undefined = undefined
Copy the code
Symbol
Symbol, which means unique, was introduced from JavaScript to address unique features. For example, object property names are unique. Symbol is created by a Symbol function call without a constructor
let sym = Symbol(123)
Copy the code
summary
The primitive data types in JavaScript are the six primitive data types. Basic data types are stored on the stack. The base data type does not refer to changes in the data type.
JS/TS references data types
- An array of
- object
- function
An array of
A javascript array is a collection of data whose members can be primitive or reference types. However, TypeScript constrains numeric members to be of the specified type.
// Create an array
let a = [1.2.3.'string'];
var arr_names:number[] = new Array(4)
// ts
let a: number[] = [1.2.3]; // All members must be numbers
let a: Array<number> = [1.2.3]; Use generics to constrain all members to be of type number
// ts specifies the type
let b = [number, string, number] = [123.'hello'.89]
Copy the code
Object type
An object is a collection of key-values, which in JavaScript can be: basic data type, reference data type.
// js
let ob = {
aa: false.bb: 123.cc: 'this is cc'.dd: null.ee: undefined;
ff: Symbol("ff"),
gg: [1.2.4].hh: function() {console.log("hh")},
jj: {x: 123.y: false}}//
let ob: Object = {
aa: false.bb: 123.cc: 'this is cc'.dd: null.ee: undefined;
ff: Symbol("ff"),
gg: [1.2.4].hh: function() {console.log("hh")},
jj: {x: 123.y: false}}Copy the code
Function types
// ts, if you need a type, you need to check the type inside the function
let func = function func(a) { console.log(a)}
// ts parameter type, return value type, determine the function type
let fn = function fn(a: number) :void {console.log(a)}
Copy the code
Supplementary types of TS
- Tuple types: Tuple types allow you to represent arrays with a fixed number of elements whose types are known but not necessarily the same.
- Enumerated types: Useful complementary enUms to the standard set of data types
- Unknown Type: Describes variable types that are Unknown when an application is written
- Any Arbitrary type: Unlike unknown type, the type variable Any allows you to access Any property, even if none exists
- Void Void is the opposite of any: there is no type at all. Typically used for function return values
- Never: The Never type indicates that the value is of a type that will Never occur
In other words, there are six more constraint types on the data type.
summary
- There are six basic data types and basic creation methods.
- The basic data type of TS is the same as that of JS. How to enumerate functional types such as types and any types
- A simple comparison between JS and TS defines the difference between creating variables, in fact, is the difference between type constraints.
The next step
- Data type judgment
- Types of assertions