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
- 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. - 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
Returnsexpr1
if it can be converted tofalse
; otherwise, returnsexpr2
. Thus, when used with Boolean values,&&
returnstrue
if both operands aretrue
; otherwise, returnsfalse
.
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
Can't Perform a React State Update on an Unmounted Component
Backslashes - Regular Expression - JavaScript
I'Ve Heard Global Variables Are Bad, What Alternative Solution Should I Use
Regex to Extract All Matches from String Using Regexp.Exec
Polymer Share Styles Across Elements
Add a Transform Value to the Current Transforms That Are Already on the Element
Making Facebook Login Work with an Android Webview
Why Does ++[[]][+[]]+[+[]] Return the String "10"
How Does This JavaScript/Jquery Syntax Work: (Function( Window, Undefined ) { })(Window)
Graph Visualization Library in JavaScript
Response to Preflight Request Doesn't Pass Access Control Check
Event.Preventdefault() Function Not Working in Ie
JavaScript - Generating Combinations from N Arrays With M Elements