JS Learning Note (i) Basic data types

Source: Internet
Author: User

JS is a weakly typed language, all variables are declared with Var, and the strings are enclosed in double or single quotes. such as var num = 123, or var num = "123";

If the variable is not used directly under the VAR keyword declaration (Disabling this method), the variable is a global variable that has a global scope. An undefined data type uses no errors (only declarations are not assigned), but instead displays undefined. JS allows repeating variables, "in the same scope chain," equivalent to assigning values such as var num = 10;var num = "abc";

Declare multiple variables at once
var v1
, V2 = 1
, v3
, v4
, V5;
equivalent VAR v1; VAR v2; VAR v3; VAR V4; var v5;
The browser is interpreted to execute, that is, the execution from the top down, the JS code is the same, at the beginning of the time possible to use the variables are all declared complete (JS no block-level scope).

Array

JS in the array declaration with [], such as var arr = ["Zhang San", "John Doe", "Harry", "Zhao Liu", "Chen Qi"]; the array is indexed by the array, resulting in the index of the array, not the corresponding array member.

Special values
Nan (not a number) is not a digit, as 0/0 obtains is NaN.
Infinity Infinity
There is an object number in the numeric type

Number.MAX_VALUE numeric maximum, similar to Int32.MaxValue in C #
Number.min_value Digit Minimum Value
Number.POSITIVE_INFINITY Zheng Infinity
Number.negative_infinity Negative Infinity

Judging method
Boolean IsNaN (number); Whether it is a number
Boolean isfinite (data);//whether there is poor
such as var num = 0/0;
Alert (IsNaN (num));
Alert (isfinite (num));

String

The string escape character specification for JS is consistent with C #.

Common methods and properties of strings
Gets the length of the string str.length
Gets the character of a seat in a string Str.charat (i);
Gets the substring string str.substring (StartIndex, EndIndex); The left bounds are obtained, and the right bounds are not taken, equivalent to the [in] set
String Str.substr (StartIndex, Count);

Separates the string by a character Array str.split (' | ', number); The second argument can be omitted

Conversion of number and string

String, number
(1) The number object calls the ToString method.
var num = 10;
var res = num.tostring ();
(2) Add a space
var num = 10;
var res = num + ""
(3) using the string (number) function
var num = 10;
var res = String (num);
(4) The number in JS does not have a fixed precision representation, which can be called by invoking a specific method, and the return type is string.
var n = 1234.56789;
var S4 = n.tofixed (2);//reserved Two decimal places
var S5 = n.toexponential (2); The 2-bit index indicates
var s6 = n.toprecision (2); Number of significant digits

String-Number
(1) Do a numeric operation other than addition
var s = "12345";
var r = S/1; s-0;
(2) using the parse system method
parseint () parsefloat ()
var s = "08";
var r = parseint (s);

The parse method, which recognizes only the numbers that begin in a string, returns Nan if it is not recognized.

Alert (parseint ("A123ABC") + 1); return Nan
(3) using the number () function
var n= "123";
var s=number (n);

Boolean type

The Boolean type of JS is equivalent to the bool type of C #. Can be converted to a Boolean type by using the Boolean () method.
Judging method
Digital to boolean//non-zero true, 0 false
String to boolean//empty string for false the rest are true
Null and undefined are false
Take the inverse twice to get the Boolean type
var num = 0;
Alert (!! NUM);
Boolean to numeric and string
True, 1 false-0

JS Learning Note (i) Basic 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.