What does || mean? [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does this construct mean?
I'm encountering this syntax for the first time and am not sure what it's doing:
self.name = _searchString(settings.dataBrowser) || "An unknown browser";
What does the or (double pipes) condition do? When would self.name be set to the second value?

This is the logical or operator.
It evaluates to its first "truthy" operand.
In particular, it will evaluate to the second operand if the first operand is "falsy" — null, false, undefined, 0, "", or NaN.

Crockford calls / called it a default operator

this is directly related to a question i have asked, you can read about it here
Short-circuit evaluation via the AND operator in PHP
so basically, it sets self.name to the value returned from the function, but if the function returns false, it sets itself to "An unknown browser";

Related

returning the higher value in logical operators in javascript [duplicate]

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

Javascript - succinct way to assign another value to variable if first value is not truthy [duplicate]

This question already has answers here:
Is there a "null coalescing" operator in JavaScript?
(19 answers)
Closed 4 years ago.
I have a function that may return a value or may return null and I want to assign it to a variable. So at the moment I've got
someVar = (someFun())
? someFun()
: "foo";
Is there a shorterer way of doing it, where I don't have to call the function twice, or add an extra variable like:
funResult = someFun();
someVar = (funResult)
? funResult
: "foo";
Basically, something like:
someVar = someFun() | "foo"
The idiomatic way is
someVar = someFun() || "foo";
The || operator will yield the value of the left-hand expression if it's truthy. If not, it will move on to evaluate the right-hand side, and return its value.
For those unfamiliar with the term "truthy", in JavaScript it means values that are not considered by the language to be implicitly false. The implicitly false values are 0, NaN, false (of course), "", null, and undefined. Any reference to an object, even if it's completely empty, is considered to be "truthy".
In your specific example, the || approach is to be preferred because it means you only call someFun() once.

Why is this javascript expression evaluated to false? [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 6 years ago.
I have written a simple function that nullifies itself after its first call:
var nullify = function(){
nullify = null;
return 1;
};
If I call it two times, like this:
console.log(nullify());
console.log(nullify);
Then the 1st expression will return 1, and the other will evaluate to null. All clear up to this point.
If I however do it in a single expression and wrap it in a function:
var fun = function(f){
return f() && !f;
}
Then, for some reason:
console.log(fun(nullify));
evaluates to false, while I would expect it to be true, since 1st operand will return 1 and the other, as a negation of null, true.
The evaluation of the right-hand operand occurs when the nullify functiion has already called nullify = null, is that right? What am I missing?
What am I missing?
That your fun function is testing the variable f, which still is bound to the function you passed in, not the variable nullify (which has the value null indeed).

What does "var app = app || {};" do? [duplicate]

This question already has answers here:
What does var x = x || {} ; [duplicate]
(6 answers)
Closed 9 years ago.
Im looking at some Backbone.js examples and they have var app = app || {}; at the top of all .js files. I understand the literal meaning of this, but what does it do in reference to everything else?
Edit: you guys are really really fast.
It will define the variable app to an empty object if it is not already defined.
This works because being undefined evaluates to false in Javascript.
If it is defined, it may still be re-defined as an empty object if it has a value which evalutes to false, such as an empty string.
The || operator in javascript will return the first operand if it is "truthy". If not, it will return the second operand. If app has not been assigned, it will be undefined, which is "falsey". Thus if it is not defined or is otherwise falsey, an empty object {} will be assigned to app.
This means "define app as an empty object if it's not already defined".
The OR operator in JavaScript does not necessarily yield a boolean. If the left-hand side of the expression yields false then the assignment takes the right-hand side of the expression.
If app is already defined, the it does nothing.
If app is not defined, then it's equivalent to var app = {};

Javascript Boolean type [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does !new Boolean(false) equals false in JavaScript?
var b = new Boolean(null);
alert(b instanceof Boolean);
if(b) {
alert('cv');
alert(b.toString());
}
Why if code block is executed? Is b supposed to be a boolean type and evaluated as false?
Please explain thanks
The code block executes, because the object exists and is not undefined although it has no value currently. The point of the Boolean object in javascript is to convert non-boolean objects to the "true" or "false" value.
if you have
if( b.valueOf() );
that will evaluate the actual value of the object.
All objects are truthy, except null. Therefore, even if you write new Boolean(false) specifically, it will still be truthy.
This is why you never write new Boolean. To cast to a boolean, just use !!

Categories

Resources