JavaScript data type Sample Sharing _ Basics

Source: Internet
Author: User
Tags numeric numeric value

We introduced the data type of JavaScript, today we have some examples to warm up, I hope we can reach the new point.

Copy Code code as follows:

<script type= "Text/javascript" >
1. Boolean type
2, Number type
3, String type
Boolean types are easy to confuse with basic types, so it is recommended that you never use a Boolean object.
Number is a reference type that corresponds to numbers
var numberobj = new number (10);
The argument passed in to override the ToString method is a string type that tells it to put back a few digits
var num = 10;
Alert (num.tostring ());//"10"
Alert (num.tostring (2));//"1010"
Alert (num.tostring (8));//"12"
Alert (num.tostring (10));//"10"
alert (num.tostring);//"A"
The ToFixed () method, which returns a string representation of the numeric value of the specified decimal place, and has rounded functionality
var num = 10;
Num.tofixed (2);//"10.00"
Toexponential () Exponential notation method that accepts a parameter representing the number of decimal digits of the output result
var num = 10;
Alert (num.toexponential (1));//"1.0e+1"
However, this small number does not need to use exponential notation, if you want to get a number of the most appropriate format should use
Toprecision () method, this method may return a fixed size (fixed) format, or it may return an exponential (exponential) format
Accepts a parameter that represents the number of digits (excluding the exponent portion) of all digits of a numeric value.
var num = 99;
Alert (num.toprecision (1));//1e+2,1e+2 represents 100 because the index cannot represent 99 so rounding up to 100
Alert (Num.toprecision (2));//"99"
Alert (Num.toprecision (3));//"99.0"
String object, and the method of a string object can also be accessed in all base strings.
1, character operation Method: CharAt (), charCodeAt (). Each parameter accepts a character position based on position 0
var stringvalue = "Hello world!";
Stringvalue.charat (1);//"E" the second position is "E"
Stringvalue.charcodeat (1);//"101" the second position "E" character encoding is "101"
2, string operation method Concat (stitching character), slice (index,index), substring (index,index), substr (index,length). Index: Location, Length: Lengths
var str1 = "Hello";
Alert (Str1.concat ("word"));//hello World
Alert (Str1.concat ("word", "!")); /hello world!
var stringvalue = "Hello world!";
Alert (Stringvalue.slice (3));//lo World
Alert (stringvalue.substring (3));//lo World
Alert (STRINGVALUE.SUBSTR (3));//lo World
Alert (Stringvalue.slice (3, 7));//lo W
Alert (stringvalue.substring (3, 7));//lo W
Alert (STRINGVALUE.SUBSTR (3, 7))//lo Worl This 7 represents the length of the Intercept
3. String position method IndexOf () and LastIndexOf ()
Both methods search for the given string from the specified string, then return the position of the string, and return 1 if not found.
The difference between the two methods is that a string is searched backwards from the beginning of the string, and LastIndexOf is searching the string forward from the end of the string.
Both of these methods have an optional argument (search starts at the specified location)
var stringvalue = "Hello word";
Alert (Stringvalue.indexof ("O"));//4
Alert (Stringvalue.lastindexof ("O"));//7
You can loop through indexof or LastIndexOf to find the specified string
var stringvalue = "Wo de wei Lai bu shi meng!wo men you Geng Hao de Ming tian!";
var positions = [];
var pos = stringvalue.indexof ("E");
while (Pos >-1) {
Positions.push (POS);
pos = Stringvalue.indexof ("E", pos + 1);
}
alert (positions);//4, 7, 22, 33, 38, 47
4, Trim () This method creates a copy of the string, removing all the spaces before and after.
var stringvalue= "Hello word";
alert (stringvalue);
Alert (Stringvalue.trim ());
5. String capitalization conversion method
toLowerCase, Tolocallowercase, toUpperCase, tolocaluppercase
var stringvalue= "Hello word";
Alert (Stringvalue.tolocaleuppercase ());//This method is more secure
Alert (Stringvalue.touppercase ());
Alert (Stringvalue.tolocalelowercase ());//This method is more secure
Alert (Stringvalue.tolowercase ());
6. String matching method replace ()
This method takes two arguments, the first argument is a regular expression or a string, and the second argument is a string or a function
var text= "Cat,bat,sat,fat";
var result=text.replace ("At", "ond");
alert (result);//"Cond,bond,sond,fond"
var result=text.replace (/at/g, "ond");
alert (result);//"Cond,bond,sond,fond"
var text= "Cat,bat,sat,fat";
Result=text.replace (/(. at)/g, "Word ($)");
alert (result);
The second parameter of replace can also be a function
function Htmlescape (text) {
The function has three parameters: 1, Pattern match 2, the position of the pattern match in the character 3, the original string
Return Text.replace (/[<> "&]/g,function" (Match,index,text) {
Switch (match) {
Case "<":
Return "<";
Case ">":
return ">";
Case "&":
Return "&";
Case "\":
Return "" ";
}
});
}
Alert (Htmlescape ("<p class=\" greeting\ ">hello world!</p>"));
<p class= "Greeting" >hello world!</p>
Localcompare () compares two strings. A.localcompare ("B")
This returns a negative number (-1) if the string (A) is ranked before the string parameter (B) in the alphabet.
Returns 0 if the string equals a string argument
Returns a positive number (1) if the string (A) is ranked after the string argument (B) in the alphabet
var stringvalue= "F";
Alert (Stringvalue.localecompare ("D"));//1
Alert (Stringvalue.localecompare ("F"));//0
Alert (Stringvalue.localecompare ("z"));//-1
fromCharCode This static method is to perform the opposite operation with charCodeAt
Alert (String.fromCharCode (104,101,108,108,111));//"Hello"
7, the HTML method is not recommended to use.
</script>

End

Children's shoes have a new understanding of JavaScript data types, I hope you can enjoy.

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.