SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member aledcoe's Avatar
    Join Date
    Apr 2010
    Location
    Dundee, Scotland
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question ++variable vs variable++

    Hi all,

    I'm reading Sitepoint's Simply JavaScript at the moment and am a bit confused by some information on page 26.

    Here, Cameron Adams is trying to explain the difference between:

    var age=26;
    var ageCopy = age++;

    and

    var age=26;
    var ageCopy = ++age;

    He says that in the first example, ageCopy is equal to 26, but in the second example, ageCopy == 27.

    I don't understand this - isn't ageCopy equal to 27 in both of these examples? What is the actual difference between these statements?

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No

    ageCopy = age++ - increments and then assigns

    ageCopy = ++age - assigns and then increments

    In both cases age ends up as 27 but not ageCopy

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by PhilipToop View Post
    ageCopy = age++ - increments and then assigns

    ageCopy = ++age - assigns and then increments
    ORLY? Are you sure it isn't the other way around?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry got that completely the wrong way around

    ageCopy = age++ - assigns and then increments

    ageCopy = ++age - increments and then assigns

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Example:
    Code JavaScript:
    var a = 1;
    alert(a++); // alerts 1
     
    var a = 1;
    alert(++a); // alerts 2

    ++var is only ever needed in some very special cases. var++ will suffice for 99,99% of the cases where you need to increment a variable
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Member localtraffic's Avatar
    Join Date
    Jul 2010
    Location
    PA, USA
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The Major difference between ++a , a++ is the manner of increment and assignment.

    in first case agecopy=age++
    let the value of age is 26
    in that case agecopy first assigns the non incremented value if age.
    later compiler increments the age value to 1 and the final age value
    becomes 27

    in second case agecopy=++age
    let the value of age is 26
    now compiler increment the value of age to 1 and final age value becomes
    27.
    later it compiler assigns the incremented value to agevalue.
    and ultimately agevalue becomes 27.

  7. #7
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,276
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    I had the same problem the first time I read that in Simply Javascript.

    I think it's a badly-done example. If he had added some more code afterwards, it would have been clearer.

  8. #8
    SitePoint Member aledcoe's Avatar
    Join Date
    Apr 2010
    Location
    Dundee, Scotland
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stomme poes View Post
    I had the same problem the first time I read that in Simply Javascript.

    I think it's a badly-done example. If he had added some more code afterwards, it would have been clearer.
    Yeah, I agree with you - it could do with a few extra lines of explanation.

    Thanks for the help everyone, I understand it now.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •