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

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

Related

What does ‘...’ in JavaScript do? [duplicate]

This question already has answers here:
What "..." means in Javascript (ES6)? [duplicate]
(1 answer)
Spread Syntax vs Rest Parameter in ES2015 / ES6
(11 answers)
Closed 4 years ago.
I’m new to coding and slef teaching JavaScript.
My task I was set was to identify the largest value in an array.
My soloition works, but I needed the ‘...’ for it to work properly.
My question is, what does the ‘...’ in the last line actually mean/do?
function createAnArr(){
let myArr = prompt("Enter your numbers separated by commas:").split(",");
return myArr;
}
console.log(Math.max(...createAnArr()));
'...' it means it will be able to take any number of arguments of the respective scope
'...': The spread operator is used for array construction and destructuring, and to fill function arguments from an array on invocation. A case when the operator spreads the array (or iterable object) elements.
more details you can see here

why 2.valueOf() is not valid but (2).valueOf() is? [duplicate]

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 4 years ago.
I guess that javascript will parse (2).valueOf() to new Number(2).valueOf() but why it doesn't for the first one ?
According to the operator precedence, the grouping operator shall have a higher priority than the member access https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
So why (2) is not be evaluated first and yield 2 instead of be parsed to new Number(2) ?
Because in 2.valueOf the . is considered to be as a part of 2 instead of being understood as method accessing.
That is why 2..valueOf() works.
console.log(2..valueOf());

Issue working with comparison operator [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Triple equal signs return false for arrays in javascript. why?
(4 answers)
Closed 4 years ago.
Console logging this.byPassViewState returns ["01"]
if i do this.byPassViewState === ['01'] it returns false
typeof(this.byPassViewState) retuns object
My question is why this.byPassViewState returns false ? it suppose to be true right ? please tell me what i'm doing wrong here
You cannot compare 2 array with just using == or === operators.
The easiest way to compare array is using JSON.stringify().
let byPassViewState = ["01"];
let compare = (JSON.stringify(byPassViewState) == JSON.stringify(["01"]) );
console.log(compare);
Please Reference : How to compare arrays in JavaScript?

what does 'in' mean in javascript if statement [duplicate]

This question already has answers here:
Javascript if in x [duplicate]
(3 answers)
Closed 6 years ago.
Hi i'm following a tutorial learning to use javascript.
function move(keyclick) {
if(40 in keyclick) {}
playerMove.x++;
render(); }
What does the 'in' word mean? I understand what the function is doing, but why not just use ==
?
Thanks
The in operator is true if the string on the LHS is the name of a property that exists on the object on the RHS.
== tests if a value matches another value, which is entirely different.
The in operator returns true if the specified property is in the specified object (cited from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in).

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