Difference Between or and || When Setting Variables

JavaScript OR (||) variable assignment explanation

See short-circuit evaluation for the explanation. It's a common way of implementing these operators; it is not unique to JavaScript.

how variables are set when using multiple OR (||) operators in Jquery/Javascript?

I think you must put some parenthesis:

someVar = $el.attr("one") || options.two || (options.extra == "true" ? undefined : "three");

in fact your own code is read as:

someVar = ($el.attr("one") || options.two || options.extra == "true") ? undefined : "three";

What is the difference between “=” and “=&” when assigning variables?

First of all: you'll hardly ever need references, avoid the confusion of using them if you can.

$a=5;    //assign value to a
$b=&$a; //make $b a reference to $a
$b=6; //assigning a value to $b assigns the same value to $a (as they point to the same location
echo $a; //6

$a=5; //assign a value to a
$b=$a; //set $b to the value of $a
$b=6; //set $b to another value leaves $a at it's original value
echo $a; //5

What does ||= (or-equals) mean in Ruby?

This question has been discussed so often on the Ruby mailing-lists and Ruby blogs that there are now even threads on the Ruby mailing-list whose only purpose is to collect links to all the other threads on the Ruby mailing-list that discuss this issue.

Here's one: The definitive list of ||= (OR Equal) threads and pages

If you really want to know what is going on, take a look at Section 11.4.2.3 "Abbreviated assignments" of the Ruby Language Draft Specification.

As a first approximation,

a ||= b

is equivalent to

a || a = b

and not equivalent to

a = a || b

However, that is only a first approximation, especially if a is undefined. The semantics also differ depending on whether it is a simple variable assignment, a method assignment or an indexing assignment:

a    ||= b
a.c ||= b
a[c] ||= b

are all treated differently.

PHP check | (OR) operator in variable assignment

Assuming you only have the states you have in your question, you can use a ternary to do this. It might help others understand what you're doing in the future

$c = ($a || $b) ? 1 : 0;

There's nothing wrong with the way you did it in your question, tho.

Javascript AND operator within assignment

Basically, the Logical AND operator (&&), will return the value of the second operand if the first is truthy, and it will return the value of the first operand if it is by itself falsy, for example:

true && "foo"; // "foo"
NaN && "anything"; // NaN
0 && "anything"; // 0

Note that falsy values are those that coerce to false when used in boolean context, they are null, undefined, 0, NaN, an empty string, and of course false, anything else coerces to true.

Difference between != and !==

= is an assignment operator, e.g. If you run var x = 1; then x will have the value of 1.

== (or !=) is a comparison operator that checks if the value of something is equal to the value of something else. e.g. if(x == 1) will evaluate to true and so will if(x == true) because 1 will evaluate to true and 0 evaluate to false.

=== (or !==) is another comparison operator that checks if the value of something is equal to the value of, and is the same type as something else. e.g. if(x === 1) will evaluate to true however, if(x === true) will evaluate to false because 1 (the value of x) is an integer and true is a boolean.

Difference between || and ?? operators

The main difference is that nullish coalescing(??) operator will only give the result as the right operand only if the left operand is either null or undefined.

Whereas the OR(||) operator will give the result as right operand for all the falsy values of the left operand.

Below are some examples

  • Snippet 1: With 0 as input

const a = 0;
// a || 10 --> Will result in 10, as || operator considers 0 as falsy value and resulting the right side operand
console.log(`a || 10 = ${a || 10}`);
// a ?? 10 --> Will result in 0, as ?? operator considers 0 as truthy value and resulting the left side operand
console.log(`a ?? 10 = ${a ?? 10}`);

checking a variable value using an OR operator

In JavaScript, the || operator returns its first operand if it evaluates to true (i.e. it is not false, null, undefined, "", or 0), and its second operand otherwise.

In the first case, ("incomplete" || "unknown") always evaluates to "incomplete", since it evaluates to true.

The entire condition then becomes:

if (status === "incomplete")

Which explains the behaviour you are observing.



Related Topics



Leave a reply



Submit