ChatGPT解决这个技术问题 Extra ChatGPT

Is there a better way of writing v = (v == 0 ? 1 : 0); [closed]

Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago. Improve this question

I want to toggle a variable between 0 and 1. If it's 0 I want to set it to 1, else if it's 1 I want to set it to 0.

This is such a fundamental operation that I write so often I'd like to investigate the shortest, clearest possible way of doing it. Here's my best so far:

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

Can you improve on this?

Edit: the question is asking how to write the above statement in the fewest characters while retaining clarity - how is this 'not a real question'? This wasn't intended to be a code-golf exercise, though some interesting answers have come out of people approaching it as golf - it's nice to see golf being used in a constructive and thought-provoking manner.

This looks already simple/clear/short to me.
trickery: v = +!v;
If 'better' also means 'faster': jsperf.com/v-0-1-0.
@Mobinga: +1. This is as simple as it should get. All the other answers i see are confusing, and some of them change the logic; introducing bugs.
@holodoc A better solution to expressing your opinion would be to create an answer that says you feel the original is the best way, and elaborate on WHY you feel that is the case. that also allows other people to easily side with your answer by voting it up.

G
Guffa

You can simply use:

v = 1 - v;

This of course assumes that the variable is initialised properly, i.e. that it only has the value 0 or 1.

Another method that is shorter but uses a less common operator:

v ^= 1;

Edit:

To be clear; I never approached this question as code golf, just to find a short way of doing the task without using any obscuring tricks like side effects of operators.


These are beautiful lines of code, like finely cut glass or rare flowering orchids. I love how you've reached straight through the logical layer and dealt directly with the math of 1 and 0 with the most optimal operations possible. I'll be using these in my own projects, but unfortunately when I need the code to be understood by coworkers I'll have to default to a more logic-based approach. Thank you though, your answer has made my day.
You can always add a comment saying what the code does.
@Kevin: it's not true that this clearly does not answer the question as asked. The q said "I want to toggle a variable between 0 and 1." A very reasonable interpretation is that this implies that the value of the variable is already either 0 or 1. This interpretation / limitation was made explicit in the answer. What in the world can you object to?
@Matthew to say that someone who does not find v ^= 1 clear should stop programming is a little harsh I think. As previously stated its not one of the more common operators and it does not do the same thing as v = v ^ 1. Also the operator means something completely different in different languages (VB). Yes a quick look-up will tell you that its an XOR operator and you will understand what its doing, but to many it may not be obvious at first glance. I don't think that means you need to quit your job.
If you save 15 characters by writing this but then use 40 characters of comments to explain it, is it really an improvement?
J
Joe

Since 0 is a false value and 1 is a true value.

v = (v ? 0 : 1);

If you are happy to use true and false instead of numbers

v = !v;

or if they must be numbers:

v = +!v; /* Boolean invert v then cast back to a Number */

just put the magic + before that !. Ohhh that looks soooo dirty! but cool :p
@Quentin - this will break the guy's code if he has switch(v){ case 0 ... or if(v == 0) or if v === 0. You're changing his result in addition to his approach....
Make a smiley of it: ({v :+! v}).v.
@Brian: not with that magic + sign! ¯\_(ツ)_/¯
+1 for v = (v ? 0 : 1) as clear and idiomatic JS.
P
Prusse

v = (v + 1) % 2 and if you need to cycle through more values just change 2 for (n + 1). Say you need to cycle 0,1,2 just do v = (v + 1) % 3.


I love the cycle! That's really smart.
+1 for thinking about the toggle as a cycle. Simple considering how flexible it is.
+1 for providing a modular solution that is also robust. In practice you would probably want to use a const variable in place of the magic numbers 2, 3, ...
Shorter (hackier) way: ++v % 3 if you want to cycle through 0, 1, 2
D
Daniel

You could write a function for it and use it like:

v = inv(v)


+1 I strongly agree this is the cleanest solution (...but then what goes in the inv function :P)
I wish I could give this answer more than just +1. If common code is not blindingly obvious, it should be wrapped in a function with a descriptive name.
@TehShrike do you consider inv to be a descriptive name?
@Bennett McElwee: inv = invert. Always seemed like a common abbreviation to me.
Why abbreviate it then, call it 'invert' or 'toggle' and have the code be readable. it's not as if we are trying to fit this stuff onto an 80 column punchcard or something.
B
Brian

If you don't care about any possibility other than 1:

v = v ? 0 : 1;

In the above case, v will end up being 1 if v is 0, false, undefined or null. Take care using this kind of approach - v will be 0 even if v is "hello world".


That won't flip the value. You mean v = v ? 0 : 1.
@Guffa - +1 for catching my dyslexia...
This is my favourite logic-based answer which I can use with coworkers (I'm seeing @Guffa's answer as number-based). I like how you've taken the unnecessary test (v==0) and brackets out, stripping it down to the absolute minimum character count. Thank you.
I'm a big fan of @Guffa's answer too - in the words of lynard skynard, keep it simple man ;)
+1 for understanding "truthy" values.
R
Ray

Lines like v = 1 - v, or v ^= 1 or v= +!v will all get the job done, but they constitute what I would refer to as hacks. These are not beautiful lines of code, but cheap tricks to have the intended effect. 1 - v does not communicate "toggle the value between 0 and 1". This makes your code less expressive and introduces a place (albeit a small one) where another developer will have to parse your code.

Having instead a function like v = toggle(v) communicates the intent at the quickest glance.


-1, seriously, v=1-v doesn't communicate the toggle?
1-v can communicate indeed toggle, but that's that the only thing it communicates. For example, it can also communicate a line with zero at v=1, or a mirroring transformation centered at v=0.5. In this sense it is relatively ambiguous. It is true that knowing that v can only ever be 0 or 1 limits its intended meaning, but that forces other developers (or your future self) to understanding that context before being able to understand this simple line. You can't be much clearer than v = toggle(v)
v ^= 1 is perfectly clear if you understand logical operations, though, which you had better if you're a programmer. I think that's the difference between that and v=1-v; one's a logical operation and one's an arithmetic operation, and we're trying to represent a logical concept not a mathematical one.
@Matthew Read: This is a great statement, which sums up my thoughts well: "we're trying to represent a logical concept not a mathematical one." Even v ^= 1 has some ambiguity, though, as it can be interpreted as a bitwise-xor.
for programmers (and yes, i do mean geeks like us), these are quite clear. They aren't hacks, because they are elegant solutions due to their simplicity.
A
Andrew Stacey

(Honesty and mathematical integrity - given the number of votes on this "answer" - have led me to edit this answer. I held off as long as possible because it was intended as a short quip and not as anything "deep" so putting in any explanation seemed counter to the purpose. However, the comments are making it clear that I should be clear to avoid misunderstanding.)

My original answer:

The wording of this part of the specification:

If it's 0, I want to set it to 1, else set it to 0.

implies that the most accurate solution is:

v = dirac_delta(0,v)

First, the confession: I did get my delta functions confused. The Kronecker delta would have been slightly more appropriate, but not by much as I wanted something that was domain-independent (the Kronecker delta is mainly used just for integers). But I really shouldn't have used delta functions at all, I should have said:

v = characteristic_function({0},v)

Let me clarify. Recall that a function is a triple, (X,Y,f), where X and Y are sets (called the domain and codomain respectively) and f is a rule that assigns an element of Y to each element of X. We often write the triple (X,Y,f) as f: X → Y. Given a subset of X, say A, there is a characteristic function which is a function χA: X → {0,1} (it can also be thought of as a function to a larger codomain such as ℕ or ℝ). This function is defined by the rule:

χA(x) = 1 if x ∈ A and χA(x) = 0 if x ∉ A.

If you like truth tables, it's the truth table for the question "Is the element x of X an element of the subset A?".

So from this definition, it's clear that the characteristic function is what is needed here, with X some big set containing 0 and A = {0}. That's what I should have written.

And so to delta functions. For this, we need to know about integration. Either you already know it, or you don't. If you don't, nothing I can say here will tell you about the intricacies of the theory, but I can give a one sentence summary. A measure on a set X is in essence "that which is needed to make averages work". That is to say that if we have a set X and a measure μ on that set then there is a class of functions X → ℝ, called measurable functions for which the expression ∫X f dμ makes sense and is, in some vague sense, the "average" of f over X.

Given a measure on a set, one can define a "measure" for subsets of that set. This is done by assigning to a subset the integral of its characteristic function (assuming that this is a measurable function). This can be infinite, or undefined (the two are subtly different).

There are lots of measures around, but there are two that are important here. One is the standard measure on the real line, ℝ. For this measure, then ∫ℝ f dμ is pretty much what you get taught in school (is calculus still taught in schools?): sum up little rectangles and take smaller and smaller widths. In this measure, the measure of an interval is its width. The measure of a point is 0.

Another important measure, which works on any set, is called the point measure. It is defined so that the integral of a function is the sum of its values:

∫X f dμ = ∑x ∈X f(x)

This measure assigns to each singleton set the measure 1. This means that a subset has finite measure if and only if it is itself finite. And very few functions have finite integral. If a function has a finite integral, it must be non-zero only on a countable number of points. So the vast majority of functions that you probably know do not have finite integral under this measure.

And now to delta functions. Let's take a very broad definition. We have a measurable space (X,μ) (so that's a set with a measure on it) and an element a ∈ X. We "define" the delta function (depending on a) to be the "function" δa: X → ℝ with the property that δa(x) = 0 if x ≠ a and ∫X δa dμ = 1.

The most important fact about this to get a-hold of is this: The delta function need not be a function. It is not properly defined: I have not said what δa(a) is.

What you do at this point depends on who you are. The world here divides in to two categories. If you are a mathematician, you say the following:

Okay, so the delta function might not be defined. Let's look at its hypothetical properties and see if we can find a proper home for it where it is defined. We can do that, and we end up with distributions. These are not (necessarily) functions, but are things that behave a little like functions, and often we can work with them as if they were functions; but there are certain things that they don't have (such as "values") so we need to be careful.

If you are not a mathematician, you say the following:

Okay, so the delta function might not be properly defined. Who says so? A bunch of mathematicians? Ignore them! What do they know?

Having now offended my audience, I shall continue.

The dirac delta is usually taken to be the delta function of a point (often 0) in the real line with its standard measure. So those who are complaining in the comments about me not knowing my deltas are doing so because they are using this definition. To them, I apologise: although I can wriggle out of that by using the Mathematician's defence (as popularised by Humpty Dumpty: simply redefine everything so that it is correct), it is bad form to use a standard term to mean something different.

But there is a delta function which does do what I want it to do and it is that which I need here. If I take a point measure on a set X then there is a genuine function δa : X → ℝ which satisfies the criteria for a delta function. This is because we are looking for a function X → ℝ which is zero except at a and such that the sum of all of its values is 1. Such a function is simple: the only missing piece of information is its value at a, and to get the sum to be 1 we just assign it the value 1. This is none other than the characteristic function on {a}. Then:

∫X δa dμ = ∑x ∈ X δa(x) = δa(a) = 1.

So in this case, for a singleton set, the characteristic function and the delta function agree.

In conclusion, there are three families of "functions" here:

The characteristic functions of singleton sets, The delta functions, The Kronecker delta functions.

The second of these is the most general as any of the others is an example of it when using the point measure. But the first and third have the advantage that they are always genuine functions. The third is actually a special case of the first, for a particular family of domains (integers, or some subset thereof).

So, finally, when I originally wrote the answer I wasn't thinking properly (I wouldn't go so far as to say that I was confused, as I hope I've just demonstrated I do know what I'm talking about when I actually think first, I just didn't think very much). The usual meaning of the dirac delta is not what is wanted here, but one of the points of my answer was that the input domain was not defined so the Kronecker delta would also not have been right. Thus the best mathematical answer (which I was aiming for) would have been the characteristic function.

I hope that that is all clear; and I also hope that I never have to write a mathematical piece again using HTML entities instead of TeX macros!


+1 - If I could downvote Quentin for not knowing the dirac_delta function I would. I would like to know which college he went to because I don't think I would hire anyone from there if they don't even know something that fundamental to digital processing. I would further downvote Quentin for his inability to grasp the bit of humor you were attempting.
If I downvoted every answer I didn't understand... I would downvote a lot of answers.
Hello Andrew, it's been a while.
@Tim: Good grief, so it has! And how is the Bishop of Bermuda? (Did I remember that right?) I guess this comment thread isn't the best place to catch up, though ... 17 years, isn't it?
@Cԃաԃ "This answer has failed... " sigh. You did read that I'm a mathematician, didn't you? This answer works perfectly for a spherical programmer in a vacuum so I don't know what you're complaining about.
M
Mouna Cheikhna

in general whenever you need to toggle between two values , you can just subtract the current value from the sum of the two toggle values :

0,1 -> v = 1 - v 1,2 -> v = 3 - v 4,5 -> v = 9 - v


This is interesting and creative but potentially dangerous. If v becomes corrupted it will suddenly start to toggle between two different values. (Just something to consider...)
+1 for the interesting idea. not that the OP or anyone else is actually going to use it in this circumstances, but this piece of info should exist somewhere in brain forever.
@oosterwal: Then surely the issue lies with the corruption...
@GManNickG: Sooner or later data corruption will happen so we need to be aware of the consequence of not detecting it. If v is normally used to alternately execute two states from a list of three or more states, then a corruption of v could cause the program to alternately execute two completely different states--this could result in unexpected and undesirable results. The lesson to take away from this is: Always perform plausibility checks on your data.
p
pimvdb

You could do

v = Math.abs(--v);

The decrement sets the value to 0 or -1, and then the Math.abs converts -1 to +1.


This has turned into a 'Generate 10000 ways to do a simple task' competition. And I like all the ways haha.
v = Math.round(Math.sin(Math.PI/(v+3)));
@Benoit - your formula equals 1 for both v=0 and v=1. THIS one is correct though! v = Math.round(Math.cos(Math.PI/((v*2+1)+2)-2*v)); :D
That would never change the value. If v = 1 then v = Math.Abs(-1) which is +1. If v = 0 then v = Math.Abs(-0) which is 0.
+1 And this was my other one... Grr.
M
Michael Berkowski

If it must be the integer 1 or 0, then the way you're doing it is fine, though parentheses aren't needed. If these a are to be used as booleans, then you can just do:

v = !v;

won't this result in Ollie's "v" being set to a boolean result, not an integer?
Yes it will, and I warned that in the sentence above the line of code. If these a are to be used as booleans
n
nidhin
v = v == 0 ? 1 : 0;

Is enough !


More of an opinion than an answer, don't you think?
@Brian: 'better' is all about opinion actually.
I like how you've taken the brackets out - that trims a couple of characters off!
@Brian: The "answer" is "No, there isn't a better way of writing v = (v==0 ? 1 : 0);". Everyone else is finding different ways of playing golf; and not actually answering the question. That's why i up-voted this answer.
T
Tadeck

List of solutions

There are three solutions I would like to propose. All of them convert any value to 0 (if 1, true etc.) or 1 (if 0, false, null etc.):

v = 1*!v

v = +!v

v = ~~!v

and one additional, already mentioned, but clever and fast (although works only for 0s and 1s):

v = 1-v

Solution 1

You can use the following solution:

v = 1*!v

This will first convert the integer to the opposite boolean (0 to True and any other value to False), then will treat it as integer when multiplying by 1. As a result 0 will be converted to 1 and any other value to 0.

As a proof see this jsfiddle and provide any values you wish to test: jsfiddle.net/rH3g5/

The results are as follows:

-123 will convert to integer 0,

-10 will convert to integer 0,

-1 will convert to integer 0,

0 will convert to integer 1,

1 will convert to integer 0,

2 will convert to integer 0,

60 will convert to integer 0,

Solution 2

As mblase75 noted, jAndy had some other solution that works as mine:

v = +!v

It also first makes boolean from the original value, but uses + instead of 1* to convert it into integer. The result is exactly the same, but the notation is shorter.

Solution 3

The another approach is to use ~~ operator:

v = ~~!v

It is pretty uncommon and always converts to integer from boolean.


In JavaScript, you can prefix a variable with + to convert it to a number, so +!v is equivalent to your solution (jAndy's solution in the OP's comments).
@mblase75: Yes, you are right that 1* can be replaced by + when trying to convert boolean into integer. Everything else in my answer remains the same. jAndy's answer is correct, but mine is more detailed. I will add his/her solution to my answer.
@Tadeck +1 for the nice idea.
M
Martin Schlagnitweit

To sum up another answer, a comment and my own opinion, I suggest combining two things:

Use a function for the toggle Inside this function use a more readable implementation

Here is the function which you could place in a library or maybe wrap it in a Plugin for another Javascript Framework.

function inv(i) {
  if (i == 0) {
    return 1
  } else {
    return 0;
  }
}

And the usage is simply:

v = inv(v);

The advantages are:

No code Duplication If you or anybody read this again in the future, you will understand your code in a minimum of time.


Are they? I ran a quick test: jsperf.com/function-vs-no-function/2 and it seems, that the difference is not that much.
7.5ns vs 8.8ns. That extra 1.3 nanoseconds will really kill you.
With all the time I saved, I took my family to Disney World! :-)
Totally correct. People writing "clever" solutions probably never had to maintain other programmers code "gems"
why not function toggle(i){ return i == 0 ? 1 : 0 }?
N
Nina Scholz

This is missing:

v = [1, 0][v];

It works as round robin as well:

v = [2, 0, 1][v]; // 0 2 1 0 ...
v = [1, 2, 0][v]; // 0 1 2 0 ...
v = [1, 2, 3, 4, 5, 0][v]; // 0 1 2 3 4 5 ...
v = [5, 0, 1, 2, 3, 4][v]; // 0 5 4 3 2 1 0 ...

Or

v = {0: 1, 1: 0}[v];

The charme of the last solution, it works with all other values as well.

v = {777: 'seven', 'seven': 777}[v];

For a very special case, like to get a (changing) value and undefined, this pattern may be helpful:

v = { undefined: someValue }[v]; // undefined someValue undefined someValue undefined ...

Can you explain the answer, possibly with jsfiddle (or equivalent)?
@Phalgun, what part? you may try it yourself ...
I am trying to plug some number to test it. For example, consider v = 7, then with OP's answer v = (v == 0 ? 1 : 0), v is 0. Now, with v = [1, 0][v], v is undefined. I think I am missing something here.
you need to declare v and take a value of either 0 or 1.
not really. if you take +!v (as in the answer below), you need no more an array , because you got the value already. in my example v is used without converting to another value or type.
c
c69

I don't know why you want to build your own booleans? I like the funky syntaxes, but why not write understandable code?

This is not the shortest/fastest, but the most clearest (and readable for everyone) is using the well-known if/else state:

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

If you want to be really clear, you should use booleans instead of numbers for this. They are fast enough for most cases. With booleans, you could just use this syntax, which will win in shortness:

v = !v;

This would actually be my preferred answer; but ternary operator vs if-else is a holy war i didn't want to light the fuse on. That said, i think the first line should be === 0. With sample input of 7, the correct output is 0, but this will output 1.
Is there value to silently correcting an input like 7 instead of throwing an exception of some kind? I mean what if some other bit of code got it's hands on the 7 when it was expecting 0 or 1 only, before your code corrected the value?
佚名

Another form of your original solution:

v = Number(v == 0);

EDIT: Thanks TehShrike and Guffa for pointing out the error in my original solution.


The == operator returns a boolean (instead of an integer) under many systems. This would only work if v was defined as an integer, and the language was cool with automatic casting from boolean to integer.
Very true. I was answering the question in the context of javascript (how the question was tagged).
@Kurt Kaylor: Javascript is one of the systems where the == operator returns a boolean, so it doesn't do the same as the original solution.
@Guffa: I am taking advantage of the fact that javascript is a weakly typed language with implicit type conversion (e.g.'0.0000' == 0, 0 == "", false == "", false == 0, 1 == true, etc). I can use the returned boolean value in the same way as I can use an integer of value 0 or 1. Try, for example, to evaluate "2 + true" in a javascript console, you'll get 3.
@Kurt Kaylor: That only works in situations where you force an implicit conversion. Try for example 'opacity:'+true and you end up with opacity:true instead of opacity:1.
M
Marco

I would make it more explicit.

What does v mean?

For example when v is some state. Create an object Status. In DDD an value object.

Implement the logic in this value object. Then you can write your code in a more functional way which is more readable. Switching status can be done by creating a new Status based on the current status. Your if statement / logic is then encapsulated in your object, which you can unittest. An valueObject is always immutable, so it has no identity. So for changing it's value you have to create a new one.

Example:

public class Status
{
    private readonly int _actualValue;
    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);

over engineering?
+1 for making me laugh
D
David G

Since this is JavaScript, we can use the unary + to convert to int:

v = +!v;

This will logical NOT the value of v (giving true if v == 0 or false if v == 1). Then we convert the returned boolean value into its corresponding integer representation.


E
Eng.Fouad

Another way to do it:

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

A
Adam Jurczyk

One more: v=++v%2

(in C it would be simple ++v%=2)

ps. Yeah, I know it's double assignment, but this is just raw rewrite of C's method (which doesn't work as is, cause JS pre-increment operator dosen't return lvalue.


Your C version is illegal; you can't modify the result of a ++ operator (it's not an lvalue). As for v=++v%2, you're modifying v twice. I don't know whether that's well defined in JavaScript , but it's not necessary. v = (v+1) % 2.
At least in c++ it is - because pre-incrementation operator has higher priority and modifies variable 'in place', so it can be used as lvalue. I think that JS implementation of ++ is such that it cant be treated as an lvalue:/ And yeas, this is redundant, but i was trying to show just another method - there are already posted better solutions :)
+1. Can be written as v = (v+1)%2.
b
brandx

If you're guaranteed your input is either a 1 or a 0, then you could use:

v = 2+~v;

B
Blazemonger

Just for kicks: v = Math.pow(v-1,v) also toggles between 1 and 0.


S
S..

define an array{1,0}, set v to v[v], therefore v with a value of 0 becomes 1, and vica versa.


My solution is correct, if you don't like it, write why. Your comment helps nobody. My solution reads a variable, as opposed to making a decision. This requires less CPU.
Y
Yanick Rochon

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

v = !!v^1;

two !! result in nothing in logical maths, you can put !!!! in there and will be same but resulting 4 unneed operations
@Alex: !! will cast to a bool. !!(1) === true and !!(0) === false
@nickf: aha, so i guess v = Boolean(v^1) would be more informational, thanks for explaining - didn't thought about casting
@Alex, "logical maths"? This is why mathematicians are poor programmers. Don't get me wrong; good programmers are also good at understanding mathematics. But this has nothing to do with logical maths. It's a language construct that, given any expression, at least in Javascript, ! will cast it into boolean. Adding another ! will negate that boolean. The casting is implicit here, and the end result works. There is no need for the -1.
i'm not a mathematician at all. you can try this and see yourself jsperf.com/javascript-boolean-test
A
Amit

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


M
Martin Bean

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

Reference: http://www.jackfranklin.co.uk/blog/2011/05/a-better-way-to-reverse-variables


t
teacher
v=!v;

will work for v=0 and v=1; and toggle the state;


A
Amber

If there are just two values, as in this case(0, 1), i believe it's wasteful to use int. Rather go for boolean and work in bits. I know I'm assuming but in case of toggle between two states boolean seems to be ideal choice.


R
Rohit Goyal

v = Number(!v)

It will type cast the Inverted Boolean value to Number, which is the desired output.


a
ankitkanojia

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

i.e. 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);

JSFiddle: https://jsfiddle.net/vikash2402/83zf2zz0/

Hoping this will help you :)


That works for toggling a value, but the value doesn't toggle between 0 and 1, but rather between trueand false. You can use v == 0 to determine the value of the variable in a condition, but if you want to use the value 0 or 1 you would need to use something like v == 0 ? 0 : 1 or Number(v) to get that. (Also, you can use v = !v; to toggle between true and false.)
yeah got it.. thank you :)