There are six data types in js, including five basic data types (Number, String, Boolean, Null, Undefined), and one mixed data type (Object).
Variables in js are loosely typed, so sometimes we need to detect the data type of the variable.
1.Number type
The Number type contains two values: integers and floating point numbers (floating point numbers must include a decimal point and at least one digit after the decimal point).
Floating point numbers will be automatically converted to integers.
var num = 1.00; console.log(num);//1,自动转换为整数
The highest precision of floating point numbers is 17 digits. Looking at the example below, the result is not 0.3. As for other programming languages, this will also happen (about floating point numbers). Point calculation will cause rounding errors, and similar situations will occur in some other programming languages)
##
var num1 = 0.1; var num2 = 0.2; console.log(num1 + num2);//0.30000000000000004
var ab = "a1"; console.log(ab/10);// NaN console.log(NaN == NaN);// false;
Numeric conversion:
Number() transformation function, which can be used for any data type; parseInt(), Convert the value to an integer type, which is often used; parseFloat(); Convert the value to a floating point type.2.String type
The single quotes and double quotes in the string type have exactly the same effect. Strings have length attributes. You can get the length of the string. var str = “hello”;console.log(str.length);//5The value of the string is immutable. To change the value of a string, first destroy the original string and then fill it with another string containing the new value.var lang = “java”; lang += “script”;
var bc = "lijinwen"; var bd = null; var be = undefined; console.log(bc.toString()); //console.log(bd.toString());//error 报错 //console.log(be.toString());//error 报错 console.log("------"); console.log(String(bc)); console.log(String(bd)); console.log(String(be));
3.Boolean type
This type has only two values, true and falseConvert to boolean value: Transformation function Boolean() converts a value to Boolean type. Details will be added later.4.Null type
The null type is regarded as a null object pointer. As mentioned above, the null type is also a null object reference. There is only one value, the null value, so when you use the typeof operator to detect a value of type null, the result is of type object. If you define a variable, but want to use the variable as an object later, it is best to initialize the object to a null value.5.Undefined type
has only one value, the undefined value. If a variable is declared using var, but the variable is not initialized, the value of the variable is undefined.var name = "lijinwen"; var age; console.log(name);//lijinwen console.log(age);//undefined //console.log(height);//error,变量height没有声明 console.log(typeof name);//string console.log(typeof age);//undefined console.log(typeof height);//undefined,变量height没有声明
6.Object An object in type
js is a collection of properties and methods. The specific creation method of objects and various characteristics of objects will be introduced in detail in the later chapters of reference types. Here is a brief introduction to Object. among the six major data types ①Constructor attribute: Constructor attribute, which can determine the constructor of the current object.var o = new Object(); console.log(o.constructor == Object);//true var arr = new Array(); console.log(arr.constructor == Array);//true
The above is the detailed content of Detailed explanation of the usage of six major data types in javascript basic tutorial. For more information, please follow other related articles on the PHP Chinese website!