Basic JavaScript concepts (variables and data types)

Source: Internet
Author: User

1. Variables

  Omitting the var operator in a local scope can define a global variable, but doing so can cause the variable to be difficult to maintain, and in strict mode causes a Referenceerror error to be thrown.

2. Data type (JS Common 6 data types, respectively is Undefined,null,boolean,number,string,object)

  2.1 typeof operator

Using the typeof operator on a value may return one of the following strings:

-"Undefined"---if the value is undefined or uninitialized;

-"Boolean"---if this value is a Boolean value;

-"string"---if this value is a string;

-"Number"---if the value is numeric;

-"Object"---if this value is an object or null;

-"function"---if this value is a function;

Note: If this value is a regular expression, the old browser returns "function", and the new browser returns "Object".

2.2 Undefined type

var message;  This variable is declared after the default undefined value//The following variable does not declare//var age;alert (message);  "Undefined" alert (age);  Generate Error alert (typeof message);  "Undefined" alert (typeof age);  "Undefined"

2.3 Null Type

The ①null value logically represents an empty object pointer, so typeof NULL returns "Object";

② actually undefined values are derived from null values, so ECMA-262 specifies that the equality test for them returns true, that is, NULL = = undefined true;

③ when a defined variable is used to hold an object in the future, it is better to initialize the variable to null.

2.4 Boolean type

Any data type can call the Boolean () function, and in some flow control statements (such as the IF statement, the while statement, and so on) will automatically perform a corresponding Boolean conversion

Data type The value converted to true Value converted to False
Boolean True False
String Any non-empty string "" (empty string)
Number Any non-0 numeric value (including infinity) 0 and Nan
Object Any object Null
Undefined Not applicable Undefined

2.5 Number Type

2.5.1 Integer

① octal number starts with 0, if the following value is out of range, then the leading 0 will be ignored, followed by the value as a decimal value resolution (such as 070 resolution to 56;079 resolution to 79), and octal literal in strict mode is invalid, will error.

2.5.2 Floating-point values

①js saving floating-point values requires a memory space of twice times the integer value (4*2=8 bytes ), and the following two cases of floating points are parsed into integers

var a = 1.;  No digits after the decimal point, resolved to 1var B = 10.0  //resolved to 10

② the highest precision of the floating-point value is the number of decimal places, and the precision of arithmetic operation is much inferior to integers, such as 0.1 plus 0.2 result is not 0.3, but 0.30000000000000004

③ as much as possible without division (/) and modulo (%) operations, because in most cases they directly cause floating point numbers to appear. If you must use division, immediately use the Math.Round () method to return to the integer operation.

2.5.3 Range of values

The maximum value that the ①ecmascript can represent is stored on the number.max_value , and the minimum value is stored on the number.min_value (which can be represented by the nearest 0 number, and the value is 5e-324). , exceeding this range as infinity, where positive infinity is expressed as Infinity, negative infinity is-infinity, a number is judged to be poor, the isfinite () function can be used, and return true is poor.

2.5.4 NaN

①nan, which is a non-numeric value, is a special value that represents a case where the operand that would have returned a numeric value does not return a value (so that no error is thrown)

② any operation involving Nan (for example, NAN/10) returns Nan, and Nan is not equal to any value, including the Nan itself.

The ③isnan () function takes a parameter of any data type, which helps us determine whether the parameter can be converted to a numeric value, and the function uses the valueOf () method of the object automatically when it is used for the object, and then determines whether the returned value can be converted to a numeric value, if not, Then the toString () method is called based on this return value, and the return value is tested.

2.5.5 Numeric conversions

Number (param): The parameter param can be any numeric type, and its conversion rules are as follows

① if it is a Boolean value, True and false are converted to 1 and 0, respectively.

② if it is a number, it is simply passed in and returned.

③ returns 0 if it is null.

④ if it is undefined, return nan.

⑤ if it is a string, follow these rules:

        • If the string contains only numbers, it is converted to a decimal value and the leading zeros are ignored, and if it is in hexadecimal format, it is converted to a decimal integer of the figured size;
        • If the string is empty, it is converted to 0;
        • The rest is converted to Nan.

⑥ if it is an object, call the object's ValueOf () method first, then convert according to the previous rule, and if the result of the conversion is Nan, call the ToString () method of the object and then convert it according to the rules above.

parseint (String, radix): parameter string is a string (can be a number), Radix is the cardinality. When converting, it ignores the preceding space until it encounters the first numeric character or minus sign, if it is not returned Nan, if it continues to parse the second character knowing that a non-numeric character is encountered, in addition, ECMASCRIPT3 recognizes octal numbers beginning with 0, ECMASCRIPT5 is not recognized, the following is an example.

The following results all return 15:

parseint ("0xF", +);p arseint ("F", +);p arseint ("n", 8);p Arseint (021, 8);   Note that here 021 is the number is not a string, parseint ("021", 8) returned is 17parseInt ("015", Ten);p Arseint (15.99);p arseint ("FXX123", +);p Arseint ( "1111", 2);p arseint ("15*3", Ten);p arseint ("15e2", Ten);p arseint ("15px");p arseint ("12", 13);

The following results return Nan

The following results return 0 in ECMASCRIPT3, 8 in ECMAScript5

parseint ("08");

parsefloat (String):parsefloat () is similar to parseint (), but it always ignores the leading 0, and parsing a hexadecimal-given string is always converted to 0 if the string contains a number that can be resolved to an integer. Parsefloat () returns an integer.

2.5.6 String Type

Features of the ① string:

var lang = "Java"; lang = lang + "Script";

The following procedure is done in the example above: first create a new string that can hold 10 characters, then populate the string with "Java" and "script", and the final step is to destroy the original string "Java" and the string "Script", because these two strings are useless.

② converted to a string:

toString (): except for null and undefined, other numeric types have this method, and in most cases the method does not have to pass arguments. However, when you call the ToString () method of a number, you can pass a parameter: the cardinality of the output value. Such as

var num = 10;alert (num.tostring ());  "Ten" Alert (num.tostring (2));  "1010" Alert (num.tostring (8));  alert (num.tostring);  A

String (param): The String () function follows the following translation rules:

        • If the value has the ToString () method, the method (with no parameters) is called and the corresponding result is returned;
        • Returns "NULL" if the value is null;
        • If the value is undefined, then "undefined" is returned.

2.5.7 Object Type

object is the basis for all objects, so all objects have the following basic properties and methods:

Constructor, hasOwnProperty (PropertyName), isPrototypeOf (object), propertyIsEnumerable, toLocaleString (), toString ( ), ValueOf ()

Basic JavaScript concepts (variables and data 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.