Home  >  Article  >  Web Front-end  >  Detailed explanation of the usage of six major data types in javascript basic tutorial

Detailed explanation of the usage of six major data types in javascript basic tutorial

伊谢尔伦
伊谢尔伦Original
2017-07-18 16:20:571538browse

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

NaN: Non-numeric type. Features: ① Any operation involving NaN will return NaN ② NaN is not equal to itself.


var ab = "a1";
console.log(ab/10);// NaN
console.log(NaN == NaN);// false;

isNaN() function is used to determine whether it is a non-numeric type. If the parameter passed in is a non-numeric type, then return true; otherwise, return false;

isNaN() function, when a parameter is passed in, the function will first convert the parameter into a numerical value.

If the parameter type is an object type, the valueOf() method of the object will be called first, and then it will be determined whether the value returned by the method can be converted to a numeric type. If not, call the object's toString() method again and determine the return value.

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);//5

The 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”;

The above code first creates a string that can hold 10 characters, then fills the string with java and script strings, and finally destroys the original String java and scrip string, because these two strings are useless at this time. This process happens in the background.

String conversion: Transformation function String(), applicable to any data type (null, undefined will be converted to null and undefined); toString() method (null, defined does not have toString() method).


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 false

Convert 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没有声明

In the above code, although age is declared, there is no initialization value, so what is printed is undefined. The variable height is not declared, so an error is reported.

But when using the typeof operator to detect the data type, the variable age is not initialized, and the result is undefined. The type detected by height without a declared variable is also undefined.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn