Analysis of basic javascript types: Analysis of javascript types

Source: Internet
Author: User
Tags integer numbers hasownproperty

Analysis of basic javascript types: Analysis of javascript types

The basic data types of javascript include string, number, boolean, function, object, and undified.

This article mainly introduces the main methods and usage of these six basic data types.

One-character string commonly used methods include:
ParseInt (str1) is not a character type Method
Substring (3, 5)
IndexOf ("h"), lastIndexOf ("h") index search
ToUpperCase () case-sensitive Conversion Function
Var str = "abc"; // The most common character variable assignment is similar to var str = new String ("abc") // common method // 1 converts a character to a word count, for example, to convert "1234" to a numeric integer var str1 = "1234" // note that parseInt is not a string type method, instead, the window object method console.info (parseInt (str1 )) var str2 = "heshengjun123test" // 2 truncate a string, for example, var str2 = "heshengjun123test" // truncate the characters between 3 and 5. // note the subscript of the character and array. calculate lele.info (str2.substring (3, 5) from 0 )) // including the third character, excluding the fifth character // The first position of the character to be searched. If not found,-1 console.info (str2.indexOf ("h") is returned ")) // you can specify a character or a string. // you can specify the first position of the character after the character. If no position is found,-1 lele.info (str2.lastIndexOf ("h") is returned ")) /// string connected to lele.info (str2.concat ("haoren ")) // concatenates two strings and returns the result equivalent to str2 + "haoren" // case-sensitive Conversion Function console.info (str2.toUpperCase ())

2. Special values NaN, Infinity, and-Infinity for number.
For NaN and other data, the returned result is NaN.
// Defines the value var num = 123; // It is equivalent to new Number (123) // 1/0. No error is returned. An infinitely Infinity-1/0 is returned, and an Infinity eagle.info (1/0) is returned) console.info (-1/0) // a decimal number may be returned for division of Two integer numbers, which is the same as console.info (4/3) in java and other advanced languages. // 1.3333333333333333 lele.info (typeof NaN) is returned) // NaN is the value of a number, indicating that it is not a number (not a number) // For example, parseInt ("abc"), NaN console.info (parseInt ("abc") is returned ")) console.info (. 6) // indicates a number of 0.6 // NaN and other data calculations return a NaN console.info (NaN-199 );


3 boolean has only two values: true and false
For characters, numeric values can be converted to true or false in boolean.
The character can be converted to the boolean type. Generally, the value is true, and the value of null is false.
Convert a number to the boolean type. Generally, the value is true, and the value 0 is false.
Objects can also be converted to the boolean type, where the null object is converted to false.
Two common types can be used! Convert to boolean type

// Define a Boolean variable with only two values true false var bool = true; // It is equivalent to new Boolean (true) console.info (bool. toSource () // The character can be converted to the boolean type, which is generally true, and the empty character is false if ("abc") {lele.info ("can be converted to the boolean type, it is usually true, and the null character is false ")} // The number is converted to the boolean type. Generally, it is true, 0, NaN is false if (! NaN) {lele.info ("numeric conversion to boolean type, usually true, 0 is false")} // you can use either of the following for common types! Convert to boolean type // For objects, you can also convert to boolean type if (! Null) {lele.info ("Convert object to boolean ")}
4. For more details about function types, refer to the next topic on functions.
There are three defining methods for a function: Function statement, function direct volume, and Function constructor.
// Define and declare a function var fun1 = function () {console.info ("This is a function")} // execute a function fun1 (); console.info (fun1.length) // return the number of parameters of the defined function lele.info (fun1.arguments) // The arguments variable must be used in the defined function to obtain the actual number of parameters // call () of the Function () the method and apply () method are described in detail in the next section. They are mainly used to change the function's console.info (fun1.call () // The toSource method is not necessarily available, firefox supports the definition of the return function console.info (fun1.toSource () // defines a function using arguments // for functions with return values, the return expression must end with a semicolon, and put it to function fun2 (x, y) {// number of actual parameters alert (arguments. length) // specific parameter value alert (arguments [0])} // Execute function fun2 );
5. object objects refer to specific instance objects, not classes.
Simple Object Representation {'name': 'hangsan ', 'age': 18}
PropertyIsEnumerable attribute judgment can be made through a loop of for... in.
HasOwnProperty attributes, not prototype attributes
// Define an object var obj = {'name': "zhangsan", 'age': 18} // each object has a variable obj. prototype = 123 // constructor console.info (obj. constructor () // returns true false console.info (obj. hasOwnProperty ('name') // returns the console.info (obj. valueOf () // propertyIsEnumerable determines if an object is not a prototype property. // you can use .. in loops out console.info (obj. propertyIsEnumerable ("name") for (var I in obj) {console.info (I )}
6 undified type variables are declared, but they are not assigned values. In fact, they are equivalent to Null.
Var a; // only declaration, no value assigned, value: undefined console.info (a) // undefined console.info (typeof undefined) // two values are equal to lele.info (undefined = null)






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.