0

Hey guys, what is the difference between ++1 and 1++? And also when used with minus. Gets me confused.

var a=++1 var b= 1++ var c = a+b alert ("c") ; What will be the outcome?

4th Mar 2017, 12:30 PM
Elekwa Solomon
Elekwa Solomon - avatar
9 Réponses
+ 11
Refer to my variables code for a reference
4th Mar 2017, 1:19 PM
Siddharth Saraf
+ 8
You should check this question 😉: https://www.sololearn.com/Discuss/160327/?ref=app
4th Mar 2017, 12:33 PM
C.E.
C.E. - avatar
+ 5
this won't work. I don't really know for JavaScript, but in C++ this would cause an error. ++ means take the variable and add one to it. so if x is 2 and you say x++ this will lead to x=x+1, so x=2+1 since variable names must not start with a number and in your example 1 would be processed as a variable, you'll get an error, because 1++ would read as 1=1+1 and as we all know 1=2 is false.
4th Mar 2017, 1:03 PM
Mario L.
Mario L. - avatar
+ 4
it was just an example. in examples it doesn't matter if the variable names are a, b, c, x or cookieMonsterDominatorOfTheWorld9000X. fact is, that a=1++ will cause an error, because ++ and -- include assignments. and you can't assign anything to a number.
4th Mar 2017, 1:19 PM
Mario L.
Mario L. - avatar
+ 4
okay, let's start over: what do you want your code to do?
4th Mar 2017, 2:29 PM
Mario L.
Mario L. - avatar
+ 3
++1& 1++ are operators that increment a value while --1 & 1-- they decrement a value.In ++1 you increment the value before assigning it while 1++ you assign the value is first assigned then incremented.
4th Mar 2017, 1:43 PM
Kelvin Gathungu
Kelvin Gathungu - avatar
0
I used the variables a, b and c in my question. So i don't really you answering with "x" @Mario
4th Mar 2017, 1:08 PM
Elekwa Solomon
Elekwa Solomon - avatar
0
I know you're tryna help. But you gotta bring it down a few notches my level. I'm kinda new to programming man. @Mario
4th Mar 2017, 1:22 PM
Elekwa Solomon
Elekwa Solomon - avatar
0
Actually i wasn't making anything in particular. Was just tryna understand how it works. Encountered it on Challenges and kept loosing.
6th Mar 2017, 9:11 AM
Elekwa Solomon
Elekwa Solomon - avatar