There are two types of JavaScript data types: primitive (that is, primitive or value types) and object (that is, reference data types)

  • Basic data types: String, Number, Boolean, Null, Undefined, Symbol
  • References to data types (Object types), such as objects, arrays, functions, etc

⚠️ Note: Symbol is ES6’s introduction of a new primitive data type that represents unique values

The data type

classification type
Base type (value type) undefined Undefined type
null Empty type
string String type
number Numeric types
boolean Boolean value type
bigint A larger range of integer values
Symbol Represents a unique value
Reference data type Object Object type
Array An array type
Function Function types

Undefined and null

Undefined Indicates that the variable has no value. You can empty a variable by setting its value to NULL

var person
var car = lamborghini
console.log(person)
console.log(car)

var car = null
console.log(car)

/* undefined undefined null */
Copy the code

JavaScript has dynamic typing

JavaScript has dynamic typing. This means that the same variable can be used for different types:

var x;               / / x is undefined
var x = 5;           // Now x is a number
var x = "John";      // now x is a string
Copy the code

JavaScript string

Strings are variables that store characters such as “Bill Gates”. The string can be any text in quotes. You can use single or double quotation marks:

var carname1="Volvo XC60";
var carname2='Volvo XC60';
Copy the code

You can use quotation marks in strings as long as they do not match the quotation marks surrounding the string:

var answer1='It\'s alright';
var answer2="He is called \"Johnny\"";
var answer3='He is called "Johnny"';
Copy the code

Try it »

JavaScript digital

JavaScript has only one numeric type. Numbers may be numbered with or without a decimal point:

var x1=34.00;      // Write with a decimal point. Other languages call it floating-point
var x2=34;         // Do not use the decimal point to write
Copy the code

Do not verify decimals with decimals

var x = 0.1; var y = 0.2;
var num = x + y;
console.log(num == 0.3) //false
Copy the code

The greatest or smallest numbers can be written by scientific (exponential) notation:

var y=123e5;      / / 12300000
var z=123e-5;     / / 0.00123
Copy the code

Base and extremum writing:

var num = 1                // An integer equivalent to an int in strongly typed languages
var binaryNum = 0b0110011  / / binary
var octalNum = 015         / / octal
var decimalNum = 15        / / decimal
var hexadecimal = 0x15     // Hexadecimal
console.log(num, binaryNum, octalNum, decimalNum, hexadecimal); //1 51 13 15 21

console.log(Number.MIN_VALUE, Number.MAX_VALUE) // The minimum number MIN_VALUE and the maximum number MAX_VALUE
Copy the code

Infinity infinity, infinity infinity

var a = 1 / 0                      // Divide by 0 Infinity
var b = 10000支那100000支那100000  // The maximum precision is Infinity

var c = 10000e10 
var d = 10000000000000000000000000000000000n 

console.log("a=%s, b=%s, c=%s d=%s",a, b, d);

/* a = Infinity, b = Infinity, c = 10000000000000000000000000000000000n d =%s */
Copy the code

NaN meaning :no an number, cause of NaN:

parseInt("我")     Error converting decimal or integer
var a = -"b"       // Subtract a non-numeric character
var num = NAN      // Self-assign
var num; num + 0;  // No assigned variable + number =NaN
1/0 - 1/0          // infinity minus infinity
Copy the code

JavaScript Boolean

Boolean (logic) can have only two values: true or false

var x=true  
var y=false
Copy the code

Boolean is often used for relational comparisons in conditions, converted to numbers true = 1 flase = 0

JavaScript array

The following code creates an array named cars:

var cars1 = new Array()
cars1[0] = "Saab"
cars1[1] = "Volvo"
cars1[2] = "BMW"

// Or (Condensed array):
var cars2 = new Array("Saab"."Volvo"."BMW");

// (literal array):
var cars3 = ["Saab"."Volvo"."BMW"];
Copy the code

Array subscripts are zero-based, so the first item is [0], the second is [1], and so on. Try »

JavaScript object

Objects are separated by curly braces. Inside parentheses, the attributes and values of the object are defined as key-value pairs (name: value). Properties are separated by commas:

var person={firstname:"John".lastname:"Doe".id:5566};
Copy the code

The object (person) in the above example has three attributes: firstName, lastName, and ID whitespace and folds don’t matter. Statements can span multiple lines:

var person={
firstname : "John".lastname  : "Doe".id        :  5566
};
Copy the code

Object attributes can be addressed in two ways:

name=person.lastname;
name=person["lastname"];
Copy the code

JavaScript variables are all objects. When you declare a variable, you create a new objectTry it »

Symbol

Symbol is a primitive data type. The Symbol() function returns a value of type Symbol, which has static attributes and static methods. Its static attributes expose several built-in member objects; Its static method exposes the global Symbol registry and is similar to the built-in object class, but as a constructor it is incomplete because it does not support the syntax: “new symbol ()”

Each Symbol value returned from Symbol() is unique. A symbol value can be used as an identifier for object properties; This is the only purpose of this data type. See Glossary Entry for Symbol for a further explanation

const symbol1 = Symbol(a);const symbol2 = Symbol(42);
const symbol3 = Symbol('foo');

console.log(typeof symbol1);
// expected output: "symbol"

console.log(symbol2 === 42);
// expected output: false

console.log(symbol3.toString());
// expected output: "Symbol(foo)"

console.log(Symbol('foo') = = =Symbol('foo'));
// expected output: false
Copy the code

grammar

Symbol([description])
Copy the code

Description Optional The value is a string. This parameter is optional. A description of a symbol that can be used to debug but not access the symbol itself

Declaring variable types

When you declare a new variable, you can use the keyword “new” to declare its type:

var carname=new String;
var x=      new Number;
var y=      new Boolean;
var cars=   new Array;
var person= new Object;
Copy the code

Console. log Supported format flag

A placeholder describe
%s string
%d   %i The integer
%f Floating point Numbers
%o/  %O object
%c CSS styles
% % Console. log(“%%”) // Output :%
Console. log(“%%”,” ZZK “) // Output :% ZZK