PDA

View Full Version : var vs const



shachi
03-09-2007, 04:31 PM
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!!

jscheuer1
03-09-2007, 05:02 PM
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.

Blake
03-09-2007, 05:02 PM
Use const if you want to be sure that it doesn't ever get changed, use var otherwise.

Try this, for example:



<script type="text/javascript">

const i=4;
i=3;
alert(i);

</script>

jscheuer1
03-09-2007, 05:14 PM
Use const if you want to be sure that it doesn't ever get changed, use var otherwise.

Try this, for example:



<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.

Twey
03-09-2007, 06:37 PM
const is a Javascript 1.7 addition, if I remember correctly, so only Spidermonkey (and possibly Rhino) have implemented it.

jscheuer1
03-09-2007, 07:14 PM
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:


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.

mburt
03-09-2007, 07:35 PM
What's the point of the const keyword? Are int and bool keywords too?

Twey
03-09-2007, 07:50 PM
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.

mburt
03-09-2007, 07:54 PM
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 :p. If they add the other keywords it could soon look alot like C++...

Twey
03-09-2007, 08:06 PM
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.

shachi
03-10-2007, 07:08 AM
But I don't get it, why use const? Changing or not changing the variable is our(the programmers) decision, what's the point of const?

Twey
03-10-2007, 07:12 AM
It depends entirely on the philosophy of the language (and the programmer). In low-level languages like C, having constants makes a lot of sense because they end up being stored in a special read-only data section in the final executable file. In higher-level languages, the performance difference doesn't really happen; however, if a variable's not meant to be changed, making it constant just ensures that it won't be by accident.

The Python philosophy, however, agrees with you, in that if the programmer wants to break the program, it's not the language's job to stop them :)

boogyman
03-10-2007, 07:27 AM
A constant should be used when you dont want the variable to change whatsoever... like the Math term M_PI ... another example is if you are trying to do a script where you need to evaluate 1+ variable(s) to 1 variable ... a constand would be a great tool here... e.g. maxtries? or maxnumber or minnumber

jscheuer1
03-10-2007, 07:41 AM
I had said before and I think it bears repeating that a javascript constant would be handy for setting and remembering values that can otherwise change. For example:


const spans=document.getElementsByTagName('span');

Once we have that, we could change the number of spans in the document but still remember what it was at the outset. This would allow us to iterate through them all, removing some or adding some without losing our place. With a variable, its value will, I believe, change dynamically as we add or subtract spans to or from the page.

This is really somewhat moot though as, it isn't supported in IE or Opera.

Twey
03-10-2007, 09:03 AM
Once we have that, we could change the number of spans in the document but still remember what it was at the outset. This would allow us to iterate through them all, removing some or adding some without losing our place. With a variable, its value will, I believe, change dynamically as we add or subtract spans to or from the page.That's not quite how it works :)

The reference to that collection that is stored in spans would remain the same -- it would always point to the same collection -- but the collection itself would still change.

shachi
03-10-2007, 11:39 AM
I think I get it now, but should I start using constants? I mean is it available on all ECMA script specifications?

Twey
03-10-2007, 12:52 PM
As we've already said, no, and no. ECMAScript does not define any such keyword. It's implemented only in Mozilla's JavaScript 1.5+.

pcbrainbuster
03-10-2007, 01:32 PM
MAAAAAAAAN ! Twey have you got experience at everything - your like a coding machine :)

techno_race
03-10-2007, 10:46 PM
const should cause a variable to be unchangeable. No error should be thrown, assignment operations should simply not be carried out on that variable.
But someone could freely change it to a variable or change the 'should be' value of the constant.

mburt
03-11-2007, 01:29 AM
In my opinion, there is no point to const:

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.

shachi
03-11-2007, 06:59 AM
In my opinion, there is no point to const:


Mine too. But read Twey's points too:



It depends entirely on the philosophy of the language (and the programmer). In low-level languages like C, having constants makes a lot of sense because they end up being stored in a special read-only data section in the final executable file. In higher-level languages, the performance difference doesn't really happen; however, if a variable's not meant to be changed, making it constant just ensures that it won't be by accident.

The Python philosophy, however, agrees with you, in that if the programmer wants to break the program, it's not the language's job to stop them


But some time ago(which is some minutes ago), I just saw this mozilla default js file which may define it all, here are some of it's lines:



const nsISupports = Components.interfaces.nsISupports;
39
40 const nsIBrowserDOMWindow = Components.interfaces.nsIBrowserDOMWindow;
41 const nsIBrowserHandler = Components.interfaces.nsIBrowserHandler;
42 const nsIBrowserHistory = Components.interfaces.nsIBrowserHistory;
43 const nsIChannel = Components.interfaces.nsIChannel;
44 const nsICommandLine = Components.interfaces.nsICommandLine;
45 const nsICommandLineHandler = Components.interfaces.nsICommandLineHandler;
46 const nsIContentHandler = Components.interfaces.nsIContentHandler;
47 const nsIDocShellTreeItem = Components.interfaces.nsIDocShellTreeItem;
48 const nsIDOMChromeWindow = Components.interfaces.nsIDOMChromeWindow;
49 const nsIDOMWindow = Components.interfaces.nsIDOMWindow;
50 const nsIFactory = Components.interfaces.nsIFactory;
51 const nsIFileURL = Components.interfaces.nsIFileURL;
52 const nsIHttpProtocolHandler = Components.interfaces.nsIHttpProtocolHandler;
53 const nsIInterfaceRequestor = Components.interfaces.nsIInterfaceRequestor;
54 const nsIPrefBranch = Components.interfaces.nsIPrefBranch;
55 const nsIPrefLocalizedString = Components.interfaces.nsIPrefLocalizedString;
56 const nsISupportsString = Components.interfaces.nsISupportsString;
57 const nsIURIFixup = Components.interfaces.nsIURIFixup;
58 const nsIWebNavigation = Components.interfaces.nsIWebNavigation;
59 const nsIWindowMediator = Components.interfaces.nsIWindowMediator;
60 const nsIWindowWatcher = Components.interfaces.nsIWindowWatcher;
61 const nsICategoryManager = Components.interfaces.nsICategoryManager;
62 const nsIWebNavigationInfo = Components.interfaces.nsIWebNavigationInfo;
63 const nsIBrowserSearchService = Components.interfaces.nsIBrowserSearchService;
64
65 const NS_BINDING_ABORTED = 0x804b0002;
66 const NS_ERROR_WONT_HANDLE_CONTENT = 0x805d0001;
67 const NS_ERROR_ABORT = Components.results.NS_ERROR_ABORT;
68


The file was nsBrowserContentHandler.js which I found out through venkman.

Twey
03-11-2007, 11:45 AM
But someone could freely change it to a variable or change the 'should be' value of the constant.The former should throw an error; the latter is simply impossible, there's no grammar for it.

shachi
03-12-2007, 02:51 PM
Thanks everyone for clearing it up. :D