comma in javascript for loop conditional statement [duplicate] - javascript

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.

Related

Does `for (;;)` (two semicolons) in JavaScript mean infinity? [duplicate]

This question already has answers here:
Two semicolons inside a for-loop parentheses
(4 answers)
Empty for loop - for(;;)
(4 answers)
Closed 3 months ago.
It's in JavaScript not C language, so I think it's not duplicate
I was testing some code and I found this one :-
for (;;) {
console.log("test");
}
And the iterations kept going forever
I was wondering what does this ;; mean? And what is its use case?
PS :- don't run it as it will freeze for infinite iteration.
The reason that for(;;) loops forever is because for has three parts, each of which is optional. The first part initializes the loop; the second decides whether or not to continue the loop, and the third does something at the end of each iteration. It is full form, you would typically see something like this:
for(i = 0; i < 10; i++)
If the first (initialization) or last (end-of-iteration) parts are missing, nothing is done in their place. If the middle (test) part is missing, then it acts as though true were there in its place. So for(;;) is the same as for(;true;), which is the same as while(true).

We cannot use 1.toString(), but we can use `let a = 1; a.toString()`; Why? [duplicate]

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());

What is assignment with round brackets used for in JavaScript? [duplicate]

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.

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

How the sorting method of integer array works ? JS [duplicate]

This question already has answers here:
How does Javascript's sort() work?
(8 answers)
Closed 8 years ago.
var anArray = [ 5, 4, 8 , 1, 3] ;
anArray.sort(function (a,b){return a - b});
1) Can someone run me through how JavaScript will execute the sort method with function that is passed as a parameter ?
It will compare 5 with 4 then because it's positive, 4 will be before 5. Then it will compare 5 with every other number but 1 and 3 are also smaller than 5. So how java script know which position to put them before 5?
then it will compare 4 with every other number and 8 with every other number etc...
how java script do this? I want to do it with pen and paper.
2) why the function that is passed as an parameter is nameless ?
thank you.
Exactly how the comparator function will be called — that is, the sequence of values passed in — is not defined by the specification of the language. It completely depends on the particular JavaScript implementation and (probably) on the values in the array being sorted. Suffice to say that the sorting algorithm calls your function when it wants to compare two numbers, and that's that.The function is expected to return a value that's negative, zero, or positive, indicating that the ordering of the two numbers should be that the first one comes first, that either can come first, or that the second one should come first. A quick way to do that is to just subtract the second number from the first.
The function in your sample code is an anonymous function. It needs no name because it will be bound to a symbol in the receiving function as a result of the function call itself. You can give the function a name if you want to.

Categories