Returning && expression from JS function [duplicate] - javascript

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

Related

what is the difference between (!a==b) and (a != b) in JS? [duplicate]

This question already has answers here:
The negation operator in JS
(1 answer)
Should I use !== or ==! when checking for not equal?
(2 answers)
What is an exclamation point in JavaScript?
(2 answers)
What is the difference between != and !== operators in JavaScript?
(3 answers)
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 11 months ago.
if(openmenu != para){
addClass(openmenu , 'collapsed');
}
I am a beginner of JS, I want to add a classname to a element after a if statement. However, if(openmenu != para) and if(!openmenu == para) showed completely different result. Does anyone know why?
!openmenu == para is parsed as (!openmenu) == para, so it first negates openmenu and then checks whether it is equal to para, which is most likely not what you want to do. You could write !(openmenu == para) which would mean the same thing as openmenu != para.
!a == b inverts a's value. For example if it's true, using ! will invert it to false.
if(!true == false) equals to if(false == false) which results as true.
but if(a != b) compares values, lets say a is true and b is false, so if(true == false) => false values are not the same. :)
In this case, if a==null and b==false
so !a = true.
Then !a == b is false but a!=b is true.
I hope that answer is helpful to you.
You are comparing different values. When you add ! in front it toggles the value between true and false
const openmenu = 1;
const para = 2;
console.log(!openmenu) // toggles to false
console.log(!para) // toggles to false
console.log(!openmenu == para) // false == 2
console.log(openmenu != para) // 1 != 2
console.log(!"Turtle") // converts to false

What does this mean? Is it an if else representation? [duplicate]

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.

Unable to see the correct value by using && operator [duplicate]

This question already has answers here:
Javascript AND operator within assignment
(7 answers)
Closed 7 years ago.
hi i am preparing for js interview.
i saw this question.
if var a=2, var b =3 What would be the value of a&&b?
The answer given is 3.
I am not able to understand why is this the answer.
Can you help..
Thanks.
&& and || do not create true or false. They return one of the operands.
The binary operators do NOT behave like other c based languages. See more about truthy and falsey values here
&& is defined as follows.
If the first operand is truthy then the result is the second operand. Otherwise it is the first.
Examples
false && true // false the first operand is falsey
0 && true // 0. 0 is a falsy value so the first value is used
1 && 0 // 0. The first value is truthy
// so the second value is used
'hello' && 'world' // 'world' the first value is truthy
// so yield the second value.
|| is defined as follows.
If the first operand is truthy then the result is the first operand. Otherwise it is the second.
Examples
false || true // true, first value is falsey so yield second
0 || true // true, first value is falsey so yield second
1 || 0 // 1, first value is truthy use it.
'hello' || 'world' // 'hello', first value is truthy so yield it.
&& and || are "Short-circuit"
This means that there are ways to structure code such that not all expressions are evaluated. This can be convienient at times but is double edged.
function launchNukes() { /* TODO */ }
0 && launchNukes(); // nukes do not fire
1 && launchNukes(); // nukes fire
0 || launchNukes(); // nukes fire
1 || launchNukes(); // nukes do not fire
&& is an AND operator, just like most everywhere else. Most languages, JavaScript included, will stop evaluating an AND operator if the first operand is false.
You can read it like that:
if a is true , return value will be b
if a is false , return value will be a
So && return values of operands not false and true.
for your example,
2 && 3 // return 3 because 2 is true

Why does this not return a boolean [duplicate]

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

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/

Categories

Resources