type of data
There are five kinds of ECMAScript simple data types (also referred to as basic data Undefined
types):Null
, Boolean
, , Number
and String
. There are one kind of complex data types - Object
, Object
essentially by a set of name-value pairs of unordered.
typeof operator
typeof
Detecting the type of a given variable data
var message = "some string";
alert(typeof message); // "string"
alert(typeof(message)); // "string"
alert(typeof 95); // "number"
Undefined type
Undefined
Only one type of value, that is special undefined
.
var message;// 在使用 var 声明变量但未对其加以初始化时,这个变量的值就是 undefined
alert(message == undefined); //true
The following example uses undefined
values explicitly initialized variables message
. But we do not need to do that, because the value of uninitialized default will get undefined
value.
var message = undefined;
alert(message == undefined); //true
However, that contains undefined
the variable value of the variable is not defined or is not the same.
var message; // 这个变量声明之后默认取得了 undefined 值
// 下面这个变量并没有声明
// var age
alert(message); // "undefined"
alert(age); // 产生错误
Running the above code, the first warning box displays the variable message
values, ie "undefined"
. While the second alert box - Since the transfer to alert () function is a variable has not been declared age - will result in an error. For variable has not been declared, you can only perform an action, that the use of typeof
operators to detect the type of data (variables without declaration calls delete
will not cause an error, but this is no practical significance, but also in strict mode does cause error).
Confusingly: Do typeof operator returns the value undefined, while typeof operator to perform undeclared variables will also return undefined value of uninitialized variables. Consider the following example:
var message; // 这个变量声明之后默认取得了 undefined 值
// 下面这个变量并没有声明
// var age
alert(typeof message); // "undefined"
alert(typeof age); // "undefined"
The results showed that performs typeof operator and uninitialized variables undeclared undefined values are returned; the result of which there is a reasonable logic. Because although these two variables from a technical point of view are essentially different, but in fact no matter it is impossible to perform the actual operation of the kind of variable.
Null Type
Null data type of the second type is only one value, the special value is null. From a logical point of view, a null value indicates a null object pointer, which is also used typeof operator returns a null value detected reason "object" time.
var car = null;
alert(typeof car); // "object"
The undefined value is derived from a null value, test for equality to be returned to their true
alert(null == undefined); //true
Boolean type
Boolean type only one word par value: true and false.
Other types of data can be converted into a Boolean
Number Type
Number types include integer and floating point values
The first octal literal must be zero (0), then the sequence of octal digits (0 to 7). If the value literals beyond the range, leading zeros are ignored, the latter value will be treated as a decimal numerical analysis.
var octalNum1 = 070; // 八进制的 56
var octalNum2 = 079; // 无效的八进制数值——解析为 79
var octalNum3 = 08; // 无效的八进制数值——解析为 8
Octal literals in strict mode is invalid, it will lead to support JavaScript engine throws an error.
The first two hexadecimal literals must be 0x
String type
String type is used to represent a character sequence of zero or more 16-bit Unicode characters, i.e., the string. String can be represented by a single or double quotation marks:
var aa = "string";
var bb = 'string';
ECMAScript string indicated by double quotes and exact string represented by single quotes.
Character literals
String data type contains special character literals, also known as escape sequences for representing non-printing characters, or characters having other purposes. These character literals in the following table:
Length of the string can be made by accessing its length property.
var text = "\u03a3";
alert(text.length);
Examples of the above results
It can be seen \u03a3
six-character sequence representing an escape character.
Features of the string
ECMAScript strings are immutable, that is, the string once created, their values can not be changed. To change the string stored in a variable, you must first destroy the original string, and then use another string containing the new value of the variable filling.
Into a string
toString () method
numeric, boolean, string, and the object value (yes, each string also has a toString () method, which returns a copy of the string) are toString () method. But this value is not null and undefined method.
In most cases, calls the toString () method does not have to pass parameters. However, the value of the call toString () method when, can pass one parameter: the base output value. By default, toString () method returns the value represented by a string in decimal format. Passing through the base, toString () may be output in binary, octal, hexadecimal, binary string as well as any other effective value of the format.
var age = 11;
var ageAsString = age.toString(); // 字符串"11"
var found = true;
var foundAsString = found.toString(); // 字符串"true"
Transformation function String ()
This function can be converted to a string value of any type
1, if toString () value method, the method is called (no parameters) and return the results;
2, if the value is null, "null" is returned;
3, if the value is undefined, "undefined" is returned.
var value1 = 10;
var value2 = true;
var value3 = null;
var value4;
alert(String(value1)); // "10"
alert(String(value2)); // "true"
alert(String(value3)); // "null"
alert(String(value4)); // "undefined"
Put a value into a string, the operator can use the plus (discussed in Section 3.5) it with a character string ( "") together.
Object Types
It is actually a collection of data and functionality in a set of ECMAScript objects. Objects can be created by the implementation of the object type the name of the new operator followed to create. Object types created instance and add properties and (or) method, you can create custom objects.
That in ECMAScript, (like java.lang.Object objects in Java) Object type is the basis for all of its instances. In other words, any of the properties and methods of type Object has also present in a more specific object.
Each Object instance has the following attributes and methods:
constructor: Save the function used to create the current object. For the previous example, the constructor (constructor) is Object ().
hasOwnProperty (propertyName): checking for a given attribute in the current object instance (rather than in the prototype instance) exists. Wherein the property name must be specified as a parameter (propertyName) a string (for example: o.hasOwnProperty ( "name")).
var o = new Object();
o.name = 'js';
o.hasOwnProperty('name');
o.hasOwnProperty('age');
isPrototypeOf (object): Object is used to check whether incoming incoming object prototype.
propertyIsEnumerable (propertyName): used to check whether a given property can be used for-in statement (discussed later in this chapter) to enumerate. And hasOwnProperty () method of the same, must be specified as a string property name parameter.
o.propertyIsEnumerable('name'); // true
toLocaleString (): Returns a string representation of the object, the region corresponding to the string and execution environment.
var born = new Date("July 21, 1983 01:15:00");
console.log(born.toLocaleString());
var arr = new Array(3);
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
console.log(arr.toLocaleString());
toString (): Returns a string representation.
valueOf (): returns the object string, or Boolean values represented. Generally toString () Returns the value of the same method.
var colors = ["red", "blue", "green"]; // 创建一个包含3 个字符串的数组
alert(colors.toString()); // red,blue,green
alert(colors.valueOf()); // red,blue,green
alert(colors); // red,blue,green