JavaScript Math (Arithmetic) object

Source: Internet
Author: User
Tags arithmetic modifier modifiers

1.JavaScript Math (Arithmetic) object

The Math object works by performing common arithmetic tasks.

1.1 Math Object

The Math object works by performing normal arithmetic tasks.

The Math object provides a variety of arithmetic value types and functions. You do not need to define it before using this object.

Syntax for using the properties/methods of math:

var x=Math.PI; var y=math.sqrt (+);

Note: The Math object does not need to be defined before the object is used.

1.2 Calculation value

JavaScript provides 8 arithmetic values that can be accessed by the Math object:

You can refer to the following JavaScript constants using this method:

Math.EMath.PIMath.SQRT2Math.SQRT1_2Math.LN2Math.LN10Math.LOG2EMath.LOG10E

1.3 Arithmetic methods

In addition to the arithmetic values that can be accessed by the Math object, there are several functions (methods) that can be used.

The following example rounds a number by using the round method of the Math object.

document.write (Math.Round (4.7));

The following example uses the random () method of the Math object to return an arbitrary number between 0 and 1:

document.write (Math.random ());

The following example uses the floor () method and random () of the Math object to return a random number between 0 and 11

document.write (Math.floor (Math.random ()));

2.JavaScript RegExp Object

REGEXP: is a shorthand for regular expressions (regular expression).

2.1 What is REGEXP?

The regular expression describes the pattern object of the character.

When you retrieve a text, you can use a pattern to describe what you want to retrieve. REGEXP is this pattern.

The simple pattern can be a single character.

More complex patterns include more characters and can be used for parsing, format checking, substitution, and so on.

You can specify the location of the search in a string, the type of character to retrieve, and so on.

2.2 Syntax

var patt=New  RegExp (pattern,modifiers); or the simpler way var patt=/pattern/modifiers;

Note: When you use constructors to create regular objects, you need a regular character escaping rule (preceded by a backslash \). For example, the following are equivalent:

var New REGEXP ("\\w+"); var re =/\w+/;

2.3REGEXP modifier

Modifiers are used to perform case-insensitive and full-text searches.

The i -modifier is used to perform a case-insensitive match.

The g -modifier is used to perform a full-text search (instead of finding the first one to stop looking, but to find all matches).

2.4test ()

The test () method searches for the value specified by the string, depending on the result and returns TRUE or false.

The following example searches for the character "E" from a string:

var patt1=New RegExp ("e");d ocument.write (patt1.test ("  theBest Things ());

2.5exec ()

The EXEC () method retrieves the specified value in the string. The return value is the value that was found. If no match is found, NULL is returned.

The following example searches for the character "E" from a string:

JavaScript Math (Arithmetic) object

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.