6

Possible Duplicate:
Are there constants in Javascript?

For example here I set the close time on this menu to 1 second or 1000 milliseconds.

menu.menu_timer=window.setTimeout(menu.hide_menu, 1000);

Does javascript have a const like C or a Define like PHP to define Constants?

Community
  • 1
  • 1

2 Answers2

11

Update 2017

ES6, or ECMAScript2017, comes with the support for constants using the keyword const.

While some use camelCase syntax for the constant's names, it still is perfectly valid to use the capitalized (snake_case) version (e.g. const VARIABLE_NAME;)


Original answer

JavaScript does not have constants.

You could use the convention of all uppercase for constants.

var PI = 3.14;

or you could wrap it in an object.

window.CONSTANTS = {
   PI : 3.14
};
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
4

The standard convention for constants in JavaScript is to use all uppercase like

var MY_CONST = 1000;

There is a const keyword but I believe it is not supported in IE

pradeek
  • 21,445
  • 2
  • 31
  • 32