how does javascript or condition work [duplicate] - javascript

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 .

Related

what is the logic behind this logical expression [duplicate]

This question already has answers here:
Does JavaScript have "Short-circuit" evaluation?
(3 answers)
Closed 8 months ago.
function expect(argument) {
this.eat(argument) || this.undefined();
}
Can someone please help me explain how this code works... the question is focused on this.eat(argument) || this.undefined();. I am guessing that one of those methods would run? and if it is, how do I tell which would run
Or Condition || will return the first true value.
if this.eat(argument) return true or any value which can be converted to true value, So the this.undefined() function will not execute.
If not it will execute.

Javascript with blockchain development, address.trans returns [] when theres no transactions pend [duplicate]

This question already has answers here:
How to compare arrays in JavaScript?
(61 answers)
How to check if an array is empty or exists?
(23 answers)
Closed 12 months ago.
address.trans returns [] when theres no transactions pend
if(txs === []){
returns a transaction still
Any solutions? Basically registering a "depo" when I never depo'd to the generated wallet.enter image description here
This should be happening because an array is an object. And two objects even with the same value are not equal in JS because they have different reference.
You can see that below:
let trans = [];
console.log(trans === []);
console.log([] === []);
console.log(trans.length === 0);
Array.prototype.length can be one way to check it:

Why is this simple JSON.parse JavaScript code failing? [duplicate]

This question already has answers here:
Why [] == [] is false in JavaScript?
(6 answers)
Weird behavior of comparison operator JavaScript when using empty array [duplicate]
(2 answers)
Closed 3 years ago.
I am trying to understand why this simple code is saying false instead of true.
let test1 = JSON.parse("[]");
let test2 = JSON.parse("[]");
console.log(test1 == test2);
Surely this code should be true but I always get it saying false. Whats the issue?
Thanks

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 === and == giving false for following? [duplicate]

This question already has answers here:
Why doesn't equality check work with arrays [duplicate]
(6 answers)
Closed 4 years ago.
I know it is very much stupid to ask but can anyone tell me
Why === and == giving false for following.
x=[[1,2]];
console.log(x[0]===[1,2]);
console.log(x[0]==[1,2]);
Here typeof(x[0]) and typeof([1,2]) is also same, then why it is giving false?
Because they are different values in memory.
x=[[1,2]];
console.log(x[0]===[1,2]); // Here you're creating a new array in memory
console.log(x[0]==[1,2]); // Here you're creating a new array in memory
var y = x[0]; //Same value in memory
console.log(x[0]===y);
console.log(x[0]==y);
Equality comparisons and sameness

Categories