JavaScript Advanced Programming Learning Note 1 basic types.

Source: Internet
Author: User

/*************************************************************************************************************
* @ Chapter II using JavaScript in HTML
*/
<script> Tag Properties
Async Optional: Indicates that the script is downloaded immediately, but does not interfere with other actions on the page, only valid for external files.
Defer Optional: Indicates that the script can delay all documents being parsed and displayed before being executed, only valid for external files.
Src Optional: Represents the outer file to be executed.
Type Optional: Represents the content type of the scripting language in which the code is written.

Deprecated syntax causes XHTML to ignore the script.
<script>
<!--

-
</script>

Document mode, which causes promiscuous mode if no document is declared. In some special cases will affect the interpretation of JS execution.
<!doctype html>

/************************************************************************************************************** ****
* @ Chapter III Basic Concepts
*/

All identifiers (variable names, function names, operators) in the ECMAScript are case-sensitive.

The first character of an identifier must be one of the letters, underscores, and characters.

Strict mode.
"Use strict";

@ variable
The variables in ECMAScript are loosely typed and can hold any type of data (basic and reference types, which are just placeholders for storing data, and variable names can be arbitrarily changed after they are declared).
var num = 4;
num = "num";
num = null;

@ Data type
ECMAScript the data types in 2, which are reference types and base types, respectively.

@ Basic Type
The base type contains undefined, NULL, Boolean, number, String;

@typeof operator
The typeof operator is used to detect the base type data, return a string, and operate on null if the version before Safari5 Chrome7 returns "Object"
"Undefined"---if this value is undefined;
"Boolean"---if this value is a Boolean value;
"String"---if the value is a string;
"Number"---if the value is numeric;
"Object"---if the value is an object or null;
"Function"---if the value is a function;

@undefined type
Undefined represents a variable that is declared but not initialized, but a variable with a undefined value is not the same as a variable that has not been defined.
VAR message;
alert (message);//"Undefined"
alert (num); Error

@null type
Null represents a pointer to an empty object, which is why using the TypeOf operator returns "Object".
If a defined variable is prepared to save the object in the future, it is best to use NULL initialization instead of the other value.

The

@number type
Number type represents a numeric type, and ECMAScript uses the IEEE754 format to represent integers and floating-point numbers.
1. Floating-point numbers, the so-called floating-point number must contain a decimal point, and must have at least one digit after it. Floating-point numbers require a memory space of twice times the integer type.
So ECMAScript will lose no chance to convert floating-point numbers to integers. As shown below:
var Floo = 10.00//Integer---Resolved to ten

2. The value range ECMAScript indicates that the minimum value is saved in Number.min_value, and the maximum value is saved in Number.MAX_VALUE.
More than these 2 values is Infinity (positive infinity)-infinity (negative infinity), for whether a valid number uses the Isfinite () function, if the value returns true between the maximum and the minimum value; The

3.NaN non-numeric value is a special value. This value indicates that the operand that originally wanted to return a number did not return a numeric value. Any value/0 in ECMAScript returns NaN;
Nan has 2 features: 1.NaN and any number of operations will return NaN, 2. Nan and any values are not equal, including itself (nan! = Nan), which defines a function IsNaN (nan) in
ECMAScript, which converts the argument to a numeric value and returns False if the non-numeric returns true;

4. Numeric conversions
There are 3 functions that can convert a non-numeric value to a value: Number (), parseint (), parsefloat ();

The number () conversion rules are as follows:
True and false are converted to 1 and 0;
If it is a numeric value, it simply passes in the outgoing, returning the original value.
If it is a null value, 0 is returned.
If it is undefined, return nan.
If it is a string, press the following rule.
If the string contains only numeric values, convert it to decimal and return the value. "+123" returns 123, "12" returns-12, "0122" returns 122 leading 0 is ignored.
If the string contains a valid floating-point format, "012.1" returns 12.1 leading 0 is ignored.
If the string contains a valid hexadecimal format. It is converted to a decimal value of the same size. such as "0xa" returns 10.
Returns 0 if the string is empty.
If the string contains characters outside the above, return nan as "Aew12" returns Nan.

If it is an object, call the object's valueof () method, and then return the value as described in the preceding rule. The ToString () method is called when there is no valueof () method, and the value is returned by the preceding rule.

parseint ()
parseint () When converting a string, it is more to see if it conforms to the numeric pattern. He ignores the spaces in front of the string until the first non-whitespace character is found. If it is not a numeric character or symbol.
parseint () returns Nan, meaning that parseint () converts an empty string to return Nan. If the first character contains a number, it resolves to all subsequent characters or encounters a non-numeric character.
"12ad1" returns 12.
parseint (12,10) The second argument is in what format to parse. Binary, octal, decimal, hexadecimal.

Parsefloat ()
Also begins parsing from the first non-whitespace character, ignores leading zeros, encounters an invalid first non-numeric type, and the second decimal point includes the subsequent invalid. parsefloat () only resolves decimal.

@string type
The strings in the ECMAScript are immutable, that is, once the strings are created, their values cannot be changed. To change the string saved by a variable, first destroy the original string, and then populate the variable with a new string.

Convert to String
One of two ways to convert a value to a string is to use the ToString () method with almost every value. The method returns the corresponding string representation. If True returns "true"
The function can pass parameters to control which input output is made. Default by Decimal. such as var a = 12; A.tostring (16) return C;
* Note that this is only useful for numeric types, and non-numeric values such as "12" are output by the original value.

To know that null and undefined are not methods in ECMAScript, use the String () constructor method to convert. Returns "NULL" if it is null.

@ unary plus minus operator
Use the unary plus minus operator to convert this value like the number () function.
var S1 = +1, S2 = + ' 1.1 ', s3 = + ' Z ';
S1 returns 1, S2 returns 1.1, S3 returns NaN;

@ Boolean operator
1. Logical Non-
Logical non-operator by one (!) Represents any value that can be applied to the ECMAScript. No matter what type this value is, a Boolean value will be returned. And then it's reversed. The rules are as follows.
If the operand is an object, returns true and then deserializes it, returns false, and the following rule is the same, so the shorthand eventually returns the result.
Returns true if the operand is an empty string;
Returns False if the operand is a non-empty string;
Returns true if the operand is a numeric value of 0;
Returns False if the operand is any number other than 0 (contains infinity);
Returns true if the operand is null;
Returns true if the operand is Nan;
Returns true if the operand is undefined;

2. Logic and
The logic and operator are represented by two (&&) symbols with 2 operands. You can manipulate any value when the operation is not a Boolean type, and the logic does not necessarily return a Boolean value, as in the following rule.
Returns the second operand if the first operand is an object.
If the two operands are objects, the second operand, the second object, is returned.
If the second operand is an object, the object is returned if the first operand evaluates to true, otherwise the first operand is returned (not necessarily false, for example, null && {} returns null).
Returns null if one of the operands is null.
If one of the operands is undefined, the undefined is returned.
If one of the operands is Nan, a nan is returned.
* Note If the operand is undefined null nan then the first operand is which one returns which such as Nan && null returns nan;
* Note that logic is a short-circuit operation, that is, the first operand can be calculated so that the second operand is not evaluated. For example:
Alert (False && ABC) printed here false
Alert (True && ABC) here is an error because the variable ABC is undefined.


3. Logical OR
The logical OR operator consists of two (| |) Indicates that there are two operands. Similar to logic, if there is an operand that is not a Boolean value, logic or does not necessarily return a Boolean value, as the rule is.
If the first operand is false, a second operand is returned.
If the first operand is an object, the first operand is returned.
If the two operands are objects, the first operand is returned.

If the two operands are null, one of the undefined NaN objects returns the first operand.
* Logic and also a short-circuit operation, and the first operand returns true then the second operand is not evaluated.


@ multiplicative operator
ECMAScript defines 3 multiplicative operators: multiplication, division and modulo. A type conversion occurs when a non-numeric operation is performed. If an operand that participates in multiplicative calculations is not a numeric value, the background uses the number () function to convert the numeric operation.

1. Multiplication
The multiplication operator is represented by a (*) to calculate the product of two values. As shown below:
var num = 10 * 21;

In cases where special values are handled, the multiplication operator follows the following rules:
If the operands are numeric, perform a regular calculation, two numbers are positive or negative numbers return a positive number, a negative number is returned, and if the product exceeds the range of the ECMAScript value, it returns Infinity (positive infinity) or-infinity;
If a number is Nan, the result is Nan;
If the infinity is multiplied by 0, the result is Nan;
If any value is multiplied by 0, the result is 0; any non-numeric value is multiplied by 0, the result is NaN;
If the Infinity is multiplied by a value other than 0, the result is Infinity or-infinity, if the result is Nan, multiplied by the non-numeric type;
If the Infinity is multiplied with Infinity, the result is Infinity;
If an operand is an object that calls the ValueOf () method first, the ToString () method is called if it is not. Call number () in the background to convert it to a numeric value.
If the operand has a value that is not a number, call number () in the background to convert it to a numeric value, and then apply the rule above.

2. Division
The division operator uses a (/) representation to perform a calculation of two numbers, as follows:
var num = 99/9;
In cases where special values are handled, the division operator is similar to the multiplication operator rule.

3. Mould-Finding
The modulo operator, represented by a (%), performs a calculation of two numbers, as follows:
var num = 99 9;
In cases where special values are handled, the modulo operator is similar to the multiplication operator rule.

The @ relational operator comparison operator.
Less than, greater than, less than equals, greater than or equal to these relational operators are used to compare two values. These operators will return a Boolean value.
As with other operators in ECMAScript, when the operands of a relational operator use a non-numeric value, the data is converted or some strange operation is done. The rules are as follows:
If the two operands are numeric, a numeric comparison is performed.
If all two operands are strings, compare the ASCLL codes corresponding to the two strings.
If one operand is a number, the other operand is converted to a numeric value and then compared. Returns False if a value is NaN;
If the operand is an object, the valueof () method of the object is called, the ToString () method is called, and then the corresponding operation is performed on it.
If an operand is a Boolean value, it is converted to a number and then compared.

The @ equality operator.
1. Equality and inequality
The equality operator in ECMAScript is represented by (= =) and returns True if the two operands are equal. The unequal operator is represented by (! =) and returns True if the two operands are not equal.
Both operators convert the operands (casts) before comparing their equality. The rules are as follows:
If one of the operands is a Boolean value, convert it to a numeric--false to 0 and true to 1 before comparing equality. For example, alert (true >= false);
If one operand is a string and the other operand is a numeric value, the string is converted to a numeric value before equality is compared;
If one operand is an object and the other is not, then the object's valueof () method is called first, the toString () is not called, and the resulting value is compared by the preceding rule.

null and undefined are equal
Before you can compare equality, you cannot convert null and undefined to any other value.
If one of the operands is NaN, the equality operator returns FALSE, and the inequality operator returns true; * Note that if the two operands are Nan then the equality operator returns false, and Nan is not equal to Nan;
If the two operands are objects, compare whether they are the same object, if the two operands point to the same object. The equality operator returns TRUE, otherwise false is returned;


2. All equal to full range
In addition to not doing operand conversions before comparison, congruent is no different from the full inequality operator and equal to the unequal operator. The congruent Operation Mr. Foo the type, and if the type is inconsistent, returns false if the type is consistently compared to the value.




JavaScript Advanced Programming Learning Note 1 basic types.

Related Article

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.