JavaScript data types

Source: Internet
Author: User

JavaScript has six data types, undefined, null, number, string, Boolean, object, and the previous five are the underlying data types, also known as primitive types, which are the basic types that can no longer be subdivided. object is a complex data type, which is usually more than one data type, in addition to a new symbol in the ES6, expressed as a unique value, this does not understand the temporary involved.

Undefined: Indicates not yet defined
Null: expressed as ' empty '
Number: integer or decimal
String: Strings literal
Boolean: Boolean value, True or False
Object: Represents an object, a key-value pair

Undefined and null

1. When you declare a variable through VAR and do not initialize the variable, the value of the variable defaults to undefined

var a;console.log(a);// undefined

It also returns a undefined when you call a function without returning a value.

function fn() {}console.log(fn()); // undefined

2. Undeclared and uninitialized or different, as follows, a is not initialized, B is not declared, the system will error

var a;    console.log(a);// undefinedconsole.log(b);// ReferenceError: b is not defined

3.null represents a pointer to an empty object, preferably initialized to NULL if you define a variable that will be used to hold the object in the future

var a = null;
What is the difference between undefined and null?

We can understand that NULL is expressed as having content, and this content is empty, and undefined is not defined, and there is no thought of what to put, suppose we put this piece of paper in front of you want to draw, if you use null, meaning this piece of paper is a blank screen, And if the use of undefined is that I put this piece of paper, but have not yet thought about what to draw, let's put it this way.

But if you use it for == comparison you will find that you return one and true if both of the statements () will be converted false .

Boolean

There are only two values for a Boolean value: a true true false representation of false, case-sensitive, and a Boolean value that typically occurs under the following conditions.

Logical operators: &&, | |,!
Transport characters: = =,! =, = = =,! = =, >, >=, <, <=
Automatic conversion of the IF statement to a Boolean value

It can also be used for Boolean() conversion, except undefined、null、false、0、NaN、""或‘‘(空字符串) that it will be converted to the false rest truw .

Number

All the numbers in 1.JavaScript are stored as 64-bit floating-point number, because of the binary floating-point algorithm, we can see the following situation

console.log(0.1 + 0.2 ); // false

The actual value is slightly greater than 0.3, the specific can be seen here

2. In addition to decimal can be represented as octal, 16 binary, binary

// 八进制:有前缀0的数值,或者有前导0、且只有0-7var num8 = 070; // 56// 十六进制:有前缀0x或0X的数值,有且只有0-9,a-fvar num16 = 0X3e; // 62 // 二进制:有前缀0b或0B的数值,有且只有0,1var num2 = 0B10; // 2

3.number can also be expressed using scientific notation

console.log(3e13); // 30000000000000console.log(3e-3); // 0.003

The number object of the 4.JavaScript provides the maximum minimum that can be represented

Number.MAX_VALUE // 1.7976931348623157e+308  2的1024次方Number.MIN_VALUE // 5e-324 2的-1023次方

Overflow occurs when you exceed, programming inifinity, if negative is-inifinity,

5.NaN is represented as a non-numeric value when an operation that would have returned a numeric value does not return a numeric value, such as a number with a string-

console.log(‘a‘ - 10); // NaN

Of course + , if you use it, you will not return Nan because it will be converted to a string.

console.log(‘a‘ + 10); // a10

Any operation of the same Nan will return Nan

console.log(NaN + 10); // NaN

And Nan is not equal to any value, including itself.

console.log(NaN === NaN); // false

6.+0 and-0
The two are usually equal, except as the denominator.

console.log((1 / +0) === (1 / -0)); // false

7. The global method associated with the value is

parseint (): Used to convert a string to an integer
Parsefloat (): Convert to floating-point number
IsNaN (): Is not a numeric value
Isfinite (): Is the normal value

The specific conversion Rule line query document, it is important to note that parseint () has a second parameter, which represents the binary of the parsed string

parseInt(‘1000‘, 2) // 8parseInt(‘1000‘, 8) // 512
String

1. Strings are usually used "" , ‘‘ indicating that you can put double quotes in single quotation marks, in turn, you need to note that the beginning of the end of the same

console.log(‘a string"); // 报错 nvalid or unexpected token 

2. Some special characters can not be directly in the string, need to pass the escape character to \ line

\0:null (\u0000)
\b: Back key (\u0008)
\f: Page Break (\u000c)
\ n: Line break (\u000a)
\ r: Enter (\u000d)
\ t: Tab (\U0009)
\v: Vertical tab (\U000B)
': Single quotation mark (\u0027)
": double quotes (\u0022)
\: backslash (\u005c)

3. When the string is created, it is immutable (including the length of the array is not variable), to change the content can only first destroy the previous content.

var lang = ‘java‘;lang = lang + ‘script‘;

Instead of stitching the string directly in the original array, the above operation script creates a new string and then populates the javascript previous two string.

4. Strings can be read using the array square bracket method

var str = ‘this is a string‘;console.log(str[0]); // t

5.toString () numeric value, Boolean value, object, string all have this method, see the MDN document for details

Object

1. 键对值 a simple set of objects, which can contain multiple data types, including nested objects

var obj = {    num: 123,    str: ‘javascript‘, bool: false, otherObj: {}}

2. The object is a reference type, meaning that the above obj actually holds the address of an object, so when another object is directly equal to obj, the address of the object is actually copied, so no matter which modification affects the other object, it belongs to the shallow copy.

var obj1 = {a: 1}var obj2 = obj1;obj2.a = 3;console.log(obj1.a); // 3

3. The reading and setting of the property can use the . dot operator or [] , the numeric key name cannot be . read, also need to note that the property name in square brackets need to use the "" parcel

var obj = {    1: 666,    ‘a_b‘: 777}console.log(obj.1); // 报错console.log(obj[1]); // 666console.log(obj[a_b]); // 报错console.log(obj[‘a_b‘]); // 777

property is deleted by using thedelete

delete obj.1;

4. To know if there is a property in the object that can be used in , return a Boolean value

var obj = {a: 1};‘a‘ in obj // true

Use for in to traverse all properties of an object

for (var i in obj) {  console.log(i);} 

In order to avoid traversing to inherited properties and methods, you should use the hasOwnProperty() property method that first determines whether the object itself is used.

for (var i in obj) {    if (object.hasOwnProperty(i)) { // ... }}

5. Object instance Common properties and methods

Constructor: Pointer to constructor
hasOwnProperty (): Determines whether the properties of the object itself
ToString (): Returns the string representation of the object.
ValueOf (): Returns the string, numeric, or Boolean representation of an object.

typeof and Instanceof

typeof can be used to determine the data type

Console.log (typeof' str ');Stringconsole.log (typeof 123); //numberconsole.log (typeof true); //booleanconsole.log (typeof Span class= "hljs-literal" >undefined); //undefinedfunction fn ( Console.log (typeof fn); //functionconsole.log (typeof null); //object             

You can see that null returns an object, stating that null is an empty object.

For the above types of typeof enough, but for complex object but not enough

console.log(typeof []); //objectconsole.log(typeof {}); //object

This time we need our instanceof, it will return a Boolean value, indicating whether it is an instance of a constructor

console.log([] instanceof Array); //trueconsole.log({} instanceof Object); //truevar str = new String();console.log(str instanceof String); //true 
Reference

Nanyi JavaScript Tutorial-data type

JavaScript Advanced Programming Chapter III

JavaScript data types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.