Declaring a boolean in JavaScript using just var

后端 未结 8 1591
余生分开走
余生分开走 2020-12-23 02:54

If I declare a JavaScript boolean variable like this:

var IsLoggedIn;

And then initialize it with either true or 1

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-23 03:30

    If you want IsLoggedIn to be treated as a boolean you should initialize as follows:

    var IsLoggedIn=true;
    

    If you initialize it with var IsLoggedIn=1; then it will be treated as an integer.

    However at any time the variable IsLoggedIn could refer to a different data type:

     IsLoggedIn="Hello World";
    

    This will not cause an error.

提交回复
热议问题