javascript function output problrm [duplicate] - javascript

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.

Related

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>

What does this shorthand mean in JavaScript? [duplicate]

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.

Difference between return ("abcd") and return "abcd"? [duplicate]

This question already has answers here:
Why use parentheses when returning in JavaScript?
(9 answers)
Closed 6 years ago.
I'm struggling with some JavaScript code that has examples that return a string in parens. Is there any difference between this:
var x = function() {
return "abcd";
}
And
var x = function() {
return ("abcd");
}
They are the same.
The parenthesis, i.e. grouping operator, here will just work as higher precedence, so that'll be evaluated first, and then the value will be returned.
There's no difference, at least in the way it is written.
Consider,
var x = 1 + 2;
vs
var x = (1 + 2);
Some prefer writing brackets around return statement but it is optional and often unnecessary. But I would advise stick to your existing code style.
In reference to this question the parenthesis do not have a specific meaning as a string is being returned. There is no expression evaluation involved so both the return statements work in the same way. In case we have an expression that needs to be evaluated, the parenthesis will provide a liberty to first evaluate the expression and then return the desired value according to the precedence rules.

Array behavior confusion [duplicate]

This question already has answers here:
Why does [5,6,8,7][1,2] = 8 in JavaScript?
(3 answers)
Closed 7 years ago.
In a coding test I have recently encountered a question that asks me to find out what will be printed in console. The question is below. I did not understand how this below code will be understood and executed.
Need help
var arr = ["a" , "b" , "c" , "d"][1,2,3];
console.log(arr);
What does that arr definition even mean?
Arrays in JS are one dimensional.
So var arr = ["a" , "b" , "c" , "d"][1,2,3];
Here [1,2,3] represents the indexes.
And comma is the least preceded operator so [1,2,3] will return you 3 (i.e. The right most element always).
Therefore
The result will return you the value at index 3 that's d
Update:
Let's see some more points for comma operator:
From MDN:
The comma operator evaluates each of its operands (from left to right)
and returns the value of the last operand.
So, It means:
var a = (10, 18);
a; //18
Also note that it's essential to mention them inside brackets while assigning to a variable.
If you try something like:
var a = 1,2,3;
a; // will return 1
So it's important that you wrap them using ().
When will you use it , operator?
You can use the comma operator when you want to include multiple
expressions in a location that requires a single expression. The most
common usage of this operator is to supply multiple parameters in a
for loop.
for (var i = 0, j = 9; i <= 9; i++, j--)
Now that you've understood about comma operator, Let's see few more examples to test our understanding:
1. var a = (1,3)/(2,1);
a // what will be the o/p
2. var a = ["Stack", "OverFlow","is","Awesome"][1,3,2][2,0]
a // What will be the o/p
3. var a = (true,0,1), b, c;
if(b,c,a) {
console.log("Will it execute?");
}
Your example caught me offguard. Here's another example which illustrates what is going on. Essentially a list of comma separated values evaluates to the value of the last.
if (false, false, true) {
console.log("hello")
}
// --> "hello"

JavaScript parentheses (1,2,3,4,5) [duplicate]

This question already has answers here:
Javascript expression in parentheses
(2 answers)
Closed 8 years ago.
I just came across some notation in JavaScript like so:
var a = (1,2,3,4,5);
This will always return the last value, in the above case 5. I'm aware of using brackets to namespace my JavaScript code, but have never seen it used this way.
Is there any use for this notation, or is it just some JavaScript byproduct?
It's the comma operator. As the mdn states (link) it always returns the later value. In your example it doesn't make much sense, since it will always assign a = 5. But consider this:
for (var i = 0, j = 9; i <= 9; i++, j--) {
...
}
It's used to increment and decrement in a single statement: i++, j--
Edit:
The parentheses in your example are necessary because its a variable declaration. In other cases they can be left out.
Parens are used to groups operations together. This is helpful for both setting operation precedence (e.g. x = (2+3) * 5 vs x = 2 + 3 * 5) and for making your code a little easier to read.
I suspect this is more a question about the comma operator. This is for making multiple assignments or operations on the same line. Here is a nice article about it: http://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/

Categories

Resources