JavaScript primitive types and corresponding objects

后端 未结 2 971
一个人的身影
一个人的身影 2020-12-03 04:09

In JavaScript not every data is an object. There exist a few primitive types, like strings, numbers and Boolean which are not objects. For each of these types there exists a

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 04:35

    What is the advantage of keeping two separate representations for numbers, strings and Booleans?

    Performance

    In what context could one need the distinction between primitive types and objects?

    Coercion comes to mind. 0 == false while new Number(0) != false

    So for instance:

    var a = new Boolean(false);
    if(a) {
      // This code runs
    }
    

    but

    var a = false;
    if(a) {
      // This code never runs
    }
    

    You can read more about coercion here: JavaScript Coercion Demystified

提交回复
热议问题