JavaScript: variable = variable || variable [duplicate] - javascript

This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
Closed 8 years ago.
I have absolutely no idea how this is called which is the reason why my searches failed miserably.
What does this code mean
var a = b || c;
I believe it's something like a will equal which ever is defined but.. I just have to be sure what it exactly does before I use it.

If b is defined, it will use b, if c is defined and b isn't, it will use c. If you put it in parenthesis () it will return true/false.

Related

Is this JS bug? [duplicate]

This question already has answers here:
What causes the different behaviors between "var" and "let" when assign them a returned value of a function which throws an error
(1 answer)
Is there any difference between declared and defined variable
(1 answer)
Closed 3 years ago.
I tried to run this code.
let a = b; //I'm sure that variable b is not defined.
Uncaught ReferenceError: b is not defined
I know this code causes this error.
b = 3;
Uncaught ReferenceError: b is not defined
let b = 3
Identifier 'b' has already been declared
Now, I can't put any String , Number,Null and Everything.
And I can't define variable 'b'.
Is this Js Bug?

What is the purpose of || in a JavaScript variable? [duplicate]

This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
How does javascript logical assignment work?
(6 answers)
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 4 years ago.
I just saw a line of code like this one below and I was intrigued by the use of ||
const myCars = ['BMW','Audi','VW']
const foo = myCars.length || [];
Does this mean if myCars.length was to ever be undefined that foo would equal an empty array instead of undefined?
Yes, how it should be read is:
if 'myCars' doesn't have a length (e.g. no values), the constant foo should be set to [].
Note: https://www.w3schools.com/jsref/jsref_length_array.asp, specifically the return value of the .length: "A Number, representing the number of elements in the array object".
this is Short-circuit evaluation in Javascript. its Unique in JS to USE || operator because other languages use this operator in conditional statements only. please read this
https://en.wikipedia.org/wiki/Short-circuit_evaluation

Why can't I do 1.toString() but I can do var a = 1; a.toString() [duplicate]

This question already has answers here:
Usage of toString in JavaScript [duplicate]
(3 answers)
Closed 6 years ago.
When I do
1.toSting()
I get an error, but
// javascript
var a = 1;
// or c#
int a = 1
a.toString()
works. Why is it that when a number gets assigned to a variable, it get some special functions?
The . is interpreted as you want a decimal/floating-point literal, not invoking a member.
You can do this in JavaScript
// Option 1
(1).toString();
// Option 2
1.0.toString();
// Option 3
1..toString();
In C#, it appears your only option is (1).ToString(), but the lexer might be smart enough not to need them.

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".

how does javascript or condition work [duplicate]

This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
Closed 9 years ago.
i have written my code something like this
this._users = users || [];
just wanted to know what does this mean to this._users?
Thanks for the help.
This basically is evaluating users to true or false. If it evaluation is true, than return users otherwise, assigns an empty array to this._users .

Categories