[The JS] Data Type

In JavaScript Variables can hold any data. JavaScript is a dynamically typed language, variables can arbitrarily change the data type:

1 let message = 'hello';
2 lmessage = [1, 2, 3]
3 console.log(message); // [1, 2, 3]

JavaScript has a total of eight basic data types.

 

number type

represents an integer number type and a floating point number can have many operations, such as addition +, subtraction -, * multiplication, division / and the like.

Including three special values ​​"Infinity" positive infinity, '' - Infinity "negative infinity," NaN "non-numeric.

Wherein NaN represents a calculation error is caused by incorrect or undefined mathematical operations come, and having a viscosity, i.e., if computation occurred a NaN, will lead to the final result is NaN.

1 let a = "hello";
2 let b = 2;
3 console.log(a / 2); // NaN
4 console.log(a / 2 + 3); //NaN

 

BigInt type

Recently introduced until BigInt javascript type, it represents an integer of arbitrary length. Usually at the end of the integer field plus n to create BigInt.

1  // Appends BigInt n indicates that this is the type 
2 const = 45687965412365478n BigInt;

Note: currently only compatible with Chrome and Firefox.

 

String type

JavaScript string must be enclosed in quotes.

. 1 the let str1 = 'Hello'; // single quotes 
2 the let str2 = "Hi"; // double quotes 
. 3 ! = The let phrase `$ {} str1, str2} {$`;! // Backticks 
. 4 the console.log (phrase); // the Hello, hi!!

Single and double quotes are simply references, there is no difference.

Anti quotation marks are extensions, which allows only the variables and expressions will be packaged in $ {} string is embedded, for example:

. 1 the let name = "Neal" ;
 2 the console.log ( "Hi, Neal $ {}"); // Hi, Neal $ {} 
. 3  // embedded variables 
. 4 the console.log (Hi `,` $ {name}) ;   // Hi, Neal 
. 5  // embedded expression 
. 6 the console.log (IT apos `$ 12 is {+} now`. 1); // apos 13 is now IT

 

A Boolean (logical type)

Boolean type comprises two values ​​true and false.

Boolean values ​​can be used as a result of the comparison:

1 let isSmaller = 1 < 2;
2 console.log(isSmaller); // true

Itself also said is correct, true correct, false incorrect.

 

"null"

Most special null, does not belong to any one type of the above, it constitutes a separate type, only a null value, simply expressed as "no" empty "," or "value is unknown."

1 let image = null; 

 

"undefined"

undefined is also a self-contained class that represents not assigned.

If a variable is declared, it has not been assigned, then its value is undefined:

1 let i;
2 console.log(i) // undefined

 

object type

is a special type of object, all other data types are "native type", because they contain only a single value of the content, storing object data set are used and complex entity.

We can create an object by using braces with an optional list of attributes {???}. A property is a key-value pair ( "key: value"), in which the key ( key) is a string (also called property

Name), value ( value) can be any value.

1 let obj = {name: "jack", age: 18};
2 let obj2 = {country: "china", city: ["chengdu", "chongqing"]};
3 let obj3 = {country: "china", province: {"sichuan": "chengdu"}};

 

symbol

symbol is an identifier representing the values ​​are unique, can not be modified.

1 let s1 = Symbol(1);
2 let s2 = Symbol(1);
3 console.log(s1 === s2); // false

 

Guess you like

Origin www.cnblogs.com/feng-fengfeng/p/12370147.html
Recommended