JavaScript-Primitive Data Types(原始数据类型)

JavaScript-Primitive Data Types(原始数据类型)


Primitive data types

Any value that you use is of a certain type. In JavaScript, there are just a few primitive data types:

1. Number: This includes floating point numbers as well as integers. For example, these values are all numbers: 1, 100, 3.14.

2. String: These consist of any number of characters, for example "a", "one", and "one 2 three".

3. Boolean: This can be either true or false.

4. Undefined: When you try to access a variable that doesn't exist, you get the special value undefined. The same happens when you declare a variable without assigning a value to it yet. JavaScript initializes the variable behind the scenes with the value undefined. The undefined data type can only have one value – the special value undefined.

5. Null: This is another special data type that can have only one value, namely the null value. It means no value, an empty value, or nothing. The difference with undefinedis that if a variable has a value null, it's still defined, it just so happens that its value is nothing. You'll see some examples shortly.

Any value that doesn't belong to one of the five primitive types listed here is an object. 

Even null is considered an object, which is a little awkward(尴尬的)—having an object (something) that is actually nothing. We'll learn more on objects in Chapter 4, Objects, but for the time being, just remember that in JavaScript the data types are either:

  • Primitive (the five types listed previously)

  • Non-primitive (objects)


typeof操作符

typeof的返回值有六种可能:number、string、boolean、object、function、undefined

///////////////////////////////////////////////////////////
//typeof的返回值有六种可能:number、string、boolean、object、function、undefined。
console.log(typeof new Object()); //object
console.log(typeof 123);//number
console.log(typeof function () {
});//function
function hello() {
    alert("say hello~~");
}
console.log(typeof hello); //function
console.log(typeof "hello"); //string
console.log(typeof romantic);//undefined
var good = null;
console.log(typeof good); //object
console.log(typeof true); //boolean


Octal and hexadecimal numbers(八进制和十六进制数字)

var n1 = 0377; //八进制
console.log(n1); //十进制的255
console.log(typeof n1); //number

var n2 = 0x00;
console.log(n2); //十进制的0
console.log(typeof n2); //number

八进制以0开头

十六进制以0x开头


Exponent(指数) literals

1e1(also written as 1e+1or 1E1or 1E+1) represents the number one with one zero after it, or in other words, 10.Similarly, 2e+3means the number 2 with 3 zeros after 

it, or 2000:

console.log(1e1);//10
console.log(1e+1);//10
console.log(2e+3);//2000
console.log(2e-3);//0.002
console.log(typeof 2e-3);//number
console.log(typeof 2e+3);//number


A special value in JavaScript called Infinity(无限)

There is a special value in JavaScript called Infinity. It represents a number too big for JavaScript to handle. Infinity is indeed a number, as typing typeof Infinity in the console will confirm. You can also quickly check that a number with 308 zeros is ok, but 309 zeros is too much. To be precise, the biggest numberJavaScript can handle is 1.7976931348623157e+308, while the smallest is 5e-324.

console.log(Infinity); //Infinity
console.log(-Infinity); //-Infinity
console.log(typeof Infinity);//number

console.log(1e309);//Infinity
console.log(1e308);//1e+308


NaN-Not a Number

 It turns out that despite its name, "Not a Number", NaNis a special value that is also a number:

console.log(Infinity - Infinity);//NaN
console.log(typeof NaN);//number


String conversions(字符串的转换)

When you use a number-like string (for example "1") as an operand in an arithmetic(算术) operation, the string is converted to a number(字符串转换为数字) behind the scenes(在幕后). This works for all arithmetic operations except addition(除了加号运算符,对所有的运算符有效), because of its ambiguity(模糊).

var s = '1';
s = 3 * s;
console.log(typeof s);//number
console.log(s);//3

var s = '1';
s++;
console.log(typeof s);//number
console.log(s);//2

var s = "100";
console.log(typeof s);//string
s = s * 1;
console.log(typeof s);//number
console.log(s);//100

var n = "100 hello";
n = n * 1;
console.log(typeof n);//number
console.log(n);//NaN


//+号运算符是一个特殊情况
var ss = '1';
ss = ss + 1;
console.log(typeof ss);//string
console.log(ss);//11


Special strings

\\    \is the escape character.

\'    对单引号转义

\"    对双引号转义

\n    End of line.

\r    Carriage return

\t    Tab.

\u    \ufollowed by a character code allows you to use Unicode.


Undefined and null

If you declare a variable without giving it a value, this is, of course, not an error. But, the typeof still returns "undefined":

console.log(typeof foo);//undefined
var foo;
console.log(typeof foo); //undefined


The null value, on the other hand, is not assigned by JavaScript behind the scenes; it's assigned by your code:

var somevar = null;
console.log(somevar);//null
console.log(typeof somevar);//object


Although the difference between nulland undefinedis small, it could be critical at times. For example, if you attempt an arithmetic operation, you get different results:

var i = 1 + undefined;
console.log(i);//NaN
var i = 1 + null;
console.log(i);//1


Primitive data types recap(概括)

Let's quickly summarize some of the main points discussed so far:

  • There are five primitive data types in JavaScript:

Number

String

Boolean

Undefined

Null

  • Everything that is not a primitive data type is an object

  • The primitive number data type can store positive and negative integers or floats, hexadecimal numbers, octal numbers, exponents, and the special numbers NaN, Infinity, and –Infinity

  • The string data type contains characters in quotes

  • The only values of the Boolean data type are trueand false

  • The only value of the null data type is the value null

  • The only value of the undefined data type is the value undefined

  • All values become true when converted to a Boolean, with the exception of the six falsy values:

  1. ""

  2. null

  3. undefined

  4. 0

  5. NaN

  6. false


在这篇文章中如果没有涉及到的,请看http://my.oschina.net/xinxingegeya/blog/290022

====END====


你可能感兴趣的:(JavaScript-Primitive Data Types(原始数据类型))