This question already has answers here:
Logical Operators (AND Operators)
(3 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 4 years ago.
I need to know how to look for two values in if statement.
I know about a method where the code will be executed when at least one value matches the specified number like in here:
if (x == 0 || y == 0) {
// code to be called
}
But the code above will be executed even if one value matches 0 and the other not. What's the way to check if both x and y are equal to 0?
I couldn't find the answer anywhere...
Use the AND operator instead of the OR operator, that is
if (x == 0 && y == 0) {
// code to be called
}
You want to use AND, which is &&
Related
This question already has answers here:
if else statement reverse
(4 answers)
Closed 12 days ago.
Good morning
there are many tutorials that explain how to check if a string is empty but not the other way around
so i would like to do this
if variable not empty then we do this
if not empty then we do this
//do the opposite
if (nomvalidation === null || nomvalidation.trim() === ""){
}
Simply change the equality operators to inequality and the or to and and.
if (nomvalidation !== null && nomvalidation.trim() !== ""){
}
You may also want to look into the typeof operator to make sure its a string and undefined as another possible "empty" state.
This question already has answers here:
Check variable equality against a list of values
(16 answers)
Closed 3 years ago.
Is there a concise or better way to write this condition without having to use so many ||.
if (myVar != 'A' && myVar != 'B' && myVar != 'C'){
...
}
You can also express it like an array,
if (!['A', 'B', 'C'].includes(myVar)) {
// if myVar is none of the values in the array
}
edit: added negation to grab the opposite case as your example
This question already has answers here:
Does a javascript if statement with multiple conditions test all of them?
(10 answers)
Closed 5 years ago.
Does JavaScript operate the same as languages like C where if multiple conditionals are given, then the if statement when the entire thing can be guaranteed to be evaluated false/true?
I.E.
if( x == y || y == z)
or
if( x == y && y == z)
I know C doesn't check the second conditional in the first statement if the first conditional is evaluated true, as well as not checking the second conditional in the second statement is the first conditional is evaluated false.
I realize this is a quick Google, but I completely forgot what this compiler-level behavior is called and as a result was not able to find the answer.
We can run a simple javascript test to see this in action.
function check(){
console.log('called');
return true;
}
let x = true, y = true;
if(x != y || x == check()){
console.log('or verified');
}
if(x != y && x == check()){
console.log('and verified');
}
Our console doesn't display and verified for our second if statement since it never runs.
What you are looking for is short circuit evaluation. Yes, javascript short circuits so that the second expression won't be evaluated if it doesn't need to be.
Yes. It's called short-circuit evaluation!
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Short-circuit_evaluation
Yes Javascript has it and the name is "short circuit evaluation" just like C. In fact I often use this to make sure I don't read a property from a object that doesn't exist.
For example:
return object && object.property
You can read more about this here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
This question already has answers here:
Question mark and colon in JavaScript
(8 answers)
How do you use the ? : (conditional) operator in JavaScript?
(20 answers)
Closed 6 years ago.
x = (x === lightImages.length - 1) ? 0 : x + 1;
I understand the rest of my code so I won't add it because that would be pointless. Basically I'm just unsure what the '? 0' does in this code, my friend coded it for me because I was struggling with the rest of the code and I'd like to know exactly what is does so I could use it again properly.
It's a ternary operator.
Basically if (x === lightImages.length - 1) is true x = 0, otherwise x = x + 1.
You can think of it as a compact if () {} else {} statement. What precedes ? is the if condition, what comes immediately after is result. Anything after the : is the else result.
See here for more info: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
This question already has answers here:
Variable assignment inside an 'if' condition in JavaScript
(6 answers)
Closed 6 years ago.
I don't know if it's possible duplicate. Testing my code, sometimes I was wrong with because I put = and not == or === in if conditions:
Consider this code :
var a = 0;
if(a = 1) console.log('true');
I don't know why this is not an error and why returns true the condition (a = 1)
I guess that what it does is assign 1 to the a variable, but why this evaluates to true and there's no error?
you're setting a to 1 and then checking the truthiness of the result. Non-zero numbers in JavaScript are true, so you get what you see.
Like in math, things are evaluated left-to-right, with parens going first.
As it was said, it does assign to your variable and will return true for all values other than 0.
A way to avoid these kind of mistakes is to change the test.
if( 3 == a)
Here if you ever write (3 = a) you would have an error.