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.
Related
This question already has answers here:
Logical operators in JavaScript — how do you use them?
(2 answers)
Javascript AND operator within assignment
(7 answers)
Closed 1 year ago.
I am practising the logical operators in JavaScript and fully understood the concepts, but It seems that I didn't with this equation.
const one = 1;
const two = 5;
console.log(one && two);
Why in this case it returns five and not one, shouldn't be returned the first value since both are true ?
From MDN on the && operator:
"If expr1 can be converted to true, returns expr2; else, returns
expr1."
So in this case, 1 can be converted to true, so it returns the second value, 5.
The LOGICAL && operator returns the last value if all other values are true, or else it will return the first non truthy value.
i.e. Java != JavaScript
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 &&
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:
Null-safe property access (and conditional assignment) in ES6/2015
(11 answers)
JS checking deep object property existence [duplicate]
(2 answers)
Closed 6 years ago.
I dont know if a.b is set. I want to do something only if a.b[0].c is true. I can do something like this:
if (a.b && a.b[0] && a.b[0].c) {
// ...
}
Is there a shortcut to check nested existence? Can I simplify this kind of condition?
I used to code golf, and one of the tricks we used is the following.
(myArray || [])[0] || 0
First, myArray || [] is evaluated, if myArray exists, this returns it, if it doesn't, then it returns an empty array. Let's say myArray isn't defined. Next, the [][0] || 0 expression gets evaluated ans because [][0] is undefined, this returns 0.
In your case it could be used the following way:
((a.b || [])[0] || {}).c || {}
This returns an empty object if something's undefined.
I'm not saying you should use this (in fact, you shouldn't), I just want to show you, that there is a smaller solution.
Update:
If tc39 gets through, then you'll have a much better solution using optional chaining:
a.b?[0]?.c