newbie: constants in JavaScript

J

Jivanmukta

Hello,
I am learning JavaScript and I have a question concerning constants in
JavaScript:
Why JSLint (http://www.jslint.com/) reports error on the following code:

const reEmail = /^[a-z0-9-.]{1,40}@[a-z0-9-.]{1,70}$/;
const reDate = /^(\d{4})(\/|-|\.)(\d{1,2})(\/|-|\.)(\d{1,2})$/;
var error_message;
var focus_on;
function getElementValue(elem)
{
if (elem.tagName == 'input' && elem.length)
for (var i = 0, len = elem.length; i < len; i++)
if (elem.checked)
{
elem = elem;
break;
}
return elem.value;
}
....

JSLint reports:

Problem at line 1 character 1: Expected an identifier and instead
saw 'const'.
const reEmail = /^[a-z0-9-.]{1,40}@[a-z0-9-.]{1,70}$/;
Problem at line 1 character 7: Stopping, unable to continue. (0% scanned).

Is the syntax I use correct? If not, how to write it?

Please help. Thank you!
 
M

Martin Honnen

Jivanmukta said:
I am learning JavaScript and I have a question concerning constants in
JavaScript:
Why JSLint (http://www.jslint.com/) reports error on the following code:

const reEmail = /^[a-z0-9-.]{1,40}@[a-z0-9-.]{1,70}$/;

If you want to learn and use Javascript as currently standardized,
namely ECMAScript Edition 3, then you can't use const as that is not
part of that standard. If you want to learn and use JavaScript as
implemented by Mozilla's Spidermonkey or Rhino engine then you can use
const. So it depends, if you want to write scripts for the web including
browsers like IE then forget about const, you have only var. If you want
to write Firefox extensions for instance, then you can use const.
 
J

Jeremy J Starcher

Hello,
I am learning JavaScript and I have a question concerning constants in
JavaScript:

Why JSLint (http://www.jslint.com/) reports error on the following code:
JSLint reports:

Glad that you are using jslint. While it isn't perfect, it is a wonderful
tool.
Problem at line 1 character 1: Expected an identifier and instead saw
'const'.

The 'const' keyword is only known by a handful of emcascript
interpreters. I *believe* that so far only the Javascript engine in
Firefox supports it and *no* version of IE supports it.

Just make it a regular variable.

The jury is still out on if one should write it in ALLUPPERCASE or not.
I tend to, but there was a recent discussion on that where some of the
regulars opposed it.
 
T

Thomas 'PointedEars' Lahn

Jivanmukta said:
I am learning JavaScript and I have a question concerning constants in
JavaScript:
Why JSLint (http://www.jslint.com/) reports error on the following code:

const reEmail = /^[a-z0-9-.]{1,40}@[a-z0-9-.]{1,70}$/;
[...]
JSLint reports:

Problem at line 1 character 1: Expected an identifier and instead
saw 'const'.
const reEmail = /^[a-z0-9-.]{1,40}@[a-z0-9-.]{1,70}$/;
Problem at line 1 character 7: Stopping, unable to continue. (0% scanned).

Is the syntax I use correct?

That depends on which language you are talking about. JSLint, despite its
name, appears to check strictly against the syntax rules of the language
standard, ECMAScript; not JavaScript, the Netscape/Mozilla.org
implementation of ECMAScript.

Understand that you are not dealing with a single programming language
here, but several versions of several similar languages instead. Currently
two ECMAScript implementations, JavaScript and JScript, are compared against
each other and against ECMAScript at <http://PointedEars.de/es-matrix>.
More are yet to come.


PointedEars
 
L

Lasse Reichstein Nielsen

Jeremy J Starcher said:
The 'const' keyword is only known by a handful of emcascript
interpreters. I *believe* that so far only the Javascript engine in
Firefox supports it and *no* version of IE supports it.

Actually, Opera, Safari and Google Chrome also support the keyword.
Whether they also have the same semantics is always a good question
for a non-standardized feature :)

E.g.: the following program:
const x;
const y = x;
x = 42;
alert(x+y)
is a syntax error in Opera, but yields NaN in the remaining browsers.
And:
var x = 21;
const x = 42;
alert(x+x)
is an error in Safari and Firefox, but not in Opera or Chrome. Likewise
is:
const x = 42;
var x = 21;
alert(x+x)

Just make it a regular variable.

I agree. Const semantics is a mess to begin with.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top