Getting Started with JavaScript (9)

Source: Internet
Author: User
Tags array definition rounds

First, the Math object

Math object that provides a mathematical calculation of the data

Using the properties and methods of math, the code is as follows:

<script type= "Text/javascript" >    var mypi=Math.PI;     var myabs=math.abs ( -15);    document.write (Mypi);    document.write (myabs); </script>// operating results //3.14159265389793//

Note: The Math object is an intrinsic object that you can call all of its properties and methods without having to create it, directly using math as an object. This is the difference between it and the Date,string object.

Math Object Properties

Math Object method

Second, upward rounding ceil ()

The Ceil () method can be rounded up to a number, which returns a value greater than or equal to X, and the nearest integer to X

Grammar:

Math.ceil ()

Parameter description:

Eg:

<script type= "Text/javascript" >     document.write (Math.ceil (0.8) + "<br/>")     document.write (Math.ceil (6.3) + "<br/>")     document.write (Math.ceil (5) + "<br/>")     document.write (Math.ceil (3.5) + "<br/>")     document.write (Math.ceil (-5.1) + "<BR/ > ")     document.write (Math.ceil (-5.9))</script>/    *     Results:    1, 7,5, 4,-5,-5    * /

Three, take down the whole floor ()

The floor () method takes a number down and returns the integer that is less than or equal to x and closest to X

Grammar:

Math.floor (x)

Parameter description:

Eg:

<script type= "Text/javascript" >   document.write (Math.floor (0.8) + "<br/>")   document.write (Math.floor (6.3) + "<br/>")   document.write (Math.floor (5) + "<br/>"  )   document.write (Math.floor (3.5) + "<br/>")   document.write (Math.floor (-5.1) + "<br /> ")   document.write (Math.floor (-5.9))</script>/* Run Result: 0, 6, 5, 3,-6,-6* /

Iv. Rounding Round ()

The round () method rounds a number to the nearest integer.

Grammar:

Math.Round (x)

Parameter description:

Note:

1) returns the nearest integer to X

2) for 0.5, the method will be rounded up (5.5 will be rounded to 6)

3) If x is similar to an integer on both sides, the result is close to the numeric value in the positive infinity direction. (such as:-5.5 rounds -5;-5.52 will be rounded to-6)

Eg: Rounds a different number to the nearest integer

<script type= "Text/javascript" >    document.write (Math.Round (1.6) + "<br/>");    document.write (Math.Round (2.5) + "<br/>");    document.write (Math.Round (0.49) + "<br/>");    document.write (Math.Round (-6.4) + "<br/>");    document.write (Math.Round (-6.6)); </script>/* Run Result: 2,3,0,-6,-7* /

v. Random number randomly ()

The random () method returns a random number between 0~1 (greater than or equal to 0 but less than 1), returning a numeric value greater than or equal to 0 but less than 1 positive sign

Eg: We get a random number between 0 and 1

// get a random number between 0~1 <script type= "Type/javascript" >    document.write (Math.random ()); </script>/* Run Result: 0.190305486195328 The result is different for each run, but it is the value of 0~1 */ / Get 0~ Random number between 10 <script type= "Type/javascript" >    document.write ((Math.random ())*10); </script>/* Run Result: 8.72153625893887* /

Six, array arrays object

An array object is a collection of objects that can be of different types. Each member object of an array has an "subscript" that represents its position in the array, starting at 0.

Method of array Definition:

1. Defines an empty array

var array name =new array ();

2. Specify an array with n empty elements when defining

var array name =new array (n);

3. When defining an array, initialize the data directly

var array name =[< element 1>, < element 2>, < element 3>...];

Eg: Defines the myarray array and assigns values

var myArray = [2, 8, 6];

Description: Defines an array of myArray, inside the element is: myarray[0]=2;myarray[1]=8;myarray[2]=6

Array elements are used:

Array name [subscript] = value;

Array properties:

Length usage:< Array Object >.length;

Returns: the length of the array, which is the number of elements in the array. It equals the subscript of the last element in the array plus one.

Array method:

Seven, array connection concat ()

The Concat () method is used to concatenate two or more arrays. This method returns a new array without changing the original array.

Grammar:

Arrayobject.concat (array1, array2,..., Arrayn)

Parameter description:

Note: This method does not alter the existing array, but only returns a copy of the concatenated array.

Eg: Create an array that will connect the parameters in Concat () to the array Myarr

<script type= "Text/javascript" > varnew Array (3);  mya[0] = "1"; mya[1] = "2";        mya[2] = "3";       document.write (Mya.concat (4,5) + "<br/>");   document.write (mya); </script>/* Run Result: 1,2,3,4,51,2,3* /

Eg: Create three arrays, then use Concat () to connect them.

<script type= "Text/javascript" >    varnew Array ("hello!" var new Array ("I", "Love" );          var New Array ("JavaScript", "!" );     var mya4 = mya1.concat (mya2, mya3);    document.write (MYA4); </script>/* Run Result: hello!, I, love, javascript,! */

Getting Started with JavaScript (9)

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.