Sample Video Frame

Created by Zed A. Shaw Updated 2024-06-20 22:54:43

05: Constants

Sometimes you need to create a variable that can never change. In programming this is usually called a "constant". A constant is more like a fact that should be the same value all the time. The concept of a constant is fairly new to JavaScript, so there are not too many guidelines on how to use them. I would say, use them sparingly until you figure out what works best. One place you will see them used is when you create functions in new JavaScript code.

The Code

View Source file code.js Only

const invincible = 94;
let changeme = 82;

console.log(`invincible=${invincible} but changeme=${changeme}`);

// this will work just fine
changeme = 100;

// uncomment this to see how you can't do it
// invincible = 100000;


console.log(`After change: invicible=${invincible} but changeme=${changeme}`);

As you can see here the keyword const is used to create a constant. This means a variable cannot change, so if you look at lines 9 and 10, you will see they are commented out (meaning disabled). You should remove the comment in front of line 10 to see thet error message you get.

What You Should See

When you run the code normally you should see this output:

invincible=94 but changeme=82
After change: invicible=94 but changeme=100

However, if you had the error of attempting to change the constant invincible then you should see this instead:

$ node ex05-constants.js
invincible=94 but changeme=82
/Users/zedshaw/Books/learn-js-the-hard-way/ex05-constants.js:7
invincible = 100000;
       ^

TypeError: Assignment to constant variable.
    at Object.<anonymous> (/Users/zedshaw/Books/learn-js-the-hard-way/ex05-constants.js:7:12)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Function.Module.runMain (module.js:701:10)
    at startup (bootstrap_node.js:194:16)
    at bootstrap_node.js:618:3

If you don't know how to read this error message yet, then be sure to study it carefully as understanding error messages is a very important skill in programming. As I mentioned before, programming is nothing but a series of failures until everything works. Maybe.

The key is to find the location in your file where the error happened first, which in this case is the learn-js-the-hard-way/ex05-constants.js:7:12 part of the error output, which says:

  1. ex05-constants.js file.
  2. 7 line.
  3. 12 character.

You then work backwards going down the list and finding each location, to see if that location has a clue to the problem.

The final advice is to Google the error message with javascript ERROR such as javascript Assignment to constant variable. Then read what people say this error message means. Sometimes the actual message makes sense, but many times it's confusing, so that's why you search for it.

Previous Lesson Next Lesson

Register for Learn JavaScript the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.