This question already has answers here:
Question mark and colon in JavaScript
(8 answers)
Closed 5 years ago.
What does the following mean?
var iCurrentIndex = oCurrentQuestionModel.getProperty("/index"); ...(1)
iCurrentIndex = iCurrentIndex ? ++iCurrentIndex : 0; ...(2)
The var iCurrentIndex = false in setp 1 and in step 2 it gets assigned value 0.
What does this representation mean can anyone explain me?
Your step 2 can be rewritten like this:
if (iCurrentIndex) {
++iCurrentIndex;
} else {
return 0;
}
You are using ternary operator, read about it here.
Assuming
var iCurrentIndex = oCurrentQuestionModel.getProperty("/index");
returns an undefined or null or just false, then an increment can not take place.
Then you need a check if the returned value is truthy (a value which resolves to true, if casted to boolean), then just increment or assign zero to iCurrentIndex.
iCurrentIndex = iCurrentIndex ? ++iCurrentIndex : 0;
The above conditional oparator uses a condition and evaluates either the part after ?, the then part of an if statement or the part after :, the else part of an if statement.
Related
This question already has answers here:
Javascript inline if statement
(4 answers)
Closed 11 months ago.
var x = 10;
var b = if(x>5){return true};
console.log(b);
I'm not getting why this is incorrect and will give an error.
The syntax of a variable initializer stipulates that it must be an expression. An if statement is not an expression.
You can write
var b = (x > 5) ? true : undefined;
(or whatever you want b to be when x is not greater than 5). That ? : expression is an expression, so it works as the initializer part of the declaration.
You can use this solution
var x = 10;
var b = x>5? true:false
# if the condition (x>5) is true.
# will execute the first return(true)
# or else the second return will execute (false)
console.log(b) #Output= true
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'.
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
This question already has answers here:
How can I convert a string to boolean in JavaScript?
(102 answers)
Closed 7 years ago.
I am getting values as True and False from back-end. I am trying to convert those values as real Boolean values, But I am getting always true with my approach. What would be the correct way to do this?
here is my try:
var x = Boolean("False".toLowerCase());
console.log( x ); //giving true instead of false.
You can use this :
var str = "False";
var x = str.toLowerCase() == "false" ? false : true;
try this code.
var x = Boolean("true" == "False".toLowerCase());
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.