DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > How To Create Constants in JavaScript

How To Create Constants in JavaScript

Dhananjay Kumar provides a simple example of creating and using constants in JavaScript.

Dhananjay Kumar user avatar by
Dhananjay Kumar
·
Dec. 07, 16 · Web Dev Zone · Tutorial
Like (3)
Save
Tweet
11.19K Views

Join the DZone community and get the full member experience.

Join For Free

constants are immutable variables which value cannot be changed. once, you have created a constant, its value cannot be changed.

while coding in javascript, many times you may have come across a requirement to create constants. before ecma script 6, it was not very easy to create constants in javascript. in this post, i will show you to create constants in both ecma script 5 and ecma script 6.

constants in ecma 5

we can create a constant in ecma script 5 using object.defineproperty().  first, we need to find out, whether variable would be a global variable or part of the window object.  once that is determined, create variable by setting writable to false.

object.defineproperty(typeof global === "object" ? global : window, 
    "foo", 
    { 
       value: 10000, 
       enumerable: true, 
       configurable: true, 
       writable: false 
     }); 

object.defineproperty() function takes three parameters,

  1. object for which variable should be created
  2. name of the variable to be created
  3. object to configure behavior of the variable.

to create a constant,

  1. as first parameter, we are passing either window object or global object
  2. as second parameter, we are passing name of the variable to be created, which is foo in this case.
  3. as third parameter, we are passing object to configure the variable behavior. keep in mind to make the writable property to false.

we have created a constant foo. if we try to reassign value of foo, javascript will ignore it. however, if we run, javascript in strict mode, then javascript will throw exception.  let us see this in action, we are running javascript in strict mode and trying to reassign value of foo.

"use strict" 
    object.defineproperty(typeof global === "object" ? global : window, 
       "foo", 
       { 
          value: 10000, 
          enumerable: true, 
          configurable: true, 
          writable: false 
        }); 

    console.log(foo); 
    foo = 90; 
    console.log(foo); 

due to strict mode, javascript will throw exception as shown in the image below:

in this way using the object.defineproperty() and making writable to false, we can create a constant in ecma script 5.

constants in ecma 6

as you might have noticed that, creating constants in ecma script 5 was not very simple. in ecma script 6, new feature const has been introduced. it enables us to create constants or immutable variable.

const foo = 10000; 
console.log(foo); 

if we try to reassign value of foo, javascript will complain about it in ecma script 6.  let us try to reassign value of foo as shown in the listing below:

const foo = 10000; 
console.log(foo); 
foo= 90; 
console.log(foo); 

javascript will complain about this as shown in the image below:

these are two ways constants can be created in javascript. i hope you find this post useful.

thanks for reading.

JavaScript

Published at DZone with permission of Dhananjay Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Is NoOps the End of DevOps?
  • Top 20 Git Commands With Examples
  • Kubernetes Service Types Explained In-Detail
  • Hyperautomation: The Beacon for Your Digital Transformation Journey

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo