Flip a boolean value in a simple way javascript [duplicate] - javascript

This question already has answers here:
How to toggle a boolean?
(9 answers)
Closed 1 year ago.
Given boolean value completed = false how do I flip it back and forth? I could of course do something like this:
if (completed) {
completed = false
} else {
completed = true
}
But that feels hacky and too long. Is there any slick and cleaner way of doing this? Thanks.

Assuming you declared variable with let or var, so that you can reassign the value :
completed = !completed

Related

what is the logic behind this logical expression [duplicate]

This question already has answers here:
Does JavaScript have "Short-circuit" evaluation?
(3 answers)
Closed 8 months ago.
function expect(argument) {
this.eat(argument) || this.undefined();
}
Can someone please help me explain how this code works... the question is focused on this.eat(argument) || this.undefined();. I am guessing that one of those methods would run? and if it is, how do I tell which would run
Or Condition || will return the first true value.
if this.eat(argument) return true or any value which can be converted to true value, So the this.undefined() function will not execute.
If not it will execute.

Why do this returns a non expected result? [duplicate]

This question already has answers here:
Why is [1,2] + [3,4] = "1,23,4" in JavaScript?
(14 answers)
Closed 4 years ago.
So I'm learning about the reduce method and I write this code...
function getSum(x,y){
return x+y
}
var arraySum = function(array) {
return array.reduce(getSum)
};
arraySum([1,[2,3],[[4]],5])
But it actually returns a string of the elements all-together...
I'm actually trying to sum them all... I expected the result to be 15 instead of "12,345"
What's happening? what am I doing wrong?
Your problem is you're adding actual arrays together, not the content within the arrays. Add a counter, and an incrementational variable like i inside the variable arraySum, and call the position of i, incrementing it every time, it should fix your problem.

Get react state with dynamic key [duplicate]

This question already has answers here:
Get state value by a dynamic key in react
(2 answers)
Closed 5 years ago.
I already know how to setState with a dynamic key name but how do I set a value as the state with a dynamic key?
Example
function thing(key) {
let stuff = this.state.key;
//Do stuff
}
Didn't even think of think of this as first but all I did was
function thing(key) {
let stuff = this.state[key];
//Do stuff
}
and it worked!

Toggle boolean using function? [duplicate]

This question already has answers here:
Does JavaScript pass by reference? [duplicate]
(13 answers)
Closed 6 years ago.
I would like to create a single function to toggle any boolean of my code, but I'm not able to get the expected result :
function toggle(x){
x=!x;
}
var test=false;
toggle(test);
alert(test);
Why doesn't this return true ?
Boolean datatype is passed by value. So, any changes made to the argument x will not reflect the actual variable test. You need to return the updated value from the function and assign it back to the variable.
function toggle(x) {
return !x; // Return negated value
}
var test = false;
test = toggle(test); // Assign the updated value back to the variable
alert(test);
But, as said in comments, it's better to use
test = !test;

what does the ! mean in Angular? i.e $scope.selected = !$scope.selected; [duplicate]

This question already has answers here:
What is an exclamation point in JavaScript?
(2 answers)
Closed 7 years ago.
I was wondering what does the ! actually mean in this method
$scope.toggleSelected = function () {
$scope.selected = !$scope.selected;
};
I understand it's allowing me to set a selected item and it won't work without it but what exactly is the ! for?
The ! is the normal negation operator.
Inside of that function, it's used to flip/toggle the value each time it's called. For example, from true to false and vice-versa.

Categories