javascript Basic Data Types and Transformations

  • 2021-08-05 08:02:08
  • OfStack

There are five basic data types in ECMAScript: Undefined, Null, Boolean, Number and String. There is also a complex data types-Object and Object, which are essentially composed of a set of unordered name-value pairs (key-value pairs). ECMAScript does not support any mechanism for creating custom types.

Because ECMAScript is loose, one means is needed to detect the data type of a variable, and typeof is the operator with this function. Detecting variables with typeof may return one of the following strings:

"undefined" 变量未定义
"boolean" 变量是布尔值
"string" 变量是字符串
"number" 变量是数值
"object" 变量是对象或者null
"function" 变量是函数

From a technical point of view, a function is an object in ECMAScript, not a data type. However, functions have some special properties, so it is necessary to distinguish functions from other objects by typeof.

The Undefined type has only one value, which is the special undefined. When a variable is declared with var but is not initialized, the value of this variable is undefined, such as:


var a;
alert(a == undefined);  //true

However, variables that contain undefined values are different from variables that have not been defined, such as:


var a; //  After this variable is declared, it is obtained by default undefined Value 
//  The following variable is not declared 
// var b
alert(a);  // "undefined"
alert(b);  //  Generate an error 

However, using typeof for undeclared or declared uninitialized variables returns undefined, such as:


var a;
// var b;
alert(typeof a); // "undefined"  
alert(typeof b); // "undefined"

The Null type also has only one value, which is null. From a logical point of view, the null value represents a null pointer, so detecting the null value with typeof will return "object", such as:


var car = null;
alert(typeof car); // "object"

Therefore, if you want to define a variable to hold an object, it is best to initialize the variable to null. In fact, undefined values are inherited from null values, so judging their equality returns true:

alert(null == undefined);     // true

Although null and undefined have such a relationship, their uses are completely different, because it is not necessary to set the value of a variable to undefined at any time. However, when defining an object variable that has not saved an object, the variable should be set to null, which can not only reflect null as a pointer to an empty object, but also distinguish null from undefined well.

The Boolean type has two literals: true and false, but values of all types in ECMAScript can be converted to values of Boolean by calling the Boolean () function. The following table lists the conversion rules for each data type:

数据类型 转换为true的值 转换为false的值
Boolean true false
String  任何非空字符串 ""空字符串
Number 任何非零数字值 0和NaN
Object 任何对象 null
Undefined / undefined

Number types are divided into integers and floating-point numbers. Integers can be expressed in decimal, octal or 106-ary, such as:


var num1 = 22; //10 Binary integer 
var num2 = 070; //8 Binary 56
var num3 = 079; //  Invalid 8 Binary system, parsed as 10 Binary system 79
var num4 = 08; // Invalid 8 Binary system, parsed as 10 Binary system 8
var num5 = 0xA; //106 Binary 10
var num6 = 0x1f; //106 Binary 31

However, octal literals are invalid in strict mode, and when arithmetic is performed, all values will eventually be converted to decimal values. Floating-point values must contain 1 decimal point, such as:


var floatNum1 = 1.1;
var floatNum2 = .1; // Effective, but not recommended 
var floatNum3 = 1.; // There is no number after the decimal point, which is resolved as 1
var floatNum4 = 10.0; // An integer that resolves to 10

The highest precision of floating-point values is 17 decimal places, but they are far less accurate than integers in arithmetic calculation, for example:


var a = 0.1;
var b = 0.2;
var c = a + b; //c The value of is 0.30000000000000004

NaN, that is, non-numerical value, is a special Number value. NaN has two characteristics: the result of any operation with NaN will return NaN, and NaN is not equal to any value, including NaN. The isNaN () function can be used to determine whether a value is NaN. When isNaN () receives a parameter, it will try to convert this value to a numeric value. Any value that cannot be converted to a numeric value will return true, such as:


alert(isNaN(NaN)); //true
alert(isNaN(10)); //false(10 Yes 1 Numeric value )
alert(isNaN("10")); //false( Can be converted to a numeric value 10)
alert(isNaN("abc")); //true( Cannot convert to numeric value )
alert(isNaN(true)); //false( Can be converted to a numeric value 1)
var obj = {name:"zhangsan", age:"1"};
alert(isNaN(obj)); //true

isNaN () can also convert objects. When an object calls isNaN (), it will first call the valueOf () method of the object, and then determine whether the return value of the method can be converted to a numeric value. If not, it will call the toString () method with this return value and then test the return value.

There are three methods for converting non-numerical values into numerical values: Number (), parseInt () and parseFloat (). Number () can convert values of any data type, while parseInt () and parseFloat () can only convert strings.

The Number () function has the following conversion rules:

1. If it is Boolean value, true is converted to 1 and false is converted to 0;


var num = Number(true); //1
var num2 = Number(false); //0

2. If it is an Number value, it is the same as the passed-in value 1;

var num = Number(1);    //1

3. If it is null, convert it to 0;

var num = Number(null);    //0

4. If it is undefined, convert it to NaN;

var num = Number(undefined);    //NaN

5. If it is String value, it should be divided into many cases. If it is an empty string, it will be converted to 0; If it is a pure numeric string, it is converted to a corresponding numeric value, if it is a number and contains ".", it is converted to a corresponding floating-point value (ignored if the front of the string is 0), and if the string is in a valid 106-ary format, it is converted to a corresponding 10-ary value; If the string contains characters other than the above format, it is converted to NaN;; If the string is an object, the object's valueOf () method is called first, and then the return value of that method is determined to be convertible to a numeric value. If the result is NaN, the toString () method is called, and the return value is tested.


var num = Number("Hello World"); //NaN
var num2 = Number(""); //0
var num3 = Number("01"); //1
var num4 = Number("01.1"); //1.1
var obj = {name:"zhangsan", age:"1"};
alert(Number(obj)); //NaN

parseInt () and parseFloat () are commonly used to convert strings because Number () is more complex in converting strings. When converting a string, these two functions detect whether the string conforms to the numeric pattern, parsing from the first non-space character, and returning NaN (including empty strings) if the first character is not a numeric or minus sign. If the first character is a string, continue parsing the following characters until all characters are parsed or non-numeric characters are encountered.

parseInt () can recognize various integer formats (decimal, octal, 106-ary). If the string begins with "0x" and is followed by a numeric character, it will be resolved to 106-ary, and if it begins with "0" and followed by a numeric character, it will be resolved to 8-ary (ECMAScript5 does not recognize octal, and will ignore the previous 0 and resolve it to 10-ary).


var a; //  After this variable is declared, it is obtained by default undefined Value 
//  The following variable is not declared 
// var b
alert(a);  // "undefined"
alert(b);  //  Generate an error 
0

To solve the compatibility problem, parseInt () provides the second parameter, in which numeric format it resolves.


var a; //  After this variable is declared, it is obtained by default undefined Value 
//  The following variable is not declared 
// var b
alert(a);  // "undefined"
alert(b);  //  Generate an error 
1

parseFloat () only recognizes the first decimal point, and the subsequent decimal point is invalid. At the same time, parseFloat () only recognizes the decimal value, so without the second parameter, other format values will be resolved to 0.


var a; //  After this variable is declared, it is obtained by default undefined Value 
//  The following variable is not declared 
// var b
alert(a);  // "undefined"
alert(b);  //  Generate an error 
2

String type values consist of a character sequence of several Unicode characters, which can be represented by single quotation marks ('') or double quotation marks (""), but the left and right quotation marks must match.


var str1 = "abc";
var str2 = 'abc';<br>var str3 = "abc'; // Syntax error 

There are two ways to explicitly convert a value to a string, toString () and String (). Numeric values, Boolean values, objects, and strings all have toString () and String () methods, while undefined and null only have String () methods, and the parameters of toString () are converted in binary format.


var a; //  After this variable is declared, it is obtained by default undefined Value 
//  The following variable is not declared 
// var b
alert(a);  // "undefined"
alert(b);  //  Generate an error 
4

Related articles: