One, "JavaScript basic data Type"JavaScript has a dynamic type. This means that the same variable can be used as a different type
"String" "Number" "Object" "Boolean" "function" "Undefined "
<1> String Type
Property:
Str.length
var str = "123,abc,900,rgy,rrrr"; Console.log (str.length);//20var str = "123,abc,900, Shang, rrrr"; Console.log (str.length );//20
Tip:
From the experimental results can be seen whether the English characters or Chinese characters, they appear in length, the same
There is no difference, there are several characters that occupy several positions, and here is not a distinction between multibyte characters,
Here they are treated equally
Method:
Convert case:
Str.tolowercase ();//Returns a string in which the letters in the string are converted to lowercase
Str.touppercase ();//Returns a string in which the letters in the string are converted to uppercase
var str = "123,abc,900, Shang, rrrr"; Console.log (Str.tolowercase ());//123,abc,900, Shang, Rrrrconsole.log (str.touppercase ());//123,abc,900, Shang, RRRR
index character (string):
Str.charcodeat (num);//Returns an Integer that represents the Unicode encoding of the specified position character
Str.fromcharcode (NUM2,NUM2,NUM3);//Returns a string from some Unicode string
Str.charat (num);//Specifies the character at the index position if the index value beyond the valid range returns an empty string.
Str.indexof (flag); Returns the first occurrence of a substring position within a string object
Str.lastindexof (flag);//Returns the last occurrence of a substring position within a string object
var str = "ABC"; Console.log (Str.charcodeat (1));//(The Unicode encoding of the character B of index = = 1) console.log (String.fromCharCode ( 65,66,67));//ABCconsole.log (Str.charat (5));//Empty Console.log (Str.charat (1));//Bvar str = "ABCBA"; Console.log ( Str.indexof ("B")),//1console.log (Str.lastindexof ("B")),//3console.log (Str.indexof ("z"));// -1console.log ( Str.lastindexof ("z"));// -1console.log (Str.indexof ("B", 2));//3console.log (Str.lastindexof ("B", 4));//3
Tip:
1.indexOf () and lastIndexOf () if no substring is matched, 1 is returned
2.indexOf ("B", 2) indicates the position of the index==2, starting from left to right.
3.lastIndexOf ("B", 4) indicates the position of the index==4 starting from the right-to-left search
To intercept a string:
Str.substring (start,end);//Returns a substring at the specified position in a string object
Str.substr (start,length);//Returns a substring of the specified length starting at the specified position
Str.slice (start,end);//Returns a substring at the specified position in a string object
var str = "0123456"; var arr = str.substring (1,4); Console.log (str);//0123456console.log (arr);//123console.log ( Str.substring (4,1));//123console.log (str.substring (1));//123456console.log (Str.substring (-2));//0123456
Tip:
1. You can see that substring (1,4) and substring (4,1) are equivalent to intercept the index = = 1 to index = = 4 between the string
2. It can also be seen that it has no effect on the original array
3. If the parameter is negative, the entire full string is returned
////
var str = "0123456"; var arr = str.substr (1,4); Console.log (str);//0123456console.log (arr);//1234
Tip:
1.SUBSTR (1,4) means to intercept from the position of index = = 1 and intercept 4 characters backward
2. It can also be seen that it has no effect on the original array
////
var str = "0123456"; var arr = Str.slice (1,4); Console.log (str);//0123456console.log (arr);//123console.log (Str.slice ( 4,1));//null, does not support this way Console.log (Str.slice (1));//123456console.log (Str.slice ( -5));//23456console.log (Str.slice (-5, -1));//2345
Tip:
1. It can be seen that it has no effect on the original array
2. Str.slice (4,1) is not supported, the previous index is greater than the index behind
3. Support parameter is negative
Regular Expressions:
Match:str.match (REG); Returns an array
Search:str.search (REG); return number
Replace:str.replace (REG); return string
Split:str.split (REG); Returns an array
var str = "123,abc,900,rgy,rrrgyr"; Console.log (Str.match (/rgy/));//["Rgy", Index:12, Input: "123,abc,900,rgy,rrrgyr" ]console.log (Str.match (/rgy/g));//["Rgy", "Rgy"]tip: The default matches the first, plus the flag "G" means global match var str = "123,abc,900,rgy,rrrgyr"; Console.log (Str.search (/rgy/));//12tip: Default matches First, return indexvar str = "123,abc,900,rgy,rrrgyr"; Console.log (Str.replace ( /rgy/, "KKK"));//123,abc,900,kkk,rrrgyrconsole.log (Str.replace (/rgy/g, "KKK");//123,ABC,900,KKK, Rrkkkrconsole.log (Str.replace (/(\d+), (\w+)/g, "$2,$1"));//Abc,123,rgy,900,rrrgyrvar str = "123,abc,900,rgy,rrrgyr" ; var arr = Str.split (",");//["123", "abc", "N", "Rgy", "Rrrgyr"]var arr = Str.split (/,/);//["123", "ABC", "/", "Rgy" , the parameter in the "Rrrgyr"]tip:split () method can be a regular expression, or it can be a different string
<2> Number Type
Number.MAX_VALUE//1.7976931348623157e+308
Number.min_value//5e-324
Number.negative_infinity//INFINITY (Infinity)
Number.POSITIVE_INFINITY//-infinity (negative infinity)
Number is converted to string:
Console.log (Math.ceil ("1000.3"));//1001 (Rounding up) Console.log (Math.floor ("1000.3"));//1000 (rounding down) console.log ( Math.Round ("1000.3"));//1000 (rounded) Console.log (parseint ("1000.3"));//1000console.log (parsefloat ("1000.3"));// 1000.3
parseint () and parsefloat ():
parseint (str [, Radix]);
Parsefloat (str [, Radix]);
The number returned is 10 binary
Radix is optional and represents the binary, which is between 2 and 36, indicating what the binary is for parsing
When the value of the parameter radix is 0, or if the parameter is not set, parseint () determines the cardinality of the number based on string.
Console.log (parseint ("1011", 2)); 11console.log (parseint ("18", 10));//18
parsefloat () Usage Similar
<3> Object Type
Property:
Constructor
Prototype
Method:
hasOwnProperty (); Used to determine if a property is a local property
isPrototypeOf ();//used to determine the relationship between a prototype object and an instance.
ToString ();
toLocaleString ();
ValueOf ();
function Cat (name,color) {this.name = Name;this.color = color;} Cat.prototype.type = "Cat animal"; Cat.prototype.eat = function () {alert ("Eat Mouse");} var cat1 = new Cat ("Da Mao", "Yellow"), var cat2 = new Cat ("Er Mao", "Black"), Alert (Cat.prototype.isPrototypeOf (CAT1));//truealert ( Cat.prototype.isPrototypeOf (CAT2)),//truealert (Cat1.hasownproperty ("name")),//truealert (Cat1.hasownproperty (" Type "));//false
<4> Boolean type
the implicit conversion rule for Boolean:
1. Special values undefined and null become false
2. Numbers 0 and Nan become false
3. Empty string becomes false
4. All other values become true
<5> function typeThere are two ways to define a function in JS: One is a function declaration and the other is a function expression
function declaration:
function functionname (arg0, arg1, arg2) {}
function expression:
var functionname = function (arg0, arg1, arg2) {};
Tip
function declaration, it has an important feature is the function declaration promotion, before executing the code will read the function declaration
Therefore, the function declaration can be placed behind the statement that called it.
For example:
Say (); function say () {alert ("Hello world!");}
There is no concept of function overloading in JS, and when a function is called, it is called by the nearest principle.
<6> undefined type
Undefined relationship to null:
Console.log (undefined = = null);//True
Console.log (undefined = = = null);//False
Learn more about undefined and null:
Http://www.ruanyifeng.com/blog/2014/03/undefined-vs-null.html
To determine the type of undefined:
if (typeof (value) = = "undefined") {
Alert ("undefined");
}
//////////////////////
Second, "Data type detection"
Introduction:
var nums = [1,2,3,4];typeof nums//Objectnums.constructor = = = array;//truenums instanceof array;//trueobject.prototype. Tostring.call (nums);//"[Object Array]"
/////////////////////
<1> typeof
Grammar:
typeof value;
Supports 6 values
"String" "Number" "Object" "Boolean" "function" "Undefined"
Example:
Console.log (typeof "123");//"string" Console.log (typeof 123);//"Number" Console.log (typeof {});/"Object" Console.log (typeof []);//"Object" Console.log (typeof null);//"Object" Console.log (typeof true);/"Boolean" Console.log (typeof function () {});//"function" Console.log (typeof undefined);//"Undefined"
/////////////////////
<2> instanceof
Grammar:
Value instanceof?;
Example:
var obj = {name: "Kylin", age:21,list: [1,2,3]}var person = function (name,age) {this.name = Name;this.age = age;} var p = new Person ("Kylin", +); Console.log (P instanceof person); Trueconsole.log (obj instanceof person); Falseconsole.log (obj instanceof Object); Trueconsole.log (new Date () instanceof date); Trueconsole.log ([] instanceof Array); True
/////////////////////
<3> Constructor
Grammar:
Value.constructor = = = =?;
Example:
var person = function (name,age) {this.name = Name;this.age = age;} var p = new Person ("Kylin", +); Console.log (p.constructor = = person); Trueconsole.log ([].constructor = = Array); Trueconsole.log (New Date (). constructor = = Date)//true
/////////////////////
<4> Object.prototype.toString.call ()
Grammar:
Object.prototype.toString.call (value);
Example:
Object.prototype.toString.call ("123");//"[Object String]" Object.prototype.toString.call (123);//"[Object number]" Object.prototype.toString.call ({});//"[Object Object]" Object.prototype.toString.call ([]);//"[Object Array]" Object.prototype.toString.call (null);//"[Object null]" Object.prototype.toString.call (true);//"[Object Boolean]" Object.prototype.toString.call (function () {});//"[Object Function]" Object.prototype.toString.call (undefined);//" [Object Undefined] "
JavaScript basic data types and type detection