Default value of a type in javascript

前端 未结 3 1996
长情又很酷
长情又很酷 2021-02-15 00:48

Is there a function in JavaScript that returns the default value for a type name that is passed to it? For example:

var defaultValue = built_in_getDefaultValue(\         


        
3条回答
  •  天涯浪人
    2021-02-15 01:37

    I don't understand why do you ask such a question. Anyways, the default value of a variable in JavaScript is null or undefined.

    For learning purposes, I took this from WikiBooks JavaScript/Variables and Types:

    Variables are commonly explicitly declared by the var statement, as shown below:

    var c;
    

    The above variable is created, but has the default value of undefined. To be of value, the variable needs to be initialized:

    var c = 0;
    

    After being declared, a variable may be assigned a new value which will replace the old one:

    c = 1;
    

    But make sure to declare a variable with var before (or while) assigning to it; otherwise you will create a "scope bug."

    And now, when you ask why the value is not 0, it is because, even null or undefined are values, which are not defined. Not defined is different from being defined and empty. It might return 0, when the value is declared and not defined.

提交回复
热议问题