Double-class judgment and data type conversion

1. Use typeof() to determine the data type
   var i = 100;
    alert(typeof (i));  //number

Calling typeof() will return the following results: string, number, Boolean, object, function, undefined;

Note: Object is returned when null is interpreted, and null is considered a placeholder for object.

2.string String type: length attribute, the length of the output string, including spaces. charAt() method: output the character at the subscript in ()

3.number digital type: (1) regardless of floating-point numbers and integers

4. Object type (directivity data type): object (including Array, Function, Date, Json, etc.)

    • An object created with the new operator plus the name of the class to be instantiated, such as:
          var obj=new Object(); //The constructor creates the object
    • Declare an object with empty {} (json) like:
          var obj={}; // create an object literal
    Objects are one of the basic data types in JavaScript. An object is a compound value: it aggregates multiple values ​​(primitives or other objects) that can be accessed by name. Objects can also be thought of as unordered collections of properties, each of which is a name/value pair. Property names are strings, so we can think of objects as mappings from strings to values. Objects
    can add custom properties just like our page elements, such as:
        obj.abc = 123; // can also be written as
        obj={"abc":123} //Attribute name: value
    Getting elements on the previous learning page through the dot operation (obj .name ) and through the bracket operation (obj["name"]) also applies to all objects
  1. Use an object to make a namespace (namespace), and global variables will be bound to the window. If different JavaScript files use the same global variables or define top-level functions with the same name, it will cause naming conflicts, which are difficult to find. One way to reduce conflicts is to bind all variables and functions into a single global variable. E.g:
        var MYAPP = {};// The only global variable MYAPP
        MYAPP.name = 'myapp';// other variables
        MYAPP.version = 1.0;// other variables
        MYAPP.foo=function () { return 'foo';};//Other functions
    Putting all your own code into the unique namespace MYAPP will greatly reduce the possibility of global variable conflicts. Many well-known JavaScript libraries do this, such as: jQuery, YUI, etc.

To determine whether it is double equal:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled document</title>
</head>
<body>
<script>
//== Data type conversion will occur
console.log(1=="1"); //true, double etc. only judge the value
console.log(1==="1"); //false, the third is to judge the data type first, the data types are not equal, the value is false
console.log("11"=="9");//false, the values ​​are not equal
console.log(1==true);//true; true is converted to a data value of 1
console.log(2==true);//false
console.log(null==0);//false, no data type conversion occurs for null
console.log(undefined==0);//false, undefined does not convert data types
console.log(undefined==null);//ture;
console.log([]==[]);//false;
console.log([]==![]);//true;
</script>
</body>
</html>

Convert string type:

  1. The 3 main primitive values ​​all have toString methods that can convert themselves to strings, like Number's toString method:
        var iNum = 10;
        alert (iNum.toString ());
  2. The only difference between String() casts and toString() methods is that casts to null or undefined values ​​can produce strings without throwing an error, as in:
        var s1=null.toString(); //error
        var s2=String(null);      //"null"
  3. Invisible conversion + (string hyphen) of the string corresponding to the above cast (i.e. explicit conversion)
        var s="100";
        alert(a+100) ;
    The output result is 100100, and it is converted into a string. When the number is added to the string, it will be converted into a string

Convert number type method:

  1. Number() method, such as
        var s = "100";
        alert(Number(s)+100);  //200
    Number() will try to convert the incoming value to a number as a whole. The conversion rules are:
    • Strings "" (empty) and " " (space) are converted to 0
    • boolean true and false become 1 and 0
    • An empty object null will become 0
    • Functions, json, undefined (undefined) and other objects will become NaN (not a number)
    • If the special array is empty or has only one data (string, null or undefined), the string will be converted according to the rules of the previous string. Empty, null and undefined will be converted to 0, and multiple data will become NaN
    Where Number is not suitable: When a string is var b = "100px", it will become NaN, so Number is used less when converting strings, and parseInt() and parseFloat() are generally used
  2. parselnt () will judge each character in turn from left to right, and output the previous number after encountering a non-number (it will recognize some special symbols, such as +-space), if the data is not a string, it will return NaN, and only keep it Integer, decimal point and the part after it will be rounded off, it is best to write parseInt(b,10), the latter represents decimal
  3. parseFloat is the same as above, but keeps the numbers after the 1st decimal point
  4. Number()/parseInt()/parseFloat() are all methods of coercive type conversion of numbers, and -, *, /, %, ++, -- also make the operation result of number string and number string or number into numeric types, they are implicit conversions of numeric types
  5. No matter which conversion method is used, if the conversion of the number fails, NaN will be returned. NaN (not a number) is the product of the failure to convert the numeric type, but it still becomes the number type. Once it appears, it means that your code is illegal. operate
  6. NaN converts boolean type to false, which is not equal to itself (and all other types are equal to itself)
  7. isNaN(): is not a number, used to determine whether a number type is not a number, returns true if it is not a number, and returns false if it is a number. If the judged value is not of type number, it will find Number to judge, and if the converted result is NaN, it will return true

boolean type conversion

1.>, < will make the comparison results of strings and numbers display correctly, but two strings cannot, such as: '10'>9 and '10'>'9', the former is true and the latter is false, because the character The comparison between strings is from left to right, bit by bit, unicode encoding size

2. When the value to be converted is a string with at least one character, a non-zero number or object, the Boolean() function will return true, if the value is an empty string, number 0 or null, it will return false

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324882497&siteId=291194637