Basic JavaScript data types and types detection, javascript Data Types

Source: Internet
Author: User
Tags hasownproperty

Basic JavaScript data types and types detection, javascript Data Types
1. [basic JavaScript Data Types] JavaScript has dynamic types. This means that the same variables can be of different types.
"String" "number" "object" "boolean" "function" "undefined"

<1> string type
Attribute:
Str. length

Var str = "123, ABC, 900, rgy, rrrr"; console. log (str. length); // 20var str = "123, ABC, 900, ran Guangyu, rrrr"; console. log (str. length); // 20
Tip:
From the experiment results, we can see that both English and Chinese characters have the same length,
There is no difference. There are only a few characters to take up a few positions. Here multi-byte characters are not distinguished,
They are treated equally here


Method:

Case sensitivity:
Str. toLowerCase (); // returns a string in which letters are converted to lowercase letters.
Str. toUpperCase (); // returns a string in which the letters are converted to uppercase.


Var str = "123, ABC, 900, ran Guangyu, rrrr"; console. log (str. toLowerCase (); // 123, abc, 900, ran Guangyu, rrrrconsole. log (str. toUpperCase (); // 123, ABC, 900, ran Guangyu, RRRR


Index character (string ):
Str. charCodeAt (num); // returns an integer that represents the Unicode encoding of characters at a specified position.
Str. fromCharCode (num2, num2, num3); // return a string from some Unicode strings
Str. charAt (num); // specifies the character at the index position. If the index value exceeds the valid range, an empty string is returned.
Str. indexOf (flag); // returns the position of the substring that appears for the first time in the String object.
Str. lastIndexOf (flag); // returns the position of the last substring in the String object.

Var str = "ABC"; console. log (str. charCodeAt (1); // 66 (index = 1 character B Unicode encoding is 66) 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. If no substring is matched between indexOf () and lastIndexOf (),-1 is returned.
2. indexOf ("B", 2) indicates the position starting from index = 2 and searching from left to right
3. lastIndexOf ("B", 4) indicates the position starting from index = 4 and searching from right to left.


Truncation string:
Str. substring (start, end); // return the substring at the specified position in the String object
Str. substr (start, length); // returns a substring of the specified length starting from the specified position.
Str. slice (start, end); // return the substring at the specified position in the 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. We can see that substring () is equivalent to substring (), and both are truncated strings between index = 1 and index = 4.
2. At the same time, we can see that it has no effect on the original array.
3. If the parameter is negative, the entire complete string is returned.

////
var str = "0123456";var arr = str.substr(1,4);console.log(str);// 0123456console.log(arr);// 1234
Tip:
1. substr () indicates to intercept from index = 1, and 4 characters to backward
2. We can also see 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 (); // null. console is not supported. log (str. slice (1); // 123456console. log (str. slice (-5); // 23456console. log (str. slice (-5,-1); // 2345
Tip:
1. We can see that it has no effect on the original array.
2. str. slice () is not supported. The preceding index is greater than the following index.
3. The supported parameters are negative.


Regular Expression:
Match: str. match (reg); // returns an array.
Search: str. search (reg); // return number
Replace: str. replace (reg); // returns a 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: match the first one by default, the flag "g" indicates global match var str = "123, ABC, 900, rgy, rrrgyr"; console. log (str. search (/rgy/); // 12tip: the first one is matched by default, and indexvar str = "123, ABC, 900, rgy, rrrgyr"; console is returned. log (str. replace (/rgy/, "kkk"); // 123, ABC, 900, kkk, rrrgyrconsole. log (str. replace (/rgy/g, "kkk"); // 123, ABC, 900, kkk, rrkkrconsole. 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", "900", "rgy", "rrrgyr"] var arr = str. split (/,/); // ["123", "ABC", "900", "rgy", "rrrgyr"] tip: split () the parameters in the method can be regular expressions or other strings.

<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 to string:
Console. log (Math. ceil ("1000.3"); // 1001 (rounded up) console. log (Math. floor ("1000.3"); // 1000 (rounded down) console. log (Math. round ("1000.3"); // 1000 (rounding) console. log (parseInt ("1000.3"); // 1000console. log (parseFloat ("1000.3"); // 1000.3


ParseInt () and parseFloat ():

ParseInt (str [, radix]);
ParseFloat (str [, radix]);

Returns 10 hexadecimal numbers.

Radix is optional and indicates the hexadecimal format. The value ranges from 2 ~ The value range is 36.
When the value of the radix parameter is 0, or this parameter is not set, parseInt () determines the base number based on the string.

console.log(parseInt("1011", 2)); // 11console.log(parseInt("18", 10));// 18

ParseFloat () usage is similar


<3> object Type

Attribute:
Constructor
Prototype


Method:
HasOwnProperty (); // used to determine whether an attribute is a local attribute.
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"; Cat. prototype. eat = function () {alert ("eat mouse");} var cat1 = new Cat ("Big Hair", "yellow"); var cat2 = new Cat ("two hair ", "Black"); alert (Cat. prototype. isPrototypeOf (cat1); // truealert (Cat. prototype. isPrototypeOf (cat2); // truealert (cat1.hasOwnProperty ("name"); // truealert (cat1.hasOwnProperty ("type"); // false

<4> boolean Type
Implicit conversion rules of boolean:

1. The special values undefined and null are changed to false.
2. The numbers 0 and NaN are converted to false.
3. Empty string to false
4. All other values change to true.


<5> function types can be defined in js in two ways: function declaration and function expression.


Function declaration:
Function functionName (arg0, arg1, arg2 ){}


Function expression:
Var functionName = function (arg0, arg1, arg2 ){};


Tip:
When a function is declared, it has an important feature of improving the function declaration. Before executing the code, it reads the function declaration.
Therefore, you can put the function declaration behind the statement that calls it.
For example:
say();function say(){alert("hello world!");}

JS does not have the concept of function overloading. Function calling follows the proximity principle.


<6> undefined type
Relationship between undefined and 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


Determine the undefined type:
If (typeof (value) = "undefined "){
Alert ("undefined ");
}


//////////////////////
Ii. 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

Syntax:
Typeof value;
// Six values are supported.
"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
Syntax:
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", 21);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

Syntax:
Value. constructor === ?;


Example:
var Person = function(name,age){this.name = name;this.age = age; }var p = new Person("kylin", 21);console.log(p.constructor == Person); //trueconsole.log([].constructor == Array); //trueconsole.log(new Date().constructor == Date)//true

/////////////////////
<4> Object. prototype. toString. call ()
Syntax:
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]"



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.