This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 3 years ago.
In one quiz I came across the following question:
What is the value of val?
var val = (5, 10, 15);
Despite the fact that I'm quite experienced with JavaScript I have never seen such kind of assignment before and failed to find any information about it on the internet.
What are the use cases and how it actually works?
Thanks in advance.
In JS, the comma operator (,) evaluates all operands, and returns the last.
So, in your case, it is equivalent to:
var val=15;
The 5 and 10 are evaluated and then silently dropped.
But consider the following:
var i=0;
var val=(i++,15);
console.log(i) //1
console.log(val) //15
The value of i is increased by 1, and it returns 15, combined to a single expression.
Related
This question already has answers here:
How does the single equal sign work in the if statement in javascript
(6 answers)
Closed 3 years ago.
This is my general code (in JavaScript):
let x = Math.floor(Math.random()×6)+1);
if (x=1){
do this
} else if (x=2){
do that
} else if.... and so on.
Whenever I test this code in a browser, only actions that could happen in the {do this} section happen. x is being locked as 1, even though it is meant to be a fair 6 sided die. Any ideas why this is happening, and how I can fix it?
The = operator in JavaScript is for assignment. x=1 will always return true because that is an assignment that will never fail. To test for equality use == (equality with conversion) or === (strict equality).
You have your if (x=1) assigning the value of 1 to x, rather than checking if it's equal to it. This results in it always returning TRUE. Using a pair of == (or ===) will solve this.
This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Calling member function of number literal
(3 answers)
Closed 2 years ago.
Is this means assignment in js of primitive makes primitive become object automatically?
let a = 1 just be transferred to let a = new Number(1), we know Number is a function, and it's prototype has toString, this makes sense? Is it right?
Finally, we know the primitive of js is stored in stack memory, but if we can only get object by assignment, so is it means only pointer exists in stack? I am confused. Thanks for your answers if you can help me.
You can not call Number.prototype methods directly from a number in digits form, e.g. 1 2 3 4 5 6 7 8 9, you have to wrap them in parentheses.
// This doesn't work
console.log(1.toString());
// This works
console.log((1).toString());
This question already has answers here:
How do I work around JavaScript's parseInt octal behavior?
(10 answers)
parseInt method in JavaScript
(6 answers)
Closed 3 years ago.
Time for some more education.
I've come across a javascript 'for loop' which loops through a nested object. What does the ,10 portion, represent in the condition statement?
for (var x = 0; x < parseInt(myObj[myCategory][MySubCategory]['amount'], 10); x++)
{
// stuff happens
}
I am not finding any documentation that talks about this so I presume I'm just unclear on what I would even search on. Thanks.
Notice, in your example, that 10 is the 2nd argument of the parseInt function.
The ,10 is for specifying a base of 10.
See radix.
You'd think base 10 would be the default, but it isn't; you need to specify this each time you call the parseInt function.
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());
This question already has answers here:
isNaN(1) and isNaN("1") returns false
(2 answers)
Closed 6 years ago.
I'm learning Javascript mechanics and I believe I just stumbled across a bug with isNaN().
Here is the test code.
var x = "1000";
Answer = isNaN(x);
console.log(Answer);
The console log returns "false" which indicates that Javascript looks at "1000" as a number. I thought that anything inside " " was considered a string. Evidently not always. If I'm wrong maybe somebody has some insight that can set me straight.
Apparently, it's a feature, not a bug.
When the argument to the isNaN function is not of type Number, the value is first coerced to a Number. The resulting value is then tested to determine whether it is NaN.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN