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());
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:
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.
This question already has answers here:
JavaScript String concatenation behavior with null or undefined values
(7 answers)
Closed 6 years ago.
I have a global variable. The variable is equal to null. var a=null; Then I write a=a+"example" in console. But output is null example. Why is it so ?
There are three possibilities in javascript:
Option 1
var a;
a=a+"example";
output: "undefinedexample"
Option 2
var a=null;
a=a+"example";
output: "nullexample"
Option 3
var a="";
a=a+"example";
output: "example"
As per your Question you need to define third option. bcz in javascript null and "" both are different types.
For more ref JavaScript String concatenation behavior with null or undefined values
If You want to concatenate strings this way, You shouldn't assign null but an empty string. Value null will be changed to string 'null' in your code. Example:
var a = '';
for(var i=0; i<10; i++) {
a = a + 'abc';
}
As you are concatenating it with string it turning out to be string see this example it adopts the datatype you assign it to be for the initial value was null
if you concat with string it takes string type if number then with number type and so on
var s = null;
console.log(s+"example");
console.log(s+5);
console.log(s+17.5)
I don't know what u really expecting,according to your problem,i think you need to concatenate two string,if am correct you can use
var str1 = '';
var str2 = "example";
var res = str1.concat(str2);
instead of null you can use ''
This question already has answers here:
Assignment with double ampersand "&&" [duplicate]
(2 answers)
Closed 7 years ago.
I have inherited some javascript which has this line:
var vehicle = data.vehicles && data.vehicles.length > 0 && data.vehicles[0];
This returns the data.vehicles[0] object, not true or false. Why?
&& and || don't return booleans exclusively. && returns the last truthy value (or the first falsey) and || returns the first truthy value or the last falsey.
Try this:
var vehicle = !!(data.vehicles && data.vehicles.length > 0 && data.vehicles[0]);
Hope this explanation help you:
basic_truthy_falsy