Javascipt, that you do not know yet——Data Type

All knowledge comes from the 2rd Edition.

There are 5 simple data types in ECMAScript (also named as basic data type): Undefined, Null, Boolean, Number, String.


Another complex data type, that's Object, Object is made up by a series of unordered key-values in nature. ECMAScript doesn't support any user-defined type, all value are above 6 data types at the end.

  • Operator: typeof

Because ECMAScript is interpreted and loosely typed, so we need a method that we can check the specified variable's data type, that's it, the typeof provides that functionality, we use typeof operator for checking a variable, and it may return one of the following string list.


'undefined': the variable isn't defined.
'boolean': the variable is a Boolean.
'string': the variable is a String.
'number': the variable is a Number.
'object': the variable is a String.

'function': the variable is a function.


Attention please, typeof is an operator, not a function, so brackets aren't mandatory, you can use it like the following case:

var message = 'some string';
alert(typeof message);

  • Undefined type

Undefined type only has one value, it's undefined, and when we declare a variable, but don't give any value, the variable's value is undefined.


So we have no need to declare a variable and give it undefined explicitly. Its significance is used for comparing; we can distinguish null object pointer and uninitialized variables.


Of course, that variable contains undefined value is different from undefined variable, for example:
var message;    // declare a variable, its value is undefined.
alert(message);    // it'll show undefined.
Alert(age);        // it'll make error.


That above code, the first warning box will display the message variable's value, it's 'undefined'. But for the second warning box, it'll make an error.


For undefined variable, we can only do an operation, that's use typeof for checking its data type.


But, misleadingly, the result for checking uninitialized variable is undefined, and the result for checking undefined variable is also undefined, that's confused! See the following sample:


var message;
alert(typeof message);    // undefined
alert(typeof(age));    // undefined


In this case, if I want to judge whether a variable is uninitialized or is undefined, what should we do?


The answer is typeof can't give correct judgment, but we can solve it by convention.


Even uninitialized variable will be assigned undefined value automatically, we still suggest that we finish value assignment operator when we declare variable. If we keep this habit, in that way, when typeof operator returns undefined value, we will know the variable is undefined, instead of uninitialized.

  • Null type

Null type is the second one which only contains one value, that only value is Null. From a couple of logic perspectives, null value means null object pointer, that's why its return value of checking type is 'object' via typeof operator.


Actually, undefined value derives from null value, so ECMA-262 rules their parity testing should be true.


Though null and undefined have such a relationship, but their uses are totally different.


As I said before, in any case, we shouldn't declare a variable and assign as undefined, but we could assign as null, so that we can keep a convention, that's null as a null object pointer, at the same time, it helps us distinguish null and undefined.


  • Boolean Type

Boolean type is one of the commonest type in ECMAScript, it contains two values:trueandfalse. you should pay attention please: these two values are case sensitive, True and False aren't value of Boolean Type, they are Identifiers, and 1 & 0 are also equal to true & false.

Although Boolean type only has two values, but all types' values can convert to Boolean type, you can use the Boolean() function for converting, for example:

var message = 'Hello world';
var messageAsBoolean = Boolean(message);

The following grid provides a conversion rule between all data type and Boolean type.

Data Type Value will be convert as true Value will be convert as false
Boolean true false
String all string are not null '' (it means a string without any character)
Number all number are not equal to 0 0 or NaN
Object all object except null null
Undefined n/a (it means not applicable) undefined

These rules help us understand some process control sentences, please see the following code:

var message = 'Hello word!';
if(message){
    alert('Value is true');
}

Run the above code, it'll pop up a warning message box, that's because the message variable is changed to Boolean value before comparing.


  • Number Type

Number type is the most concerned data type in ECMAScript, it uses IEEE754 format to describe integer & float. ECMAScript defines some different literals format.

The most basic format is decimal integer, for example:

var intNum = 98;
Except for decimal integer description, the integer also could describe by octal number & hexadecimal number.

And the octal number should be started with 0, and could only contain that number between 0 and 7, if it doesn't observe that rule, it'll be regarded as decimal integer, for example:

var octalNum1 = 070;    // octal number:56
var octalNum2 = 079;    // invalid octal number, it'll be regarded as 79
var octalNum3 = 08;     // invalid octal number, it'll be regarded as 8

Hexadecimal number should be started with 0x, and could only contain number (0~7) and character (A~Z), never mind their case, fox example:

var hexNum1 = 0xA;    // it's equal to 10 (decimal system).
var hexNum2 = 0x1f;    // it's equal to 31 (decimal system).

  • String Type

String type is used to describe that char sequence makes up by zero or a couple of 16-bit Unicode char, it's string, it could express by ' or ", the following two style of writing are both correct:

var firstName = "Nicholas";
var lastName = 'Zakas';

But if a string starts with ' ("), and must end with ' (").

String literal

String type contains some specific string literal, also named transferred meaning sequence, they are used to express non-printing characters.

Literal Meaning
\n Line feed
\t Tabulation
\b Blank space
\r Enter
\f Form feed
\\ Slash
\' Single quotes
\" Double quotation marks
\xnn Express a character via hexadecimal code
\unnnn Express an Unicode character via hexadecimal code

These string literals could place at random location of a string, and will regard as a character, for example:

var text = "This is the letter sigma: \u03a3.";

the '\u03a3' will be interpreted as Σ.

Characteristic of string

String is unchanged in ECMAScript, in other words, its value won't be changed after creation, if you want to change a variable's value, the js run-time environment will destroy the last value, and create a new string for filling this variable.


  • Object Type

In ECMAScript, Object is a group of data and function's collection. Object could be created by thenew operator, for example:

var o = new Object();

You also could omit () behind object name, but we don't suggest that you omit the ; sign.

var o = new Object;

Every instance of Object contains the following properties and functions:

  1. constructor - It's used to save the creation function, for the above case, constructor is equal to Object().
  2. hasOwnProperty(propertyName) - It's used to check whether the specified property name exists in the object or not.
  3. isPrototypeOf(object) - It's used to check whether the specified object is the object's prototype or not.
  4. propertyIsEnumerable(propertyName) - It's used to check whether the specified property could use for-in sentence or not.
  5. toString() - return its string description of the object.
  6. valueOf() - return its string, number or boolean description of the object.

because all objects are based on the Object in ECMAScript, so all objects have these above properties and functions.


=======================================================

ECMAScript中有五种简单数据类型(也称为基本数据类型):Undefined, Null, Boolean, Number, String.

另外一个复杂数据类型——Object,Object本质上由一系列无序键值对组成。ECMAScript不支持任何用户自定义类型,所有的值最终都是以上的六种数据类型。

  • 操作符:typeof

因为ECMAScript是松散类型的,因此我们需要一个方法来检测变量的数据类型——typeof就是负责提供这方面信息的操作符。我们使用typeof操作符检查一个变量,它可能返回以下列表中的一项。

“undefined”: 这变量是未定义的

“boolean”:这个值是布尔值


你可能感兴趣的:(Learning,English)