473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

newbie: constants in JavaScript

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[i].checked)
{
elem = elem[i];
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!
Nov 8 '08 #1
5 4636
Jivanmukta wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 8 '08 #2
On Sat, 08 Nov 2008 19:03:17 +0100, Jivanmukta wrote:
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.

Nov 8 '08 #3
Jivanmukta wrote:
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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Nov 8 '08 #4
Jeremy J Starcher <r3***@yahoo.comwrites:
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
--
Lasse Reichstein Holst Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Nov 9 '08 #5


Thomas 'PointedEars' Lahn wrote:
Currently
two ECMAScript implementations, JavaScript and JScript, are compared against
each other and against ECMAScript at <http://PointedEars.de/es-matrix>.
Superb table! Many thanks.
Nov 10 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Ted | last post by:
Is there a way to define a constant value for a color in a CSS declaration? I have numerous colors in different CSS elements, and if I make a change in color, I have to change all the reference...
0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
3
by: www.gerardvignes.com | last post by:
I do most JavaScript programming with objects and methods. I use tiny boot and shutdown scripts as hooks for the browser load and unload events. My JavaScripts interact with scripts running on...
1
by: david.binner | last post by:
Hi, folks. I am writing some numerical math programs in Javascript (translating from C++) and am seeking your advice regarding machine constants. C++ offers the <cfloatlibrary for accessing...
5
by: mattmao | last post by:
Greetings everyone. The Newbie is coming back from uni... I knew making a good form validation JavaScript would be a piece of cake for you guys, but as a novice programmer as me, I still find...
13
by: paragpdoke | last post by:
Hello Everyone. Merry Christmas to all ! I'm a JavaScript newbie (and new to thescripts.com too). And I have problems in working with objects in JavaScript. The problem: <a...
44
by: Andy Dingley | last post by:
On 15 May, 04:55, Prisoner at War <prisoner_at_...@yahoo.comwrote: Or in another universe, where things are understood and site code is stable and reliable, beginners don't even think about...
2
by: Leslie Sanford | last post by:
I want to define a set of floating point constants using templates insteand of macros. I'd like to determine whether these constants are floats or doubles at compile time. In my header file, I have...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.