What does mean && between variables in line without if? [duplicate] - javascript

This question already has answers here:
Using &&'s short-circuiting as an if statement?
(6 answers)
Javascript AND operator within assignment
(7 answers)
Closed 8 years ago.
I have following line of code in javascript:
q && (c = q === "0" ? "" : q.trim());
WHat does it mean? I understand that c is equals either empty string or q.trim() result, but what does mean q && ()?

JavaScript optimizes boolean expressions. When q is false, the right hand side of the expression doesn't matter for the result, so it's not executed at all. So this is a short form of:
if( q ) {
c = q === "0" ? "" : q.trim()
}

It is a guard against undefined/null/false variables.
If q is not defined, false or set to null, the code short circuits and you don't get errors complaining about null.trim()
Another way to write that would be:
if(q) {
c = q === "0" ? "" : q.trim();
}

You can use this kind of clause to check wether q is defined.

Related

.includes() on an empty string logic [duplicate]

This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
Closed 2 years ago.
I'm trying to debug someone`s code. The "e" variable line is checking whether the user has "save-data" enabled or if he's on a slow (2g) connection.
e = navigator.connection && (navigator.connection.saveData || (navigator.connection.effectiveType || "").includes("2g"));
if (!e && d)...
The part I'm confused about is the empty brackets in the last OR statement. Is it just a redundant code or am I missing something?
What's the point for checking whether an empty string has "2g" in it?
(navigator.connection.effectiveType || "") will resolve to an empty string if effectiveType is null, undefined or some other falsy non-string value.
If you didn't do that you'd attempt to call null.includes() which would throw an exception.

Why does the OR operator behave like this in Javascript? [duplicate]

This question already has answers here:
How does javascript logical assignment work?
(6 answers)
Javascript || operator
(5 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 3 years ago.
I was always using the || as a nullish coalescing sort of operator. Either A or B.
But if I do something like,
1 const a = null
2 const b = '123'
3
4 console.log(a || b) // 123
5 console.log(a) || console.log(b) // null, 123
Why are BOTH console logs executed on line 5? Even if a === null, shouldn't it just execute the first console log, and not look at console.log(b)?
The left side is evaluated.
console.log(a) returns undefined (because that function always returns undefined)
Since the LHS evaluated as a falsy value: The right side is evaluated.
The value of a is irrelevant as that isn't what is on the LHS.

How can jquery || "OR" be used outside of an if statement? [duplicate]

This question already has answers here:
JavaScript OR (||) variable assignment explanation
(12 answers)
What does the construct x = x || y mean?
(12 answers)
In Javascript, what does it mean when there is a logical operator in a variable declaration? [duplicate]
Closed 8 years ago.
I've never seen the OR paramater || used outside of an if statement.
What does this line of code do?
var doc = ($iframe[0].contentWindow || $iframe[0].contentDocument).document
Is it saying making it equal to either one of those???
A || B
evaluates A first. If it is true, A is returned, and B never needs to be looked at.
If A is false, B is evaluated and returned.
For example, if you write
function (x)
{ x = x || 50
...
This would make x=50, if x is nil (or some kind of false value).
Otherwise, x would not be changed.
It is like having a default value, or a failsafe protection. If you know that the answer should never be false, then if A is false, you provide a backup value of B.
A way to get a DOM reference to the iframe's window object is to use:
contentWindow.document
Now, cause IE<8 has problems with it, a small polyfill is to use
var doc = ($iframe[0].contentWindow || $iframe[0].contentDocument).document;
// Browser you get this one ^^^ ? NO? Sh** you're IE7, go with^^
So earlyer versions of IE will skip the contentWindow cause not recognized, and thanks to the || (or) operator will follow up with the next contentDocument.
I don't have to repeat what's the OR operator cause other smart people already explained it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
This is just a short form of the ternary operator, which always returns a value depending to a statement. So, e. g.:
var fruit = "apple";
var test = fruit === "apple" ? fruit : "banana";
This sets the variable test to the value of fruit, when fruit is set to "apple". Otherwise, test will be initialized with "banana".

Javascript: "a === b or c"? [duplicate]

This question already has answers here:
Javascript shorthand if
(5 answers)
Closed 9 years ago.
I would like to check whether variable a is equal to b or c.
Of course I know the explicit way to do this;
a === b || a === c
but is there a shorthand way of doing this in Javascript? I mean, for example,
a === (b || c)
does not work.
I found similar questions here
Short hand to do something like: if($variable == 1 || $variable == "whatever" || $variable == '492') .
PHP: If a equals b or c or d
but they are talking about PHP.
I don't think there's a shorter or more concise way of doing this in regular JavaScript.
An alternative would be to do a ternary, but this is arguably far less readable, so I would stick with the expression you have.
Ternary:
a === b ? true : a === c
Advised, as in your question
a === b || a === c
The simplest way is as you described a===b||a===c all other method can only expand your code. So, Use a===b||a===c instead of using any other .
((a === (b||c))
its the shortest way

What do ? and : mean in javascript [duplicate]

This question already has answers here:
How do you use the ? : (conditional) operator in JavaScript?
(20 answers)
What does ? : mean in Javascript? [duplicate]
(9 answers)
Closed 10 years ago.
I am trying to understand the Javascript/jQuery in this http://www.queness.com/post/12078/create-jquery-pinterest-pin-it-plugin plugin. The lines 20 and 22 are confusing me, the code is:
pi_media = e.data('media') ? e.data('media') : e[0].src,
pi_desc = e.attr('title') ? e.attr('title') : e.attr('alt'),
Can anyone help me out with what these lines mean in Javascript
It's the JavaScript ternary operator.
x = condition ? a : b
is equivalent to
if(condition)
x = a;
else
x = b;
Note that an assignment is not necessary. As an expression, it simply evaluates and produces a or b depending on the truth value of condition.
It's called the Ternary Operator. It means:
Evaluate the expression to the left of the ?
If the expression evaluated to true, run the first piece of code (between the ? and the :)
If the expression evaluated to false, run the second piece of code (after the :)
This is a construct common to many C-style languages.
Those are part of the ternary operator.
Basically, if the condition before the ? is evaluated to true, the expression immediately following the ? is the one which is evaluated, and otherwise the expression following the : is evaluated.
For example take the code:
var result=condition?arg1:arg2;
First the condition is evaluated.
If the evaluation results to true, then arg1 is returned and assigned to result
If the evaluation results to false, then arg2 is returned and assigned to result

Categories