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

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>

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.

returning the higher value in logical operators in javascript [duplicate]

This question already has answers here:
Logical operators in JavaScript — how do you use them?
(2 answers)
Javascript AND operator within assignment
(7 answers)
Closed 1 year ago.
I am practising the logical operators in JavaScript and fully understood the concepts, but It seems that I didn't with this equation.
const one = 1;
const two = 5;
console.log(one && two);
Why in this case it returns five and not one, shouldn't be returned the first value since both are true ?
From MDN on the && operator:
"If expr1 can be converted to true, returns expr2; else, returns
expr1."
So in this case, 1 can be converted to true, so it returns the second value, 5.
The LOGICAL && operator returns the last value if all other values are true, or else it will return the first non truthy value.
i.e. Java != JavaScript

increase object value on javascript? [duplicate]

This question already has answers here:
What's the difference between ++i and i++ in JavaScript [duplicate]
(6 answers)
Closed 1 year ago.
I want to increase object value just like loop i++ , but in object didnt worked ,
how to increase this value ? any methods?
const object = {
price:30
}
console.log(object.price++);
using ++ like that is the post-increment operator meaning object.price++ returns object.price first then increments. You want the pre-increment operator ++object.price.
const object = {
price:30
}
console.log(++object.price);

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.

Javascript var reference. Could someone explains to me what's the difference between primitive and objs/array? [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Does JavaScript pass by reference? [duplicate]
(13 answers)
Closed 8 years ago.
I'd like understand why this difference (I suppose works like Java, so it's stack and heap difference)
var a = 10;
console.log(a);//10
function ChangeVal(){ b=a; b++; }
console.log(a);//10
var a = {name:"MyName"};
console.log(a);//{name:"MyName"}
function ChangeVal(){ b=a; b.name = "YourName"; }
console.log(a);//{name:"YourName"}
Assigning the value of one variable to another always involves copying the value. Thus:
b = a;
assigns the value of variable "a" to the (global! you forgot var!) variable "b". That happens in both your examples.
In the first example, the value of "a" is the number 10. In the second, it's a reference to an object. After
b = a;
in both cases, the variable "b" has the same value — a copy of the same value — as "a".
Because one reference to a particular object is as good as another, in your second example it's perfectly natural that changing the value of the "name" property on that object can be done via the reference in either "a" or "b".

Categories

Resources