This article briefly introduces the javascript data types and their direct conversion methods. Although there are not many examples, it is also a summary of my personal experience. We recommend it to you here.
JavaScript Data Type
1. Boolean (Boolean)
Boolean: (Value Type) var b1 = true; // Boolean Type
2. Number (Number)
Value: (Value Type) var n1 = 3.1415926; // Value Type
N1.toFixed (3); // The value is rounded to three decimal places.
3. String (String)
The Code is as follows:
Var s1 = 'hello'; // string type
String: (value type, character string immutable)
4. Undefined (Undefined)
Undefined is a value type. It is slightly different from null in the database, for example, numeric calculation or string calculation.
The Undefined and Null types are data types with only one value, which are undefined and null respectively.
5. Null (Null Object)
6. Object (Object type)
Object is a reference type, and others are basic data types.
String is also a basic type. dynamic attributes cannot be added for String, but can be referenced for type.
Reference Type Object instanceof type, judge whether a value is of a certain type, all reference type instanceof Object returns true
7. Application Type
Object: (reference type)
The Code is as follows:
Var tim = new Date (); // object type (object)
Var names = ['zs', 'Ls', 'ww ']; // The array is also an object type (object)
Var obj = null; // object
Function: (reference type)
The Code is as follows:
Function fun () {}// typeof (fun); // The output result is a function, function Type
PS: view the type of a variable using typeof (variable)
Null and undefined in JavaScript
Undefined, indicating an unknown state
This variable is declared but not initialized. Its value is undefined ). (Access to non-existent properties or object window. xxx) if the method does not explicitly return a value, the return value is undefined. When the typeof operator is applied to undeclared variables, it is displayed as undefined (*)
Null indicates an object that does not exist. null is a special value.
You can assign a value to null to a variable. The value of the variable is "known" (not undefined), that is, null. (Used to initialize the variable, clear the variable content, and release the memory)
Undefined = null // The result is true, but the meaning is different.
Undefined = null // false (*), PS: first judge whether the type is consistent, then determine the value. === Strictly equal ,! = Strictly not equal
Because = will convert the value type before determining whether it is equal, sometimes unexpected results may exist, so we recommend using =. However, in some cases, = can bring better results.
Type conversion
The Code is as follows:
ParseInt (arg) converts a specified string to an integer.
ParseFloat (arg) converts a specified string to a floating point number.
Number (arg) converts a given value (of any type) to a Number (which can be an integer or floating point Number). It converts the entire value instead of a partial value. If the string cannot be fully converted to an integer, NaN is returned. (Not a Number)
IsNaN (arg) is used to determine whether arg is a non-digit (NaN), and NaN are not equal.
String (arg) converts a given value (of any type) to a String;
Boolean (arg) converts a given value (of any type) to Boolean;
(*) Eval (codeString) computes and executes the js Code of a string.
The above is the javascript data type and conversion method. I hope you will like it.