Defining Constants in JavaScript: Browser Issues

This blog is based off of http://www.hedgerwow.com/360/dhtml/js_constant.html.

A constant is defined as:
A variable whose value cannot change through assignment or be re-declared while the script is running
A constant is declared using the const keyword.
The scope of a constant is the same as the scope would have been had it not been declared as a constant (local within a function, global outside of a function, or as a closure.

Thru a simple example, Hedger shows that Opera does not respect constants.

If you define a constant, you should not be able to change the value of the constant in your script.

function hello() {};
  const hello = 'goodbye'; //should throw an error

function myfunction() {
  const hello= 'goodbye';
  var hello = 42; //should throw an error
  //statements
}

Hedger defines his constants using the define method.

Advertisement

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s