The basic data type of JavaScript contains the basic data type of string,number,boolean,function,object,undified basic 6
This article focuses on the main methods of these six basic data types and the use
1 Character type stringfrequently used methods are
parseint (STR1) is not a method of character type
SUBSTRING (3,5) character intercept
IndexOf ("H"). LastIndexOf ("h") Index Lookup
toUpperCase () uppercase and lowercase conversion functions
var str= "abc"; The most frequently used character variable assignment is similar to the Var str=new String ("abc") ///Regular usage //1 converts a character to a word count, such as converting "1234" to a numeric type of integer var str1= "1234"
//note here is that parseint is not a string-type method, but rather a method of the Window object console.info (parseint (str1)) var str2= " Heshengjun123test " //2 to intercept a string for example , the Var str2=" heshengjun123test " //intercept the characters between 3,5/ /need to be aware of characters, The subscript of an array is calculated from 0 console.info (str2.substring (3,5))//contains the third character, does not contain the first position of the fifth character//find character, and returns 1 if not found. Console.info (Str2.indexof ("H"))//can pass a character, can also pass in the string //From the beginning of the character to find the position of the first character. If not found then return-1 console.info (Str2.lastindexof ("H"))////// string Connection console.info (Str2.concat ("Haoren "))//Two strings are concatenated to return equivalent to str2+" Haoren " //uppercase and lowercase conversion functions console.info (Str2.touppercase ())
2 numeric type number a few special values nan,infinity,-infinity
For Nan and other data, the calculation returns Nan
Defines the value var num=123;//equivalent to new number (123) //1/0 does not error, will return an infinity Infinity -1/0 Returns an infinitesimal-infinity Console.info (1/0) console.info ( -1/0) //Two-digit integer division may return a decimal, as in high-level languages such as Java console.info (4/3)//return 1.3333333333333333 Console.info (typeof NaN)//nan is the value of a number, which means that it is not a count (not a numbers) //For example parseint ("abc") Returns a Nan console.info (parseint ("abc")) Console.info (. 6)//= a number 0.6 //nan and other data calculations return a Nan Console.info (NaN-199);
3 Boolean Type Boolean has only two values: True,false
for characters. Value can be turned to true or False in Boolean
Character can be converted to Boolean type, normal is true, null character is False
The number is converted to a Boolean type, and the normal case is true,0 false
can also be converted to a Boolean type for an object, where the null object is converted to false
Two can be used for the general type!
Convert to Boolean type
Defines a Boolean type variable that has only two values of true false var bool=true;//equivalent to new Boolean (true) Console.info (Bool.tosource ()) // The character can be converted to a Boolean type, the normal condition is true, and the null character is False if ("abc") { console.info ("The character can be converted to a Boolean type, and normal is true.") The null character is false ") } //The number is converted to a Boolean type. The normal case is True,0,nan false if (! NaN) { Console.info ("the number is converted to a Boolean type, the normal case is true,0 is false") } //can use two for a generic type. Convert to Boolean type //For object can also be converted to Boolean if (!null) { Console.info ("Object converted to Boolean") }
4 function types Many other specific introductions please refer to the next topic on functions
There are three ways to define functions: function statements, function direct quantities, functions constructors
Define, declare a function var fun1=function () {Console.info ("This is a function") } //Run a function fun1 (); Console.info (fun1.length)//Returns the number of parameters that define a function Console.info (fun1.arguments)//arguments This variable needs to be used in the definition function to get the actual number of parameters //For The call () method of the function and the Apply () method. The following section specifically describes, mainly changing the function of the scope Console.info (Fun1.call ()) //tosource method does not necessarily have. Firefox supports the definition of return functions Console.info (Fun1.tosource ()) //define a function that uses arguments// for functions with return values, the return value expression needs a semicolon end, and put it to the last function fun2 (x, y) { ///actual number of arguments alert (arguments.length) //Specific Reference alert (arguments[ 0]) } //Run function fun2 (1,2,3,4);
5 Object objects here refer to the detailed instance object, not the concept of a class
Simple representation of Object {' name ': ' Zhangsan ', ' Age ': 18}
The propertyisenumerable attribute is inferred and can be passed for. In Loop out
hasOwnProperty its own properties, not the properties of the prototype
Define an object var obj={' name ': "Zhangsan", ' age ': +// variable obj.prototype=123 //constructor for each object Console.info (Obj.constructor ()) //infer whether its own property returns true false Console.info (obj.hasownproperty (' name ')) / /Returns the value of this object Console.info (obj.valueof ()) //propertyisenumerable infers that an object is not a prototype attribute //be able to pass in for: In loop out Console.info (obj.propertyisenumerable ("name")) for (var i in obj) { console.info (i) }
6 The Undified type variable is declared, but it is not assigned and is actually equivalent to NULL
var A; There is only a declaration, no assignment. Value is undefined console.info (a) //undefined type console.info (typeof undefined) //Two values equal Console.info (Undefined==null)
Anatomy of a basic type of javascript: