This article mainly introduces the basic data types and conversions of javascript, which has a good reference value. Let’s take a look at it with the editor
ECMAscript There are 5 basic data types in: Undefined, Null, Boolean, Number, and String. There is also a complex data type - Object. Object is essentially composed of a set of unordered name-value pairs (key-value pairs). ECMAscript does not support any mechanism for creating custom types.
Since ECMAscript is loosely typed, a means is needed to detect the data type of the variable. Typeof is the operator with this function. Using typeof to detect variables may return one of the following strings:
"undefined" | Variable is undefined |
"boolean" | The variable is a Boolean value |
"string" | The variable is a string |
"number" | The variable is a numerical value |
"object" | The variable is an object or null |
"function" | Variables are functions |
#From a technical point of view, functions are objects in ECMAscript, not a type of data. However, functions have some special properties, so it is necessary to use typeof to distinguish functions from other objects.
The Undefined type has only one value, which is the special undefined. When a variable is declared using var but not initialized, the value of the variable is undefined, such as:
var a; alert(a == undefined); //true
However, variables containing undefined values are different from undefined variables, such as:
var a; // 这个变量声明之后默认取得了undefined值 // 下面这个变量并没有声明 // var b alert(a); // "undefined" alert(b); // 产生错误
However, using typeof on undeclared or uninitialized variables will return undefined, such as:
var a; // var b; alert(typeof a); // "undefined" alert(typeof b); // "undefined"
The Null type has only one value, which is null. From a logical point of view, a null value represents a null pointer, so using typeof to detect a null value will return "object", such as:
var car = null; alert(typeof car); // "object"
So if you want todefine a variable to store the object, It's best to initialize this variable to null. In fact, the undefined value inherits from the null value, so judging their equality will return true:
alert(null == undefined); // true
Although null and undefined have such a relationship, their uses are completely different, because whenever There is no need to explicitly set the value of a variable to undefined. However, when defining an object variable that has not yet saved the object, the variable should be set to null. This not only reflects null as a pointer to an empty object, but also can It's good to distinguish between null and undefined.
The Boolean type has two literal values: true and false, but all types of values in ECMAscript can be converted into Boolean type values by calling the Boolean() function. The following table lists various data Conversion rules corresponding to the type:
Data type | Convert to the value of true | Convert to the value of false |
Boolean | true | false |
String | Any non-empty string | ""Empty string |
Number | Any non-zero numeric value | 0 and NaN |
Object | Any object | null |
Undefined | / | undefined |
Number类型分为整数和浮点数,整数可以用十进制,八进制或十六进制表示,如:
var num1 = 22; //十进制整数 var num2 = 070; //八进制的56 var num3 = 079; // 无效的八进制,解析为十进制79 var num4 = 08; //无效的八进制,解析为十进制8 var num5 = 0xA; //十六进制的10 var num6 = 0x1f; //十六进制的31
但是八进制字面量在严格模式下是无效的,在进行算数计算时,所有的数值最终都会转换为十进制数值。浮点数值必须包含一个小数点,如:
var floatNum1 = 1.1; var floatNum2 = .1; //有效,但不推荐 var floatNum3 = 1.; //小数点后面没有数字,解析为1 var floatNum4 = 10.0; //整数,解析为10
浮点数值的最高精度是17位小数,但在进行算数计算时精确度远不如整数,例如:
var a = 0.1; var b = 0.2; var c = a + b; //c的值为0.30000000000000004
NaN,即非数值,是一个特殊的Number值,NaN有两个特点:任何和NaN操作的结果都会返回NaN,NaN与任何值都不相等,包括NaN。使用isNaN()函数可以判断一个值是不是NaN,isNaN()在接收到一个参数时,会尝试将这个值转换为数值,任何不能转换为数值的值都会返回true,如:
alert(isNaN(NaN)); //true alert(isNaN(10)); //false(10是一个数值) alert(isNaN("10")); //false(可以被转换为数值10) alert(isNaN("abc")); //true(不能转换为数值) alert(isNaN(true)); //false(可以转换为数值1) var obj = {name:"zhangsan", age:"1"}; alert(isNaN(obj)); //true
isNaN()也能转换对象,对象调用isNaN()时,会首先调用对象的valueOf()方法,然后确定该方法的返回值是否可以转换为数值,如果不能,则用这个返回值再调用toString()方法,再测试返回值。
非数值转换成数值的方法有三个:Number()、parseInt()、parseFloat()。Number()可以转换任何数据类型的值,而parseInt()和parseFloat()只能转换字符串。
Number()函数有以下转换规则:
1.如果是Boolean值,true转换为1,false转换为0;
var num = Number(true); //1 var num2 = Number(false); //0
2.如果是Number值,就和传入的值一样;
var num = Number(1); //1
3.如果是null,转换为0;
var num = Number(null); //0
4.如果是undefined,转换为NaN;
var num = Number(undefined); //NaN
5.如果是String值,要分多种情况,如果是空字符串,则转换为0;如果是纯数字的字符串,则将其转换为相对应的数值,如果字符串是数字且包含".",则将其转换为对应的浮点数值(如果字符串最前面是0,会被忽略),如果字符串是有效的十六进制格式,会将其转换为对应的十进制数值;如果字符串包含上述格式之外的字符,则转换为NaN;如果字符串是对象,会首先调用对象的valueOf()方法,然后确定该方法的返回值是否可以转换为数值,如果结果是NaN,则调用toString()方法,再测试返回值。
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
由于Number()在转换字符串是比较复杂,所以转换字符串常用parseInt()和parseFloat()。这两个函数在转换字符串时,会检测该字符串是否符合数值模式,从第一个非空格字符开始解析,如果第一个字符不是数值或者负号,则返回NaN(包括空字符串)。如果第一个字符是字符串,则继续解析后面的字符,直到解析完所有的字符或者遇到非数字字符。
parseInt()能够识别各种整数格式(十进制、八进制、十六进制),如果字符串以"0x"开头且后跟数字字符,就会被解析为十六进制,如果以"0"开头且后跟数字字符,则会被解析为八进制(ECMAscript5不识别八进制,会将前面的0忽略,解析为十进制)。
var num = parseInt("123Hello"); //123 var num2 = parseInt(""); //NaN var num3 = parseInt("0xA"); //10(十六进制) var num4 = parseInt("22.3"); //22 var num5 = parseInt("070"); //56(ECMAscript3八进制) 70(ECMAscript5十进制) var num6 = parseInt("23"); //23(十进制)
为了解决兼容性问题,parseInt()提供第二个参数,以何种数值格式解析。
var num1 = parseInt("0xAF", 16); //175 var num2 = parseInt("AF", 16); //175,可以省略前面的"0x" var num3 = parseInt("10", 2); //2(二进制) var num3 = parseInt("10", 8); //8(八进制) var num3 = parseInt("10", 10); //10(十进制) var num3 = parseInt("10", 16); //16(十六进制)
parseFloat()只识别第一个小数点,后面的小数点就无效了,同时parseFloat()只识别是十进制值,所以没有第二个参数,别的格式数值会被解析为0。
var num = parseFloat("123Hello"); //123 var num = parseFloat("0xA"); //0 var num = parseFloat("12.1"); //12.1 var num = parseFloat("12.1.1"); //12.1 var num = parseFloat("023"); //23 var num = parseFloat("1.1e3"); //1100
String类型值由若干个Unicode字符组成的字符序列构成,可以由单引号('')或者双引号("")表示,但是左右引号必须匹配。
var str1 = "abc"; var str2 = 'abc';<br>var str3 = "abc'; //语法错误
将一个值显式转换为字符串有两种方法,toString()和String(),数值、布尔值、对象和字符串都有toString()方法和String()方法,而undefined和null只有String()方法,toString()的参数是转换的进制格式。
var num = 10; alert(num.toString()); //"10" alert(num.toString(2)); //"1010" alert(num.toString(8)); //"12" alert(num.toString(10)); //"10" alert(num.toString(16)); //"A" alert(true.toString()); //"true" String(num); //"10" String(true); //"true" String(null); //"null" var num1; String(num1); //"undefined"
The above is the detailed content of Detailed introduction to JavaScript basic data types and conversion example codes. For more information, please follow other related articles on the PHP Chinese website!