# javascript - 运算符 - 谷歌code style

## 有没有更好的写作方式v=(v== 0?1:0); (20)

``````v = (v == 0 ? 1 : 0);
``````

## 解决方案列表

• `v = 1*!v`
• `v = +!v`
• `v = ~~!v`

• `v = 1-v`

## 解决方案1

``````v = 1*!v
``````

• `-123`将转换为整数`0`
• `-10`将转换为整数`0`
• `-1`将转换为整数`0`
• `0`将转换为整数`1`
• `1`将转换为整数`0`
• `2`将转换为整数`0`
• `60`将转换为整数`0`

## 解决方案2

``````v = +!v
``````

## 解决方案3

``````v = ~~!v
``````

``````v = dirac_delta(0,v)
``````

``````v = characteristic_function({0},v)
``````

∫X fdμ=Σx ＆in; X f（x）

1. 单身人士套装的特色功能，
2. 三角函数，
3. 克罗内克三角函数。

Another creative way of doing it, with `v` being equal to any value, will always return `0` or `1`

``````v = !!v^1;
``````

If possible values for v are only 0 and 1, then for any integer x, the expression: v = Math.pow((Math.pow(x, v) - x), v); will toggle the value.

I know this is an ugly solution and the OP was not looking for this...but I was thinking about just another solution when I was in the loo :P

Untested, but if you're after a boolean I think `var v = !v` will work.

Well, As we know that in javascript only that Boolean comparison will also give you expected result.

ie `v = v == 0` is enough for that.

Below is the code for that:

``````var v = 0;
alert("if v  is 0 output: "+ (v == 0) );

setTimeout(function(){
v = 1;
alert("if v  is 1 Output: "+ (v == 0) );
}, 1000);``````

`v = inv(v)`

``````v = 1 - v;
``````

``````v ^= 1;
``````

### 编辑：

`v = 1 - v` ，或`v ^= 1``v= +!v`行将完成所有工作，但它们构成了我所称的黑客行为。 这些并不是美丽的代码，而是具有预期效果的廉价技巧。 `1 - v`不通信“在0和1之间切换值”。 这会让你的代码表现力减弱，并引入一个地方（尽管很小），另一个开发人员将不得不分析你的代码。

``````v = ~(v|-v) >>> 31;
``````

``````v = v ? 0 : 1;
``````

``````v = !v;
``````

``````if (v === 0)
{
v = 1;
}
else
{
v = 0;
}
``````

``````v = !v;
``````

`v`是什么意思？

``````public class Status
{
public Status(int value)
{
_actualValue = value;
}
public Status(Status status)
{
_actualValue = status._actualValue == 0 ? 1 : 0;
}

//some equals method to compare two Status objects
}

var status = new Status(0);

Status = new Status(status);
``````

``````v = +!v;
``````

（在C中它将是简单的`++v%=2`

PS。 是的，我知道这是双重任务，但这只是对C方法的原始重写（它不会按原样工作，导致JS预增加运算符不能返回左值。

``````v = !v * 1
``````

multiplying by 1 to turn boolean into number

``````v = v == 0 ? 1 : 0;
``````