声明只使用VAR在JavaScript中的布尔 [英] Declaring a boolean in JavaScript using just var

查看:86
本文介绍了声明只使用VAR在JavaScript中的布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我宣布一个JavaScript布尔变量是这样的:

If I declare a JavaScript boolean variable like this:

var IsLoggedIn;

和然后,用初始化它真正的 1 ,是安全的呢?或将与 1 初始化它把这些变量是多少?

And then initialize it with either true or 1, is that safe? Or will initializing it with 1 make the variable a number?

推荐答案

类型取决于你的初始化:

Types are dependent to your initialization:

var IsLoggedIn1 = "true"; //string
var IsLoggedIn2 = 1; //integer
var IsLoggedIn3 = true; //bool

但是看看这个例子:

But take a look at this example:

var IsLoggedIn1 = "true"; //string
IsLoggedIn1 = true; //now your variable is a boolean

您变量的类型取决于在JavaScript中分配的值。

Your variables' type depends on the assigned value in JavaScript.

这篇关于声明只使用VAR在JavaScript中的布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆