Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: var vs const

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default var vs const

    Hello all,
    I have a simple question. There is this keyword const in javascript, has anyone ever used it? I had never had to use it so if anyone actually has used it, can you tell me which is better to use? Simply var or const?
    Thanks!!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'd never heard of such a thing and thought it might be real useful for like getting the number of DOM elements that would later change so that the original number would be retained and available after those changes. But, its behavior in testing is inconsistent as far as I can tell.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2007
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Use const if you want to be sure that it doesn't ever get changed, use var otherwise.

    Try this, for example:

    Code:
    <script type="text/javascript">
    
    const i=4;
    i=3;
    alert(i);
    
    </script>
    "Rock and roll ain't noise pollution." - AC/DC

    http://www.blake-foster.com

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Blake View Post
    Use const if you want to be sure that it doesn't ever get changed, use var otherwise.

    Try this, for example:

    Code:
    <script type="text/javascript">
    
    const i=4;
    i=3;
    alert(i);
    
    </script>
    This is just my point, here are some results:

    Opera - alerts 3
    FF - alerts 4
    IE gives syntax error on the line declaring the constant (this really refers to the line below that which attempts to change it).

    As M. Gandhi said of Western Civilization:

    "I think it would be a good idea."

    Note: If you remove the error line in IE, it doesn't even give an alert.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    const is a Javascript 1.7 addition, if I remember correctly, so only Spidermonkey (and possibly Rhino) have implemented it.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I saw it listed as a part of 2.0 but, that doesn't mean it wasn't implemented before that. FF got it right in any case except if it was written as:

    Code:
    const i : int = 3
    Which is supposed to be a way to define the data type. Done that way, FF threw an error.

    Bottom line is, I think:

    Only useful for compiled jscript (jscript.net) and server side code where supported as, on the client side, support is still too inconsistent.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    What's the point of the const keyword? Are int and bool keywords too?
    - Mike

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    int and bool are not currently keywords, but they are reserved for future use.

    const causes a variable to be unchangeable. No error is thrown, assignment operations are simply not carried out on that variable.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    No error is thrown
    Try IE: As John said, it gives an error (typical of IE).
    I think it shouldn't be tampered with though, that they should leave it at "var". It can be assigned boolean, integer, and string values, it's pretty much the ultimate keyword . If they add the other keywords it could soon look alot like C++...
    - Mike

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Try IE: As John said, it gives an error (typical of IE).
    I was talking about interpreters that support it. Perhaps I should have said:
    const should cause a variable to be unchangeable. No error should be thrown, assignment operations should simply not be carried out on that variable.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •