JavaScript and Operator Within Assignment

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.

Variable assignment inside an 'if' condition in JavaScript

It has nothing to do with the if statement, but:

if(a=2 && (b=8))

Here the last one, (b=8), actually returns 8 as assigning always returns the assigned value, so it's the same as writing

a = 2 && 8;

And 2 && 8 returns 8, as 2 is truthy, so it's the same as writing a = 8.

Javascript: || operator for variable assignment

  1. Yes. If x has a truthy value, it will be assigned back to itself. If it doesn't, the default 'some value' will be assigned to it.
  2. There may be a tiny performance benefit in example 2, but example 1 is the standard idiom. Consistency with other programmers is useful because they'll understand your code more easily. Unless you're doing lots of default value initialization in a large loop, the performance gain should be negligible.

Javascript OR operator in variable assignment with jQuery selectors

As jQuery method returns a jQuery object which will always be truthy; thus you are getting undefined. if you are only intending to get HTML you can use.

var active = container.children('li.active').html() || container.children('li:first-child').html();

However you want to get the element, do away with || and use .length property to check if element exists.

var active = container.children('li.active');
if(active.length == 0)
active = container.children('li:first-child')

var container = $('.dummy');

var active = container.children('li.active').html() || container.children('li:first-child').html()

console.log(active);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul class="dummy">

<li>One</li>

<li>Two</li>

<li>Three</li>

</ul>

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.

Comparison Operator Vs. Assignment Operator In JavaScript

change this:

if (e.target.className = 'delete') {

to

if (e.target.classList.contains('delete')) {

Assignment with double ampersand &&

This is a common way to make sure your function exists before you call it.

It works like this (From developer.mozilla.com):

expr1 && expr2 Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.

In other words, Javascript does not coerce the operands to boolean values unless it has to.

4 && 5 Returns 5, not true.

In your case, if the first expression is undefined (which is convertible to false), then ctx will be false, and the second expression does not get evaluated. If the first expression is a function (which cannot be converted to false), then Javascript evaluates the second expression, and assigns it's value to the ctx variable.



Related Topics



Leave a reply



Submit