A brief introduction to six data types and special attention points of javascript

  • 2020-03-30 00:59:40
  • OfStack

There are six data types commonly used in js: String, Null, Number, Boolean, and Object.

1. Attention of typeof

When it comes to data types, the operator typeof is mentioned. Note:

Typeof is an operator, not a method. Although we often use typeof() to get the data typeof the object.

2. Take typeof&cake for null; Is object (because null is an empty object reference), and typeof is function


alert(typeof null);    //Returns the object
function demo(){
      alert('demo');
    }
alert(typeof demo);     //Return   The function

2. Set initial values for object variables of various data types

Note that if an Object variable of type Object starts not knowing what to assign, don't   Var demo = {}; It is best to set it to null;


var d2=null;
d2={'key':"shit"};
var d3='';//String default
var d4=0;   //The Number type is initially set to 0
var d5=null; //Set the initial default value for the object type

3. Difference between undefined and null and points of attention

1. If "==" is used for comparison, they are equal, because they are comparing values

2. There are two ways to distinguish them (their core is to compare their data types)

  1) distinguish them with typeof  

  2) use the congruent "==="   : compares values and data types, and returns true only if they are all the same


alert(undefined == null);             //true
alert(typeof undefined == typeof null); //false
alert(undefined === null);             //true

4. Boolean attention points

1, true and 1 comparison is the same, false and 0 comparison is the same (is "==" comparison), because the internal will achieve the conversion of data types, true to 1, false to 0. We'll talk more about that.   But use "==="   They are not equal, because their data types are not the same.

2, display conversion to Boolean, using the Boolean() method to display the conversion, you need to pay attention to the various data types, when to convert to true and when to convert to false

  1) String, as long as it's not & cake; Empty string   Both convert to true

  2) Number type, as long as it is not 0, even if it is negative, will be converted to true

  3)Object type, as long as the type is not null, will be converted to true

  4) Undefined type, all converted to false

I'm not going to do a demo, but you can try it yourself.

3, (***) if()  Statement (s)   Inside is a Boolean function called

5. Pay attention to the Number type

  1. Float type cannot do accurate calculation


alert(0.1+0.2);//Return 0.300000000000000004

3, NaN (Not a Number)

  1) var d = 0/0;   Note: in js an error is not reported, but NaN is returned

  2) can be obtained through number.nan

  3) NaN and any object will return NaN

  4) isNaN() determines whether it isNaN


alert(isNaN(NaN));//true
alert(isNaN(12));//false
alert(isNaN('123'));//False: because a number of string type can be automatically converted to & PI; digital
alert(isNaN('lew'));//true
alert(isNaN(false));//(*) false: because bool  Values can be converted to Numbers, true to 1, false to 0

5) how isNaN() works internally: the same applies to objects. Principle: the prime minister calls the valueOf() method of the object, and if it can be converted to a number, makes the judgment directly. If not, call the toString() method again and test the return value.

    ValueOf () internally invokes the toObject() method, and the internal execution principle of the two methods is as follows:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201312/20131220155941741.jpg" >


var box={
        //Rewrite   Box object toString()& cake; methods
        toString:function(){
              return '123';
            }
    };
alert(isNaN(box));//false
alert(box);//123     Alert () internally also invokes the valueOf()& send of the object first; Then call the toString() method

  6) convert other data types to Number types

    There are three functions: Number(): can be converted for all data types; ParseInt () and parseFloat() are converted only to strings.


alert(Number('123'));//123
alert(Number('0234'));//234
alert(Number(true));//1
alert(Number(null));//(**)0
//Except for the & cake above; Everything else returns NaN
alert(Number(undefined))//NaN

The internal implementation of Number() : the same as isNaN() also calls valueOf()& PI; Then call toString(). So it is conceivable that the performance is relatively poor. So whenever the object to be transformed is a string, call parseInt()& PI; Or parseFloat() because they don't need to make type judgments internally.

The parseInt () and the parseFloat ()   Call note: from the first character of the number to the first part of the number of the character before the number & PI; This part of the string is converted to a number


alert(parseInt('123leb'));//123
alert(parseInt('123leb345'));//123
alert(parseInt('len234'));//NaN

ParseInt () gets only Numbers when the argument in parseInt() is of type float; The integer part


alert(parseInt(56.12));//56

  6. String type

  1) (* important *) strings in ECMAScript have invariance: strings are not changed after they are created.

  To change a string variable that has been assigned, first destroy the string in the variable, and then use a string & PI that contains the new value. Fill variables.


var d='hello';
d=d+' shit';//Execution: assign a copy of 'hello', then empty the string in d, concatenate the string 'hello' and 'shit', and assign the value to the d variable. (so the value of the string will not change once it is created)

  2) the toString() method converts other data types toString types. But if null or undefined&sponge; If you do an operation, you will report an error.

  3) but the String() method can also implement the toString() effect, but can operate on null and undefined.

  The inside story: call toString() first, and if you can convert it to a string, return the result directly. No, then determine whether it is null or undefined, and return 'null' or' undefined'

Summary: if you know that the variable can't be null&sponge; Or undefined, so toString() is better than String(), because String() has to be judged internally, so it's bad.


Related articles: