JS returns the data type 5 class

Source: Internet
Author: User

Calling the TypeOf operator on a variable or value returns one of the following values:

    • Undefined-If the variable is of type undefined
    • Boolean-If the variable is of type Boolean
    • Number-If the variable is of type number
    • String-If the variable is of type string
    • Object-If the variable is a reference type or a Null type

typeof operator

The typeof operator has a parameter, which is the variable or value to check. For example:

Output "string" alert (typeof 86); Output "Number"
Undefined type
As mentioned earlier, the Undefined type has only one value, which is Undefined. When the declared variable is not initialized, the default value of the variable is undefined.
var otemp;
The value is undefined and differs from the undefined value. However, the typeof operator does not really differentiate between these two values. Consider the following code:
The preceding code outputs all two variables as "undefined", even if only the variable OTEMP2 has never been declared. If you use an operator other than typeof for OTEMP2, it will cause an error, because the other operators can only be used on declared variables. For example, the following code throws an error:
var otemp; Alert (OTEMP2 = = undefined);
When the function has no explicit return value, the returned value is also "undefined", as follows:
------------------------------------------------------
  
  
ToFixed () method
The ToFixed () method returns a string representation of a number with a specified number of decimal places. For example:
Here, the parameter of the ToFixed () method is 2, indicating that two decimal places should be displayed. The method returns "68.00", and the empty string bit is supplemented by the. This method is useful for applications that work with currencies. The ToFixed () method can represent numbers that have 0 to 20 decimal places, and values that exceed this range raise an error.     
Toexponential () method
Another method associated with formatting a number is toexponential (), which returns a string form of a number represented by scientific notation. Similar to the toFixed () method, the Toexponential () method also has a parameter that specifies the number of decimal places to output. For example:
The result of this code is "6.8e+1", as explained earlier, which represents 6.8x10

1

。 The question is, what if I don't know what form (predetermined form or exponential form) to use to represent a number? You can use the Toprecision () method.
Toprecision () method
The Toprecision () method returns the predetermined or exponential form of a number based on the most meaningful form. It has a parameter, which is the total number of digits (not including the exponent) used to represent the number. For example
The task of this code is to represent the number 68 with a single digit, and the result is "7e+1", which means 70 in a different form. Indeed, the toprecision () method rounds the logarithm. However, if you use a 2-digit number to represent 68, it is much easier:
Of course, the output is "68", because this is exactly the exact representation of the number. But what if you specify more digits than you want?
In this case, toprecision (3) is equivalent to toFixed (1) and the output is "68.0". The  toFixed (), toexponential (), and Toprecision () methods perform rounding operations to correctly represent a number with the correct number of decimal digits.    
CharAt () and charCodeAt () methods
The String object also has a number of methods. First, two methods CharAt () and charCodeAt () access a single character in a string. Both methods have a parameter, which is the position of the character to manipulate. The CharAt () method returns a string containing the character at the specified position:
In the string "Hello World", the character at position 1 is "e". In the section "ECMAScript primitive types" We said that the position of the first character is 0, the position of the second character is 1, and so on. Therefore, the call to CharAt (1) returns "E". If you want to get a character code instead of a character, you can call the charCodeAt () method:
This example outputs "101", which is the character code of the lowercase letter "E".   
Oncat () method
Next is the concat () method, which connects one or more strings to the original value of the string object. The method returns a string primitive value, keeping the original string object unchanged:
In the above code, the call to the Concat () method returns "Hello World", while the String object is still "Hello". For this reason, it is more common to concatenate strings with a plus sign (+), because this form logically indicates the true behavior:
  
IndexOf () and LastIndexOf () methods
To date, a method of connecting strings has been discussed to access a single character in a string. But what method should I call if I can't determine if a character actually exists in a string? At this point, the IndexOf () and LastIndexOf () methods can be called. The IndexOf () and LastIndexOf () methods return the position of the specified substring in another string, or 1 if no substring is found. The difference between the two methods is that the IndexOf () method retrieves the string starting at the beginning of the string (position 0), and the LastIndexOf () method retrieves the substring starting at the end of the string. For example:
Here, the first "O" string appears at position 4, "O" in "Hello", and the Last "O" appears at position 7, "O" in "World". If there is only one "O" string in the string, the IndexOf () and LastIndexOf () methods return the same position.       
Localecompare () method
The next method is Localecompare (), which sorts the strings. The method has one parameter-the string to compare, and one of the following three values is returned:
    • Returns a negative number if the string object precedes the string in the argument in alphabetical order.
    • Returns 0 if the string object equals the strings in the argument
    • Returns a positive number if the string object is sorted alphabetically in the arguments.
Note: If a negative number is returned, the most common is-1, but the actual return is determined by the implementation. If a positive number is returned, then again, the most common is 1, but the real return is determined by the implementation. Examples are as follows:
In this code, the string "yellow" is compared with 3 values, namely "brick", "yellow", and "zoo". Since "Yellow" is in alphabetical order after "Brick", Localecompare () returns 1; "Yellow" equals "yellow", so Localecompare () returns 0; "Zoo" is located in "yellow" After that, Localecompare () returns-1. Again, since the value returned is determined by the implementation, it is best to call the Localecompare () method in the following way:
var oStringObject1 = new String ("Yellow"); var oStringObject2 = new String ("Brick"); var iresult = Steststring.localecompare ("Brick"); if (Iresult < 0) {alert (OStringObject1 + "comes before" + OStringObject2);} else if (Iresult > 0) {alert (OStringObject1 + "comes after" + OStringObject2);} else {alert ("The Strings is equal");}
With this structure, you can ensure that this code works correctly in all implementations. The Localecompare () method is unique in that the region in which the implementation is located (locale, which refers to the country/region and language) illustrates exactly how this approach works. In the United States, English is the standard language that ECMAScript implements, Localecompare () is case-sensitive, and uppercase letters are followed by lowercase letters in alphabetical order. In other regions, however, this may not be the case.   
Slice () and substring ()
ECMAScript provides two ways to create string values from substrings, namely slice () and substring (). Both of these methods return substrings of the string to be processed and accept one or two parameters. The first parameter is the starting position of the substring to get, and the second argument (if used) is the position to get before the substring terminates (that is, the character at the end of the fetch is not included in the returned value). If you omit the second argument, the terminating bit defaults to the length of the string. As with the concat () method, neither the slice () and the substring () methods change the value of the String object itself. They return only the original string value, keeping the string object intact.
In this example, slice () and substring () use the same, and the return value is the same. When only argument 3 o'clock is available, two methods return "Lo World" because the second "L" in "Hello" is on position 3. When there are two parameters "3" and "7", the value returned by the two methods is "Lo W" (The Letter "O" in "World" is located on position 7, so it is not included in the result). Why are there two ways to function exactly the same way? In fact, these two methods are not exactly the same, but only when the parameters are negative, they handle the arguments in slightly different ways. For a negative parameter, the slice () method adds a parameter to the length of the string, and the substring () method handles it as 0 (that is, ignores it). For example:
This shows the main differences between the slice () and the substring () methods. When the parameter is only 3, slice () returns "Rld", and substring () returns "Hello World". This is because for the string "Hello World", Slice ("3") will be converted to slice ("8"), while substring ("3") will be converted to substring ("0"). Similarly, when using Parameters 3 and 4, the difference is also obvious. Slice () will be converted to slice (3, 7), as in the previous example, returning "Lo W". The substring () method interprets two parameters as substring (3, 0), which is actually substring (0, 3), because substring () always takes the smaller number as the starting bit and the larger number as the terminating bit. Therefore, substring ("3,-4") returns "Hel". The last line of code here is used to illustrate how to use these methods.     
toLowerCase (), toLocaleLowerCase (), toUpperCase () and toLocaleUpperCase ()
The last set of methods to discuss involves case-to-casing conversions. There are 4 ways to perform case conversions, that is,
    • toLowerCase ()
    • toLocaleLowerCase ()
    • toUpperCase ()
    • toLocaleUpperCase ()
The first two methods are used to convert the string to lowercase, and the last two methods are used to convert the string to uppercase. The toLowerCase () and toUpperCase () methods are primitive and are implemented in the same way as in java.lang.String. The toLocaleLowerCase () and toLocaleUpperCase () methods are implemented based on a specific region (the same as the Localecompare () method). In many regions, zone-specific methods are identical to the common approach. However, there are several languages that apply specific rules (such as Turkish) to Unicode case conversions, so you must use a region-specific method for the correct conversion.
In this code, toUpperCase () and tolocaleuppercase () all output "Hello World", and toLowerCase () and tolocalelowercase () output are all "Hello World". In general, it is safer to use a zone-specific method if you do not know which encoding to run a language in.     
instanceof operator

Storing a value with a reference type when using the TypeOf operator will cause a problem, regardless of what type of object is referenced, and it returns "Object". ECMAScript introduced another Java operator, instanceof, to solve this problem.

The instanceof operator is similar to the typeof operator to identify the type of object being processed. Unlike the TypeOf method, the Instanceof method requires the developer to explicitly confirm that the object is of a particular type. For example:

Output "true"

This code asks "is the variable ostringobject an instance of a String object?" "Ostringobject is indeed an instance of a String object, so the result is" true ". Although not as flexible as the TypeOf method, the Instanceof method is useful in cases where the TypeOf method returns "Object".

JS returns the data type 5 class

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.