SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member walkamongus's Avatar
    Join Date
    Jul 2007
    Location
    VA
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simply Javascript p.26

    I am brand-spanking new to programming. I have learned XHTML and CSS and I'm attempting to learn Javascript now. After reading the first 2 chapters theres only one thing I'm having trouble getting my mind wrapped around:

    I don't understand the functional differences described in the "before or after" box. I don't understand why "var age = 26; var ageCopy = age++;" will make ageCopy = 26 but placing the increment operator before "age" will make the value = 27.

  2. #2
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have a really technical explanation, but that is simply a "feature" of many programming languages.

    having var ageCopy = age++; says "makes ageCopy equal to age, but then increment age by 1". ageCopy remains the same because you are not actually copying the VARIABLE, but rather the VALUE... so you can modify age however you want and ageCopy remains unaffected...

    Likewise, having ++age is just like a sub-function. before assigning it to ageCopy, it says increment age by 1... so now 27. Noiw that we've done that, we can assign it to ageCopy... so ageCopy gets a value of 27

    Hope that helps!
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  3. #3
    SitePoint Member walkamongus's Avatar
    Join Date
    Jul 2007
    Location
    VA
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so if age = 26, then "var ageCopy = age++" is assigning the value of "age" to "ageCopy" and then assigning the new incremented value to "age"?

    If that is correct, its almost like 2 actions: 1) assigning "age" to "ageCopy" and then 2) performing "age = age+1"

    and if you put the increment operator before "age", then it does these 2 steps in reverse order.

    Sorry for the trouble, I just have a habit of making sure I understand the "how" and "why" of everything I'm learning.

  4. #4
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, that's the way I would understand it.
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  5. #5
    SitePoint Member walkamongus's Avatar
    Join Date
    Jul 2007
    Location
    VA
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    awesome, thanks for the help!

    i'm sure i'll have plenty more questions...


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
  •