The six types are String String Number Number Boolean value Null Null value undefined Object Object

The first five are basic data types, and Object is a reference data type

First, take a look at typeof

The typeof operator returns a string representing the data typeof the expression.

Typeof can return the following types: number, string, Boolean, undefined, NULL, object, function, and the new symbol (see MDN)

Developer.mozilla.org/zh-CN/docs/…

Common return values

// The current browser is Chrome; var num = null ; console.log(typeof num); //object var num = /s/ ; console.log(typeof num); //object var num = "a" ; // "123" console.log(typeof num); //string var num = a; console.log(typeof (num)); Var num = 123; console.log(typeof num); //number var num = NaN; console.log(typeof num); //number var num = undefined ; console.log(typeof num); //undefined var num; console.log(typeof num); / / undefined var num = [1, 3]. console.log(typeof num); //object var num = true; console.log(typeof num); //boolean var num = new Array(); console.log(typeof num); //object var num =function(){}; //var num =function; Console. log(typeof num); //function var num =new Object(); console.log(typeof num); //object function Kkb(name){ this.name =name; } var num = new Kkb(); console.log(typeof num); //object function Kkb(name){ this.name =name; } var num = new Person(); console.log(typeof num.name); //undefined function Kkb(name){ this.name =name; } var num = new Kkb("hello"); //var num = new Person(123); -->number console.log(typeof num.name); //stringCopy the code

2. Cast

  • Casting a data type to another data type

  • Conversion basically means converting other data types. Convert to String, Number, Boolean

  • 2.1 Convert to String

    • toString()

    A. Call toString() on the converted data type;

    B. This method will not affect the original variable, it will return the conversion result;

    C, note that null and undefined do not have toString() methods

    • String()

    A. Call String() and pass the loaded data to the function as an argument;

    B. This method will not affect the original variable, it will return the conversion result;

    C. When using String(), null and undefined can be converted to String;

    D, toString() for Number, Boolean; Null and undefined will not be called toString(), but directly converted to null and undefined.

  • 2.2 Convert to Nuber

    • Number()

    A. If the string is a pure number, it is directly converted to a number.

    B. If the string contains non-numeric content or is undefined, it is converted to NaN

    C. If the string is an empty string or space, it is converted to 0

    D, if Boolean; True converts to 1, false converts to 0;

    E. If the value is Null, the value is 0

    • parseInt() / parseFloat()

    A, parseInt() converts a string to an integer

    B, parseInt() : read from left to right, take out the valid integer content, stop reading on the first non-numeric content, if the first character is non-numeric conversion NaN; C, parseFloat() converts a string to a float, just like parseInt(), except that it takes a valid decimal;

    Var a = '111.1111.11' var b = kkb111.11 parseFloat(a) // 111.111 parseFloat(b) // NaNCopy the code

    ParseInt () or parseFloat(), for non-strings, it will convert it to String first and then operate on it;

  • 2.3 Convert to Boolean

    • Boolean()

    A, numeric Boolean values, all except 0 and NaN are true;

    B, string to Boolean, except empty string true;

    C, null, and undefined convert to Boolean values, all false;

    D) Objects are also converted to true;

  var KKB=100;
  var KKBS='100';
  console.log(typeof KKB);            //number
  console.log(typeof KKB);           //string  
  console.log(typeof String(KKB));    //string
  console.log(typeof Number(KKBS));   //number
Copy the code

Implicit conversion

1. Convert to String

Algorithm operator (+), any value and string +, will be converted to the string, and then the string is combined; For multiple values +, work from left to right, paying attention to numeric types;

You can use this feature to convert any data type to String by simply (any String + “”); This is an implicit conversion, done automatically by the browser, which actually calls the String() function;

2. Convert to Number

Algorithm operator (- * / %), any value of – * /, will be converted to Number, and then the operation; You can use this feature to convert any data type to Number by simply converting (any string -0) (any string * 1) (any string / 1) to Number. This is an implicit conversion that the browser does automatically, essentially calling Number();

The unary operator (+(plus sign)), which converts non-number values to Number and then evaluates; An implicit type conversion works the same way as the number () function.

3. Convert to Boolean

Logical operators (! Than),! Invert a Boolean value; If a non-Boolean value is negated, it is converted to a Boolean value and negated; It can be used to invert other data types twice and convert them to Boolean values. Same principle as Boolean();

4. Add a string to a number, and the number becomes a string.

5. Subtract a number from a string, and the string becomes a number. NaN is converted if the string is not a pure number. Same thing with strings minus numbers. Subtracting two strings is also converted to numbers first.

6. Multiplication, division, greater than, less than and subtraction are the same.

``` console.log(1+'2') //12 string console.log(1-'2')//-1 number console.log(10-'kkb') //NaN console.log(10-'10a') //NaN  console.log(10*'20') //200 number console.log('10'*'20') //200 number console.log(2/'1') //2 number console.log('2'/'1') //2 number console.log('2'/'kkb') //NaN ```Copy the code

7, == implicit conversion ()

A. defined equals to null b. When comparing strings with numbers, the string is changed to number C. D. When a string is compared with a Boolean, Log ('0'==0) // True string to number console.log(0==false) //true Boolean to number Console. log('0'==false) // both convert to numbers console.log(null==false) //false console.log(undefined==false)//false ' 'Copy the code

Ok, that’s all, sharing is a virtue, heh heh!