JavaScript Primitive Data Types

JavaScript Primitive Data Typesapp

 

Primitive data types

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

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

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

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

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..net

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.‍code

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

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:three

  • Primitive (the five types listed previously)ip

  • 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=============

相關文章
相關標籤/搜索