JavaScript data types
1.Boolean
Boolean: (value type) var b1=true;//Boolean type
2.Number (number)
Value: (value type) var n1=3.1415926;//Numerical type
n1.toFixed(3);//Round and reserve 3 decimal places.
3.String (String)
The code copy is as follows:
var s1='hello';//String type
String: (value type, string immutable attribute)
4.Undefined (undefined)
Undefined belongs to the value type, and the result calculated from other values is not what we want, but is slightly different from null in the database, such as calculation with numbers or calculation with strings.
The Undefined type and the Null type are both data types with only one value, namely undefined and null.
5.Null (empty object)
6.Object (Object type)
Object is a reference type, and the others are basic data types.
String is also a basic type, and cannot add dynamic attributes to String, but can be used when referring to types.
Reference type object instanceof type, determine whether a value is a certain type, all reference types instanceof Object return is true
7. Application Type
Object: (reference type)
The code copy is as follows:
var tim=new Date();//Object type (object)
var names=['zs','ls','ww'];//Array is also an object type (object)
var obj=null;//object
Function: (reference type)
The code copy is as follows:
function fun(){ } //typeof(fun);//The output result is function, function type
PS: Use typeof (variable) to view the type of variable
Null and undefined in JavaScript
undefined, indicates an unknown state
The variable declared but not initialized, and the value of the variable is an unknown state (undefined). (Accessing non-existent properties or object window.xxx) When the method does not have an explicit return value, the return value is an undefined. When the typeof operator is applied to an undeclared variable, it is displayed as undefined (*)
null means an object that has not yet existed, null is a value with special significance.
You can assign a value to a variable to null, and the value of the variable is "known state" (not undefined), that is, null. (Used to initialize variables, clear variable content, and free memory)
undefined==null //The result is true, but the meaning is different.
undefined===null //false(*),PS: First determine whether the type is consistent, and then determine the value. ===Strictly equal, !==Strictly not equal
Since == converts the value into types and then determines whether it is equal, sometimes there may be unexpected results, so it is recommended to use ===. But note that in some cases, using == can bring better results.
Type conversion
The code copy is as follows:
parseInt(arg) converts the specified string into an integer
parseFloat(arg) converts the specified string into a floating point number
Number(arg) converts the given value (any type) into a number (can be an integer or a floating point); it converts the entire value, not a partial value. If the string cannot be completely converted to an integer, NaN is returned. (Not a Number)
isNaN(arg), determines whether arg is a non-number (NaN), and NaN and NaN are not equal.
String(arg) converts the given value (any type) into a string;
Boolean(arg) converts the given value (any type) into the Boolean type;
(*)eval(codeString) calculates and executes a string of js code.
The above is the data types and conversion methods of javascript. I hope everyone likes it.