JavaScript (b): Data Types & Values

Source: Internet
Author: User
Tags bitwise

Part I: Data types

JavaScript data types are typically 6 (ES6 new seventh Symbol type)

Number: value

String: Strings

Boolean: Boolean type, True or False

undefined: No value assigned

null: null, value is "none" state

Object: Objects

Usually Number,string,boolean is called the primitive type (primitive type) , that is, the most basic value, cannot continue subdivision, and the object is called the Composite type (complex type) value, An object is often a composition of more than one primitive type, while undefined and null are two special values!

Objects can be divided into three sub-objects: narrowly defined objects (object), arrays (array), functions.

JavaScript is all about objects, that is, all data can be considered a generalized object!

typeof operator : Determines what type a value is, and returns a string type.

1Console.log (typeof123);//' number '2Console.log (typeof' 123 ');//' String '3Console.log (typeof true);//' Boolean '4 functionTest () {}5Console.log (typeofTest);//' function '6Console.log (typeofundefined);//' undefined '7Console.log (typeof NULL);//' object '8Console.log (typeof []);9Console.log (typeofwindow);

Operation Result:

Values, strings, Boolean types, functions, undefined return the number,string,boolean,function,undefined respectively;

In addition, all other objects return an object.

typeof can be used to check for a variable that is not declared or is not assigned:

1 if (typeof v = = = ' undefined ') {2     console.log (' yes '); 3 }

Or:

1 var v; // Watch this . 2 if (typeof v = = = ' undefined ') {3     console.log (' yes '); 4 }

The result of the operation is output: Yes.

Comparison of null and undefined:

They actually have almost the same grammatical effect, which is one of the shortcomings of JavaScript design! (Google developed the JavaScript language alternative to the dart language, only null, no undefined).

In the IF statement, they are all converted to false; in the = = equality operator, the two are equal!

The only difference is probably:typeof null=== ' object ', and typeof undefined=== ' undefined '

if (undefined) {    console.log (' nothing ');} Else {    console.log (' undefined will be converted to false in the IF statement ');} if (! NULL {    console.log (' null is converted to false in the IF statement ');} Console.log (typeofnull = = = ' object '); Console.log (typeof undefined = = = ' undefined ');

Operation Result:

Boolean value:

These operators return a Boolean value:&& (and), | | (or),! (non), ===,==,!==,!=,>,>=,<,<=

These six values will be converted to False:udefined,null,false,0,nan, "and" (an empty string) on some expected positions (e.g. IF)! The rest will be converted to TRUE!!!

Note that even if a space exists between strings, it is not an empty string!!!

The remainder is converted to true, such as a string with spaces, an empty array, and an empty object.

Part II: Values

2.1 Integers and floating-point numbers

Inside JavaScript, all numbers are stored in 64-bit floating-point number. So:

However, in the bitwise operation, because the bitwise operator only works on integers , the 32-bit signed integer will be used at this time, and the return value is also a 32-bit signed integer!

Because the floating-point number is not accurate, the operation involving decimals should be careful:

2.2 Numerical accuracy

According to the International standard IEEE 754,javascript floating point number of 64 bits, starting from the leftmost, this constitutes:

    • 1th bit: Sign bit, 0 for integer, 1 for negative number;
    • 2nd to 12th place: index part;
    • 13th to 64th bit: Fractional part (valid number)

IEEE 754 stipulates that the first digit of the valid number is 1 by default, and is not guaranteed to exist in 64-bit floating-point numbers. As a result, JavaScript provides a maximum number of valid digits (13~64) +1=53 bits.

So the absolute value is less than 2 of the 53-square integer, namely-(2^53-1) ~ 2^53-1, can be accurately expressed!

2.3 Range of values

The 64-bit floating-point index part length is 11 bits, so the exponent maximum value is 2047 (2^11-1). Half indicates a negative number, and the value range is 2^1024~2^ (-1023) "Open interval".

If the exponential portion >=1024,javascript returns Infinity (infinity), this is called a "forward overflow", or if the <=-1023 (that is, very close to 0), returns 0, which is called a "negative overflow"!

Specific maximum and minimum values: The numberobject contains Max_value and Min_value.

Safe numerical Accuracy: The numberobject contains Max_safe_integer and Min_safe_integer.

2.4 Numeric Input:

    1. Decimal: the normal value;
    2. Hex: prefix 0x or 0X;
    3. Octal: prefix 0o or 0O, or only prefix 0, but only 0-7 eight Arabic numerals (not recommended, ES5 Strict mode and ES6 are abolished this way)
    4. Binary: prefix 0b or 0 b

2.5 Special Values

+0 and-0;

One of the 64-bit floating-point numbers in JavaScript is a sign bit, so any number (including 0) has a negative value corresponding to it.

Note that +0,-0,0 is strictly equal in most cases! Except for 1/0, infinity,1/-0 is-infinity.

Nan:not a number, non-numeric

Note:nan is not equal to any value, including itself ! At the same time it is with any number of operations, the resulting is Nan.

The IsNaN () method is used to determine whether a value is nan!

IsNaN () is only valid for numeric values, and if other values are passed in, they are first converted to numeric values. such as: passing in a string, being converted to Nan, and finally returning true!

Infinity: infinity;-infinity: Negative infinity! (Note that there is no finity constant that says!) )

Note:Infinity cannot compare size with Nan! other number types are available!

isfinite (): Returns a Boolean value and detects if a value is limited!

Global methods related to numeric values: parseint () and parsefloat ()

parseint () is used to convert a string to an integer ; If the argument is not a string, it is first converted to a string, the string header has a space, it is ignored, and when it encounters a character that is not a numeric value, it does not proceed;

There are only two possible return values: NaN, decimal integer!

1Console.log (' parseint ()----');2Console.log (parseint (' 123 ')));3Console.log (parseint (123));//equivalent to Console.log (parseint (' 123 '));4Console.log (parseint (' 123 '));//Ignore spaces5Console.log (parseint (' abc '));//The first character cannot be converted to an integer, returning a Nan6Console.log (parseint (' 1234ab1234 '));//Back to 12347Console.log (parseint (0o12));//octal number8Console.log (parseint (0x12));//Hexadecimal number9Console.log (parseint (0B11));//Binary numberTenConsole.log (parseint (+10));//return ten One //Console.log (parseint (++10));//Error

Operation Result:

Note : For some numbers that will be automatically converted to scientific notation, parseint will convert the numbers of scientific notation into strings, leading to some alternative results:

parseint () can also accept the second parameter (between 2~36), indicating that the parsed is worth the system; The result returns a decimal number; By default, the second parameter is 10, which is 10 binary number to 10 binary number

The second parameter is not between 2 and 36, return Nan, the second parameter is 0, ' 23x ', undefined, etc., by default by 10 binary processing!

1 console.log (parseint (77,8)); // 8 Binary 77 rpm 10 binary, get 7*8+7=63 2 console.log (parseint (11,2)); // 2-in-turn 10-in system 3 console.log (parseint (36,10)); // the same as the default 10-in-turn 10-in effect! 

Parsefloat (): Used to convert a string into a floating-point number.

The string conforms to the scientific notation, and the corresponding conversion occurs; When a character in a string that cannot be converted to a floating-point number is encountered, the conversion is stopped and the converted part is returned;

Reference: Nanyi JavaScript standard reference Tutorial

JavaScript (b): Data Types & Values

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.