javascript if statement not evaluated as expected [duplicate] - javascript

This question already has answers here:
What's the difference between & and && in JavaScript?
(4 answers)
Closed 5 years ago.
Why my "C" condition go to 'else' statement?, they separated are going to 'if' statement but together are not working.
var objTest = {
ID : "10"
};
//A: First Condition: Exist value in property ID
console.log((objTest.ID ? 'if' : 'else')); // output => "if"
//B: Second Condition: Value different from "0"
console.log((objTest.ID != "0" ? 'if' : 'else')); // output => "if"
//C: First and Second Condition together must be "if"
console.log((objTest.ID & objTest.ID != "0" ? 'if' : 'else')); // output => "else"

Your problem is that you are using the wrong ANDopernator, you need to use && instead of &:
console.log((objTest.ID && objTest.ID != "0" ? 'if' : 'else'));
The first &is a bitwise operator and &&is the logical operator.
Please take a look at What's the difference between & and && in JavaScript? for further details.

Related

What does the "?" operator do in Javascript? [duplicate]

This question already has answers here:
Question mark and colon in JavaScript
(8 answers)
Closed 3 years ago.
I am wondering what this question mark symbol means in a function return statement in JS.
function getValue(val) {
return (val != null ? val.toString().replace(/,/g, '') : "");
}
Its a Conditional (Ternary) Operator:
Syntax:
variablename = (condition) ? value1:value2
Example:
var voteable = (age < 18) ? "Too young":"Old enough";
Explanation:
If the variable age is a value below 18, the value of the variable voteable will be "Too young", otherwise the value of voteable will be "Old enough".
In this case "?" allow to write if ... else in one line, it's what we called ternary operator, refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
It's a way to choose a value conditionally based on another value.
Variables are 'truthy' in javascript, and so let's say you have a variable x, and you want to choose variable y based on if variable x is truthy or not
var y = x ? '1' : '2';
If x is truthy, y will be '1', otherwise '2'.

What does the ? and : mean in these one line return statements? [duplicate]

This question already has answers here:
How do you use the ? : (conditional) operator in JavaScript?
(20 answers)
Closed 6 years ago.
Trying to figure out what the ? and : mean in this one line return statement.
return input && input.length ? [input.filter(p => p > 0).length, input.filter(n => n < 0).reduce((a, b) => a + b, 0)] : [];
This is a ternary if statement, I'd use parenthesis for organization sake, but it works like this:
[condition] ? [returns if condition is true] : [returns if condition is false]
Return a ? a : b;
Means if a return a else return b!
It means that if the condition before ? is true then use the first part before : else use the second part.
In your case it means that if input is not null and has value, then return the filtered one otherwise return an empty array.

Returning && expression from JS function [duplicate]

This question already has answers here:
Javascript AND operator within assignment
(7 answers)
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
Closed 6 years ago.
Here is the function I am working with. I actually found it in React Native documentation :
var testFunction = function(word) {
return word && '🍕';
}
Here is how I am using this function :
var testWord = testFunction("Alex");
The final value of testWord, returned by testFunction, is "🍕".
I would have expected the returned value to be either true or false, as the result of the && expression. However the value is a string of value "🍕".
Could someone explain the logic behind this ?
The && evaluates as the right hand side if the LHS is true, otherwise it evaluates as the LHS. It doesn't evaluate as a boolean.
0 && 1 // 0 because 0 is not true
1 && 0 // 0 because 1 is true
1 && 2 // 2 because 1 is true

Boolean Value of Zero [duplicate]

This question already has answers here:
Why does ('0' ? 'a' : 'b') behave different than ('0' == true ? 'a' : 'b') [duplicate]
(6 answers)
Closed 8 years ago.
I have the following JavaScript/jQuery code:
if (isVariance && value)
tableCell.addClass('NonZeroVariance');
Where:
isVariance == true and value == "0.00".
(isVariance && value) == "0.00".
(isVariance && !!value) == true.
The if condition evaluates to true, and the class is added to tableCell.
So, my expectation was that zero would be interpreted as false, and that "0.00" would be evaluated as false. But that's not what happens here. Can someone tell me why?
Yes, it appears that value is a string. And while the number 0.00 will evaluate to false, the string "0.00" evaluates to true.
The solution is to first convert the string to a number, and then perform the same test.
value = Number(value);
if (isVariance && value)
tableCell.addClass('NonZeroVariance');
EDIT:
The reason that the string "0.00" evaluates to true, is because: The result is false if the argument is the empty String (its length is zero); otherwise the result is true. - http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/

Use of `?` in a JavaScript timer function return [duplicate]

This question already has answers here:
Question mark and colon in JavaScript
(8 answers)
Closed 8 years ago.
So I stumbled on some Javascript timer example and there was this function, which gets a number into it and then the function returns the time:
function timer(variable){
return variable > 9 ? variable : "0" + variable;
}
And the whole code would look something like this:
var sec = 0;
function timer(variable){
return variable > 9 ? variable : "0" + variable;
}
setInterval(function(){
$("#time-id").val("Minutes: "+timer(parseInt(sec/60, 10))+" Seconds: "+timer(++sec%60));
}, 1000);
So yes, the setInterval function is pretty clear to me but about the timer().
What is the usage of ? in it? And how does it understand if I have multiple params given to it. Should it not be marked like this : timer(variable1, variable2){} ?
Timer is formatting time so that if it is 6:05, it shows up as 6:05 and not 6:5
return variable > 9 ? variable : "0" + variable;
is the same as
if ( variable > 9 ) {
return variable
} else {
return "0" + variable
}
?: is an operator shared by a lot of modern languages. It's a shortcut for an if test. The structure is as follows :
condition ? resultIfTrue : resultIfFalse
In your case, it appends a 0 to your variable if the variable is < 10. You could read it as :
if (variable > 9)
do nothing
else
add a "0" before
It's saying that if variable is 10 or more, it just echoes back variable; otherwise it adds a 0 on the start to still make it two digits.
variable > 9 ? variable : "0" + variable; is a ternary expression. variable > 9 is evaluated, and if it's truthy, variable is returned, otherwise "0" + variable is returned. Think of it as a one-liner substitute for an if-else statement with assignment.

Categories

Resources