What Does the Construct X = X || Y Mean

What does the construct x = x || y mean?

It means the title argument is optional. So if you call the method with no arguments it will use a default value of "Error".

It's shorthand for writing:

if (!title) {
title = "Error";
}

This kind of shorthand trick with boolean expressions is common in Perl too. With the expression:

a OR b

it evaluates to true if either a or b is true. So if a is true you don't need to check b at all. This is called short-circuit boolean evaluation so:

var title = title || "Error";

basically checks if title evaluates to false. If it does, it "returns" "Error", otherwise it returns title.

What does the comma mean in the construct (x = x || y,z)?

The Comma Operator in JavaScript evaluates operands and returns the value of the last one (right-most). By JS Operator Precedence, the OR operation will be evaluated first, followed by the assignment.

So this expression x = x || y, z is in effect (x = (x || y)), z. The OR operator will either return the boolean result of the comparison or, for non-boolean types, the first operand if it is truthy, or the second operand otherwise. The assignment operator is also higher precedence than the comma operator, so x will be assigned the value returned by the OR. The value of z will not have any effect on either the OR operation or the assignment. In fact, it will be evaluated last, meaning it is essentially a separate statement with no effect on anything else in that 'expression.' I can't see any practical value in writing the expression that way.

What is the purpose of x = x || 0 ?

|| in a variable assignment is a common way to specify a default value. This is because of JavaScript's falsy values. In JavaScript, undefined, null, empty string and 0 all evaluate to false in a boolean context.

For example:

var blah = undefined;
if (blah) {
console.log('got it!');
}
else {
console.log('not true!'); // this one outputs
}

Using || in an assignment is a way of saying "if defined, otherwise use this".

For this code,

function values(b) {
this.b = b || 0;
}

we can use a truth table:

b          this.b
------------------
5 5
10 10
0 0
undefined 0
null 0
'' 0

The values really of interest are undefined and null. So what we really want is:

if (b !== undefined && b !== null) {
this.b = b;
}
else {
this.b = 0;
}

But this.b = b || 0 is much shorter to write.

what does object || {} means in javascript?

parent.auth || {} means if parent.auth is undefined, null or false in boolean case then new empty object will be initialized and assigned.

or you can understand it like:

var auth;
if(parent.auth){
auth=parent.auth;
} else {
auth={};
}

What does this specific line of code mean/do in Javascript

It is basically saying

If counter[string[i]] is falsy (undefined, 0, empty string, null etc) use 0 to add to 1 otherwise use its existing value to add to 1 and make this addition the new value for counter[string[i]]

It is using the logical OR operator ||

See JavaScript OR (||) variable assignment explanation

Use of || {} in Javascript

What it means

If the variable secret is falsy (one of the following):

  • false
  • 0
  • '' (empty string)
  • null
  • undefined
  • NaN

..then set it to {} (an empty object - it's the same as new Object()).


Alternative code

It could also be written as this:

if (!secret) secret = {};

But as it's longer most people prefer the above.


Why?

This solution is useful as javascript has no default function parameters.

For example an example in PHP could look like this:

<?php
function foo($bar = 'default') {
echo $bar;
}
?>

and in JS that could be

function foo(bar) {
bar = bar || 'default';
console.log(bar);
}
foo(); //default
foo(NaN); //default
foo(undefined); //default
foo(null); //default
foo(false); //default
foo(0); //default
foo(''); //default
foo('something'); //something
foo(12); //12
foo(1.2); //1.2

What if I only want to set a default if nothing else has been set?

If you only want to check for no value (no falsy values), then you can use the typeof function in JS:

function foo(bar) {
if (typeof bar == 'undefined') bar = 'default';
console.log(bar);
}
foo(); //default
foo(undefined); //default
foo(NaN); //NaN
foo(null); //null
foo(false); //false
foo(0); //0
foo(''); //(empty string)
foo('something'); //something
foo(12); //12
foo(1.2); //1.2

Variable equals to A || B. How does this work?

It sets a default value for out, ie if out is not provided to the function, (or it is a falsy value such as false, 0, null, undefined or ''), then out is assigned {}.

How it works:
Javascript uses short circuit evaluation, which means the the or (||) operator will return the first non-falsy value. If out is not falsy, nothing changes, if out is falsy, it gets set to {}.



Related Topics



Leave a reply



Submit