What does this shorthand mean in JavaScript? [duplicate] - javascript

This question already has an answer here:
What does this symbol mean in JavaScript?
(1 answer)
Closed 6 years ago.
In this code, what does 'q--' mean in the while loop?
getTotal: function () {
var q = this.getItemCount(),
p = 0;
while (q--) {
p += basket[q].price;
}
return p;
}
Is this JS shorthand? Is there an online tool that converts shorthand JavaScript into longhand? Also, why are vars q and p declared this way instead of defining them this way:
var q = this.getItemCount();
var p = 0;

It's the decrement operator. The value of q is decreased by 1 each time q-- is evaluated but, importantly, the value is returned before the decrement.
So, the loop above will continue until q=1 but the value used inside the loop during this final iteration will be q=0.
In layman's terms: q-- means "Give me the value of q then decrease it by 1 directly afterwards".

as you can tell q is a variable with the number of items and the -- is Decrement Operator it just subtracts one form q until reaches 0.
This is works because in javaScript 0 == false and it will get out of the loop when q reaches 0.

Here, q-- means "subtract 1 from q and keep its old value q". The variables are declared in the shorthand method to reserve the size of the page and therefore its loading time.

Related

javascript function output problrm [duplicate]

This question already has answers here:
Difference between pre-increment and post-increment in a loop?
(22 answers)
Closed 1 year ago.
The output of console.log(in1()) should be 11 but it's 10 why?
function outer() {
let i = 10;
return function inner() {
return i++;
}
}
var in1 = outer();
console.log(in1());
Quoting https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment here:
If used postfix, with operator after operand (for example, x++), the increment operator increments and returns the value before incrementing.
Therefore, the postfix (that is, x++; postfix because it appears after) operator only gives the existing value before doing anything. That's why, when called, gives what exists, not what's the result of the addition.
Because of this, use the prefix operator instead, ++x, to get the value while still incrementing the variable.
The same applies to the decrement operator.
It's because using i++ returns i first, then adds 1 to i. If you call in1() the second time, you will see 11, 12 and so on.
Try using ++i instead.
When using the postfix syntax i++ the value is returned before incrementing. Use the prefix syntax instead ++i.

Why the value plus plus at last is 100? [duplicate]

This question already has answers here:
javascript i++ vs ++i [duplicate]
(8 answers)
++someVariable vs. someVariable++ in JavaScript
(7 answers)
Closed 1 year ago.
when i use ++ operator in javascript, why I get the value at last is 100 not 101?
I want to know the detail of the ++ operator in javascript?
let value = 100;
value = value++;
console.log(value); // 100 why the value is 100 at last
Because when you have the assignment operator, the right-hand operand is evaluated first, then that value is assigned to the receiver on the left.¹
So here's what happens in value = value++:
Read the value of value (100) and set it aside (since we'll need it in a minute).
Increase the value of value by one (making it 101).
Take the value from #1 (100) as the result value of the right-hand operand.
Store that value in value.
¹ In specification terms it's slightly more complicated than that, but the details aren't important here.
That's because <value>++ returns the previous value and not the new one
If you want it to return the new value use ++<value>

Why incrementor is printing 3 even condition is checking for less then 3? [duplicate]

This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
How do JavaScript closures work?
(86 answers)
Closed 4 years ago.
for below code output will print "3" for 3 times, i.e updates same variable for 3 times
for(var k=0;k<3;k++){
setTimeout(()=>{
console.log(k)
},1000)
}
similarly if i use let instead of var 1,2,3
for(var k=0;k<3;k++){
setTimeout(()=>{
console.log(k)
},1000)
}
i know why it prints 0,1,2 as i is different every time. i'm just wondering why its printing 3 in case of var as condition is to checking for less then 3?
The above result is because, var defines variable globally, or locally to an entire function regardless of block scope.
let creates a variable declaration for each loop which is block level declaration. So basically it creates a scope within { }.
If you want to print using var keyword use self invoked functions :
for(var k=0;k<3;k++){
(function(k){
setTimeout(()=>{
console.log(k)
},1000)
})(k);
}
the function setTimeout will execute after the delay provided. The code execution doesn't stop since the javascript is based on concurrency model and event loop, so the execution continues till the delay provided. which in case, increment the k. Also, the post-increment operator will use the value before iteration. So, for last iteration the value of k is 3, but since we have given a delay, it would print 3 every time.

Explain why for loop behaves like this in JavaScript? [duplicate]

This question already has an answer here:
Javascript: When writing a for loop, why does it print the last index number?
(1 answer)
Closed 7 years ago.
Though I'm aware of closures and scopes. I need some detailed explanation on the following piece of code.
for (var i = 0; i < 10; i++) {
i // no console.log
}
Output
9
Why would it not display from 0 to 9 ?
Ran on chrome developer console.
Your question is a bit vague, but it seems you are asking why the output would end at 9, instead of 10 (even though, in your example, you provide no output method).
Your loop uses the < operator to compare i and the 10 constant, which translates to "less than 10". Since you are incrementing by whole numbers, 9 will be the last integer before your loop falls outside of the < 10 range, thereby breaking out of the loop.
If you are expecting 10 as your final output, you can use the "less than or equal to" operator (<=) to compare i and 10.
Edit
You've edited your original question, and the answer to it is best summed up in another Stack Overflow answer:
All statements in javascript have a value, including the block
executed in looping constructs. Once the loop block is executed, the
final value is returned (or undefined if no operations take place).
The statement that is implicitly providing the return value "100" is
numbers[i] = i+1;, as the final iteration of i+1 produces 100 and
assignment operations return the value being assigned.
If you're running that in the console, it's because the console shows the final result of the last statement in the program it runs.
In your case, the for loop gives a final value of 9.
While it may seem odd that I'm talking about a for statement giving a result (since statements don't give results), they actually do give a result with respect to the execution of the entire program. This result is obtainable by whatever is executing your code.
Here's an example. You can do the same with eval(), which will give you the result of the final statement.
var n = eval('for(var i = 0; i < 3; i++) {i}');
console.log(n);
This will give you 2 in the console as the result of the program eval'd even though the only result could come from the for loop, which returns the last statement result of its last loop.

JavaScript increments [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
++someVariable Vs. someVariable++ in Javascript
I know you can add one to a variable simply by doing i++ (assuming i is your variable). This can best be seen when iterating through an array or using it in a "for" statement. After finding some code to use online, I noticed that the for statement used ++i (as apposed to i++).
I was wondering if there was any significant difference or if the two are even handled any differently.
Yes there is a big difference.
var i = 0;
var c = i++; //c = 0, i = 1
c = ++i; //c = 2, i = 2
//to make things more confusing:
c = ++c + c++; //c = 6
//but:
c = c++ + c++; //c = 13
And here is a fiddle to put it all together: http://jsfiddle.net/maniator/ZcKSF/
The value of ++i is i + 1 and the value of i++ is just i. After either has evaluated, i is i + 1. It's a difference in timing, which is why they're often called 'pre-increment' and 'post-increment'. In a for loop, it rarely matters, though.
People like Douglas Crockford advise not to use that way of incrementing, amongst other reasons because of what Rafe Kettler described. No matter how experienced you are, sometimes ++i/i++ will suprise you. The alternative is to simply add 1 to i using i += 1, readable, understandable and unambiguous.
have a look at this link : http://www.w3schools.com/js/js_operators.asp
it's post increment versus pre increment. They both end up incrementing the value but one returns the value BEFORE incrementing (++y) and the other one returns the value AFTER (y++).
However, it doesn't make any difference when using it in a for loop --
for( var i = 0; i < 100; i++ ) { ... }
is the same as
for( var i = 0; i < 100; ++i ) { ... }
a=1;
b=1;
c=++a;//the value of a is incremented first and then assigned to c
d=b++;//the value of b is assigned to d first then incremented
now if you print a,b,c,d..the output will be:
2 2 2 1
++i is called pre-increment and i++ is called post-increment. The difference is when the variable is incremented. Pre-incrementing a variable usually adds 1 and then uses that value, while post-incrementation uses the variable and then increments.

Categories

Resources