Data type
1. String
2. Numerical type
3. Boolean type
4. Objects
5. Arrays
In order to be more standard, make oneself in the future work more easy and convenient maintenance, adopt some data type of name life habit
Type |
Prefix |
Example |
Array |
A |
Aarray |
Boolean value |
B |
Bmale |
Floating point |
F |
FTax |
Function |
Fn |
Fnswap |
Integral type |
I |
Iage |
Object |
O |
Ocar |
Regular |
Re |
Repattern |
String |
S |
Suniversity |
A string of strings
var smystring = "Hello World";
The string can be any text in quotation marks. You can use single and double quotation marks.
Instance:
Code: var smystring = "Hello World"; var smystring = ' Hello World '; var smystring = ' Hello World '; var smystring = "' Hello World '"; Result: Hello Worldhello World "Hello World"
The string has the length property and can return the number of strings in the variable.
var smystring = "Hello World"; document.write (smystring.length); // number of strings for output smystring: One
Gets the character at the specified position, which can be used with the charAt () function.
Note: The first character position is 0, the second character is 1, and so on.
var smystring = "Hello World"; document.write (Smystring.charat (1)); // the output is: E
If you want to get a string from a variable, you can use substring () or substr ()
var smystring = "ABCDEFG" document.write (smystring.substring (// output BCDEFG, Intercept from position 1 to last // output BCD, intercept from position 1 to position 4, not including position 4
var smystring = "ABCDEFG" // output BCDE, starting from position 1, intercept 4-bit
On the search string, you can use IndexOf () or lastindexOf (),
IndexOf (): Returns the index value in the source string for the first occurrence of a substring within the source string, is an integer, and the lookup direction is left to right
LastindexOf (): The lookup direction is from right to left,
Returns 1 if no substring is found
var smystring = "Abcdefghijk"= Smystring.indexof ("E") + "<br>"; // find the location of E from the Go // Optional parameters, looking backwards from the first few characters s3 = Smystring.lastindexof ("E") + "<br>"; // find from the back forward // Optional parameters, looking forward from the first few characters document.write (S1);d ocument.write (S2);d ocument.write (S3);d Ocument.write (S4);
Results:
444-1
Second, BOOL Boolean
Selectable values: True, Fasle
copy code copy code var bb = false ; // declares that BB is false if (BB) {// document.write ("AAA" + "<br>"); // just output AAA else {/ / otherwise document.write ("CCC" + "<br>"); // result output: CCC copy code copy code
() to display the type of the variable: var true ; document.write (typeof (BB) + "<br>"); var false ;d ocument.write (typeof (BB) + "<br>"); var bb = "12345";d ocument.write (typeof (BB) + "<br>"); var bb = 12345;d ocument.write (typeof (BB) + "<br>");
Results:
Boolean Boolean Stringnumber
Third, numeric number
In JavaScript, you want to limit the value of a number without qualifying it as an integer or floating-point type
var iNum1 = ten; var fNum2 = 56.41; var iNum3 = -41; document.write (iNum1+ "+inum2+" "+iNum3); // output Result: ten 56.41-41
Iv. Types of conversions
ToString (): Converts a logical value to a string and returns the result.
varA = 3;varb = A + 3;varc = "Student" +A;varD =a.tostring ();varE = a + "";d Ocument.write (a+ "+ B +" "+ C +" "+ D +" "+ e+" <br> ");d Ocument.write (typeof(a) + "" +typeof(b) + "" +typeof(c) + "" +typeof(d) + "" +typeof(e));//Output Result:3 6 Student3 3 3 Number Number string string
parseint (): Converts a string to an integer.
Parsefloat (): Converts a string into a floating-point type.
Only characters can call these two methods, otherwise they are converted to Nan. At the time of conversion, the character at position 0 is checked first, and if the character is a valid character, the character at 1 is checked, and if it is not a valid character, the conversion is just terminated.
Document.writeln (parseint ("4555.5544") + "<br>");d Ocument.writeln (parseint ("0.5544") + "<br>" );d Ocument.writeln (parseint ("a0.5544") + "<br>");d Ocument.writeln (parsefloat ("4555.5544") ) + "<br>");d Ocument.writeln (parsefloat ("3a.5544") + "<br>");d Ocument.writeln (parsefloat ( "a0.5544") + "<br>");
Results:
45550nan4555.55443nan
V. Array of arrays
An array is a set of variables that have the same type and name. These variables are called array elements (element), each array element has a number, this number is called subscript, we can use subscript to distinguish these elements. The number of array elements is also known as the length of the array.
In JavaScript, arrays are created using the keyword array declaration, and you can also declare the length of a variable.
Cases:
var amyarray = new Array (9); Declaring the length of a variable
When you are unsure of the final number of arrays, you can declare an array without specifying a specific number.
Example: var New // amyarray[0] = 1234; amyarray[1] = "1234"; amyarray[2] = "Red"; amyarray[3] = "Blue"
; amyarray[4] = "BLACK"
//
output to page
output effect:1234,1234,red,blue,black
Alternatively, you can create an array directly
Example: var New Array (1234, "1234", "Red", "Blue", "Black");d ocument.write (Amyarray+ "<br>");d Ocument.write ( typeof(amyarray[0]) + "" +typeof// output array element data type
output effect:1234,1234, red,blue,blacknumber string
In addition, you can also use
Arrays can be converted to strings using toString ()
var amyarray = new Array (1234, "1234", "Red", "Blue", "Black");d Ocument.write (amyarray.tostring () + "<br>");
Join () changes the connector between arrays
Example:var amyarray = new Array (1234, "1234", "Red", "Blue", "Black");d Ocument.write (amyarray.tostring () + "<br& gt; "); document.write (Amyarray.join ("-"). ToString () + "<br>");d Ocument.write (Amyarray.join ("+")); Effect:1234,1234,red,blue,black1234-1234-red-blue-black1234+1234+red+blue+black
Split () turns the string into an array
Example:var smystring = ("1234,red,blue,black"), var Amyarray = Smystring.split (",");d Ocument.write (smystring+ " <br> ");d Ocument.write (Amyarray.join ("-")); Effect:1234,red,blue,black1234-red-blue-black
Reverse () reverse the array elements
Example: var smystring = ("1234,red,blue,black"); var amyarray = Smystring.split (",");d ocument.write (smystring+ "<br>");d Ocument.write ( Amyarray.join ("-") + "<br>");d Ocument.write (Amyarray.reverse (). Join ("+")); Effect:1234 , Red,blue,black1234-red-blue-blackblack+blue+red+1234
Use sort () to sort the elements of an array in alphabetical order
Example: var smystring = ("1234,red,blue,black"); var amyarray = Smystring.split (",");d ocument.write (smystring+ "<br>");d Ocument.write ( Amyarray.join ("-") + "<br>");d Ocument.write (Amyarray.reverse (). Join ("+") + "<br>"); document.write (Amyarray.sort (). Join ("=")); Effect:1234, red,blue,black1234-red-blue- Blackblack+blue+red+12341234=black=blue=red
JavaScript Basics (iii)