"JavaScript advanced Programming"--basic wrapper type and monomer built-in object

Source: Internet
Author: User
Tags numeric value wrapper
1.Boolean Type

A Boolean type is a reference type that corresponds to a Boolean value.

var falseobject=new Boolean (flase);
alert (falseobject&&true);//true;

var Falseobject=false;
alert (falseobject&&true);//false

It's okay, don't use a Boolean type ~ ~

2.Number Type

ToString (), you can add a parameter to represent the system:

var num=10;
Alert (num.tostring ());//"Ten"
Alert (num.tostring (2));/"1010"

ToFixed () that returns a string of numeric values, according to the specified decimal places.

var num=10.005;
Alert (num.tofixed (2));//"10.01"

Toexponential (), returns the string form of the numeric value represented by the E notation, which specifies the number of decimal places:

var num=10;
Alert (num.toexponential (1));//"1.0e+1"

Toprecision (), which returns a string form of a numeric value, possibly fixed or exponential, that specifies the number of digits (excluding the exponent portion) of all digits.

3.String Type

Each string type has a length property that represents the number of characters.

CharAt () returns the character at the specified position:

var str= "Hello";
Alert (Str.charat (1));//"E"

charCodeAt () returns the encoding of the character at the specified position:

var str= "Hello";
Alert (Str.charcodeat (1));//"101"

You can also access the characters at the specified location through square brackets:

var str= "Hello";
Alert (str[1]);//"E"

Slice (), substr (), substring () can split strings and accept two parameters: the first represents the starting position, the second: the end position for slice and substring, and the number of characters for the substr. When a negative value is passed, the behavior is different. It's not necessary to remember ~ ~

IndexOf () and LastIndexOf () have said before, and this is no longer said.

The trim () method removes all whitespace from the string prefix and suffix, and does not change the original string:

var str= "   Hello  World   ";
Alert (Str.trim ());//"Hello World"
alert (str);//"   Hello World   "


toLowerCase () and toUpperCase () convert strings to lowercase and uppercase, respectively.
toLocaleLowerCase () and toLocaleUpperCase () are implementations for specific areas.


There are some ways to match the string, you need to combine regular expressions, and so I learned to see after it ~ ~

Localecompare (), which compares two strings: Returns a negative number if the string is preceded by a parameter, returns 0 if it is equal, or returns a positive number if it is greater than:

var str= "Yellow";
Alert (Str.localecompare ("yellow"));//0
alert (str.localecompare ("Brick"));//1
alert (Str.localecompare (" Zero "));//-1


fromCharCode () accepts a series of character encodings and converts them to strings.

4.Math Objects

There are some common methods:

Math.min (1,2,3,4);//1
Math.max (1,2,3,4);//4
//Tips
var arr=[1,2,3,4,5,6];
var max=math.max.apply (Math,arr);

Math.ceil (1.5);//2
Math.floor (1.5);//1 Math.Round
(1.5);//2 math.random

();//return random number from 0 to 1, left-right open

Math.Abs ();
Math.pow ();
Math.sqrt ();

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.