JavaScript Basics Getting Started Tutorial (ii)

Source: Internet
Author: User
Tags arithmetic value of pi wrapper

Description

The previous blog introduces JS and some basic types of JS simple knowledge, this blog will detail the basic type of JS, the introduction of the object type, more detailed description of the object type will be followed.

Description of the type in JS

The types in JS are divided into basic types and object types, where the basic types are: ① numbers, ② strings, ③ boolean values. There are also two primitive values null and undefined. Objects include array objects, function objects, and ordinary objects, and ordinary objects are unordered collections of named values, and array objects are ordered collections of numbered values. The JavaScript core also defines three useful classes: the date (Data) class, the regular (RegExp) class, the error class.

Digital

Unlike other common programming languages, JS does not distinguish between integers and floating-point numbers, and all values in JS are represented by floating-point values.

Integral type Direct Quantity

In JS, a number sequence by default is a decimal integer, while supporting the use of "0x" or "0X" hexadecimal, it is worth noting that although some JavaScript interpreters support the octal representation of the beginning of 0, but the ECMAScript standard is not supported, So try not to use octal notation to avoid problems with porting.

Floating-point Direct volume

Floating-point direct quantities are represented as: [digits][.digits][(E|e) [(+|-)]digits], such as ①3.14, ②6.02e23, ③.22e-23//0.22x10-23

Arithmetic operations in JS

JS not only supports the common addition (+) minus (-) multiplication (*) except (*) and remainder (%), but also supports more complex operations, but requires the support of the Math object:

JS statement Role
Math.pow (2,53) 53 Powers of 2
Math.Round (0.6) Rounded with a result of 1.0
Math.max (x, Y, z) To find the maximum value
Math.PI Returns the value of Pi Pi

JS arithmetic operation will appear overflow and underflow, which is slightly different from other languages, the overflow result is Infinity or-infinity, the result of underflow is 0 or-0, the general 0 and 0 no difference (the 0===-0 result is true), but sometimes there will be different, such as 1/ 0 result is Infinity, and 1/-0 result is-infinity. Another result is Nan (not a number), which is expressed in Nan when the result is not numeric or cannot be converted to a number, such as a negative root, 0 divided by 0, Infinity divided by infinity, and so on.

The global function isNaN () and the global function isfinity () can be used to determine the type of the numeric value, and when a number is Nan the isNaN () function returns True when a number is not nan, infinity, or-infinity isfinity ( ) returns True.

Rounding errors for binary floating-point numbers

Look at the following code:

1 var x =. 3-. 2; 2 var y =. 2-. 1; 3 x = = y    //false4 x = =. 1    //false5 y = =. 1    //true

The reason for this result is that the representation of the binary floating-point number is approximate, and it is worth noting that this problem will not only occur in JS, but will exist in all other languages that use floating-point counting. In order to better solve this problem can be the number of their own to expand a multiple, see the following code:

1 var x = 3-2; 2 var y = 2-1; 3 x = = y;    // true
Date and time

Date is part of the language core of JS, which briefly describes some simple uses of date, and a more detailed introduction will be presented in the following blog.

1 varnow =NewDate ();//Get current Time2 varthen =NewDate (2011,0,1);//January 1, 20113 varlater =NewDate (2011,0,1,17,10,30)//January 1, 2011 17:10 30 seconds4 varelapsed = Now-then;//The result is the number of milliseconds for the time interval5Later.getfullyear ();// .6Later.getmonth ();//0: Month count starting from 07Later.getdate ();//1: Monthly date count starting from 18Later.getday ();//6:0 stands for Sunday, 6 for Saturday

Text

Speaking of strings, you need to talk about encoding, JS uses UTF-16 Unicode encoding, so that since the "Basic Multilingual Plane" character is two bytes, and the "auxiliary multilingual Plane" character is four bytes. This means that a string of length 2 in JS may be two characters or only one character.

1 var p = "π"; 2 var e = "??" ;// "\UD835\UDC52" 3 p.length    //14 e.length    //2

It is important to note that the various function operations defined in JS are all for the "basic multi-lingual plane", so be careful when working with strings.

String Direct Amount

String literals can be enclosed in single or double quotes, and in ECMASCRIPT3, the direct amount of the string must be written on one line, whereas in ECMAScript5, the direct amount of the string can be split into several lines, but each line must end with a backslash (\).

1 "wouldn ' t you prefer D ' Reilly's book?" 2 "34 line"    //ECMAScript5

Because both JS and HTML support both single and double quotes to enclose the content, it is common to enclose the content in separate quotes, respectively:

1 <  onclick= "alert (' Hello World ')">click me</  Button>
Use of strings

Like Java, JS supports strings using the plus sign for stitching, the string "Hello," + "world" equivalent to "Hello, World". To determine the length of a string using its Length property, there are a number of methods that can be called in addition to the JS string:

1 vars= "Hello, World";2 varlen=s.length;3S.charat (0)//h: First character4S.charat (s.length-1)//D: Last character5S.substring (1,4)//ell: 第2-4个 character6S.slice (1,4)//ell: Ibid .7S.slice (-3)//Rld: last three characters8S.indexof ("L")//2: Position of the first occurrence of the character L9S.lastindexof ("L")//10: Position of the last occurrence of the character LTenS.indexof ("L", 3)//3: Position after position 3 and the first occurrence of the character L OneS.split (",")//["Hello", "World"]: divides the string into substrings in the array according to ",".  AS.replace ("H", "H")//"Hello, World": Replace all h in full text with H -S.touppercase ()//"HELLO, World

Furthermore, the reference to the string is subscript, meaning that S.charat (0) is equivalent to S[0]. Like Java, JavaScript's string is immutable (immutable), and the method defined by the string class cannot alter the contents of the string. A method like String.touppercase () returns a completely new string instead of modifying the original string.

Boolean value

The following values are converted to false when the condition is judged, and all other cases are converted to true, including arrays, functions, objects, and so on. It is worth noting that although 0 will be converted to false, "0" is converted to true.

1 undefined 2 NULL 3 04 -05NaN6 "" // empty string
null and undefined

Although null and undefined are different, they are often interchangeable. For example, the result of null==undefined is true (can use = = = Strictly equal to the judgment of null and undefined unequal). In general, the difference between null and undefined is that NULL is present but the vacancy value, and undefined is not present. For example, a variable is defined, but no value is generally null, but if an object does not have the length property at all, and we forcibly use his length property, it returns undefined.

Wrapping Object

Now you may be wondering why the basic data types in JS have attributes, such as the following code, although S is a string type, but it still has properties. Some of the students who have studied Java may be entangled that string should not be a type of object? The answer here is "no", in JS, string is a separate basic type, and string is a type of object.

1 var s= "Hello, World"2var world=s.substring (S.indexof ("W"), s.length);

The result is that the above effect, is basically the type of JS packaging caused. The JS interpreter converts a basic type to a temporary corresponding wrapper class type each time the variable is used, such as the above S, when using its Length property, the JS interpreter automatically executes a similar temp=new String (s); Temp.length the operation to replace the sentence just now. As with strings, numbers and Booleans have constructors that correspond to the wrapper class type, Number () and Boolean (), respectively. Null and undefined do not have wrapper classes, and accessing their properties will be an error.

At the time of sentencing, the = = operator will determine the same base type with the same value as its corresponding wrapper type, while = = = is determined to be unequal.

References to mutable objects

Objects differ from their original values, first of all they are mutable-their values are modifiable:

 1  var  o={x:1}; //  defines an object             2  o.x=2; //             3  o.y=3; //  change this object again and add a new property to it  4  5  var     a=[1,2,3]; //  Arrays are also modifiable             6  a[0]=0; //             7  a[3]=4; //  add an element to the array  

Comparisons of arrays and object types are reference comparisons, similar to the Java language, where two objects are unequal, even if they are of the same type and have the same property values. Only two references point to the same object, and the two references are equal.

Type conversions

Because of the weak type of JS, so the JS interpreter itself will automatically do a lot of implicit conversion, if you expect a Boolean value, JS will convert the given value into a Boolean value, the conversion rules see above, similarly if the expected string, JS will automatically convert the given value into a string type, if the expected number, It also automatically converts the value to a number (if the conversion result is meaningless, it will get Nan).

The next thing about type conversions, personally, is that this blog is a bit better than the JavaScript definitive guide.

JavaScript Basics Getting Started Tutorial (ii)

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.