FAQ: Variables - Create a Variable: const

This community-built FAQ covers the “Create a Variable: const” exercise from the lesson "Variables ".

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Create a Variable: const

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

5 posts were split to a new topic: What are the differences between var and let? Are they necessary?

I clicked on view solution, and it supposedly gave me the answer, an error popped up.
Error message:
/home/ccuser/workspace/learn-javascript-variables-constV2/main.js:5
const testing;
^^^^^^^
SyntaxError: Missing initializer in const declaration
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

Please fix this problem, Codecademy developers! I would be grateful for your help.
:sweat_smile:

Nothing to fix? This is deliberate, step 3 says:

After you clear this checkpoint, if you want to see about another quirk of const in action open the hint!

please check the hint, all should be explained

1 Like

Now I understand, I don’t think I read it properly now. Thanks

Hi. Why is const even talked about as a variable? A variable can vary. A constant stays constant. It remains the same. If someone can explain this to me without doublespeak I would appreciate it.

variable is a generic term to describe the name we give to our data. The lexicon is well established and one would suspect the working group did not see a need to add to the nomenclature over such a minor concern.

A const is a variable with a permanent, immutable binding. We just have to live with that.

1 Like

I will live with it perfectly fine. It just contradicts what i learned from a year of being a straight A student at a vocational programming school in the 1980’s. Thanks for for the explanation and response. I was taught variables are values that can change either internally from internal math or string manipulations or externally from another program or user input to any given program and constants do not change. Pretty simple. You just used different wording to express the same concept and some working group re-defined the meaning of the English word variable over the years. I will just go ahead and think of it as a peculiar use of the word variable in relation to computer programming.

2 Likes

Why do constants even exist? Isn’t that what hardcoding is for?

const protects variables, even if they are hard-coded. But more importantly, it protects functions and data structures from being removed. We cannot alter a protected function. The size and contents of an array can be changed, as with the properties of an object, but their references are fixed. This is very useful, on the whole.

2 Likes

@wispring, @andythurman041968214, perhaps this trivial example of variables declared with const will shed some light. You don’t know ahead of time what value the variable will be assigned to, but once assigned, the binding is constant as @mtf explained.

const reorder = arr => {
  const newArray = arr.slice(); //make a copy, so we don't mutate the original array
  for(let i = 0; i < newArray.length; i += 2) {
    const temp = newArray[i];
    const next = i + 1;
    if(next >= newArray.length) {
      break;
    };
    newArray[i] = newArray[next];
    newArray[next] = temp;
  };
  return newArray;
};

console.log(reorder([1,2,3,4,5,6,7,8,9,10]));
const someArray = ['dog', 'cat', 'elephant', 'mouse', 'Larry', 'Sue', 'potato salad'];
console.log(reorder(someArray));
console.log(someArray); //make sure the original array was unchanged

Output:

[ 2, 1, 4, 3, 6, 5, 8, 7, 10, 9 ]
[ ‘cat’, ‘dog’, ‘mouse’, ‘elephant’, ‘Sue’, ‘Larry’, ‘potato salad’ ]
[ ‘dog’, ‘cat’, ‘elephant’, ‘mouse’, ‘Larry’, ‘Sue’, ‘potato salad’ ]

What on earth is a constant variable??? It’s an oxymoron.

The term variable is universal. It refers to the name we give to an object reference. Think of it as the identifier, in this case of a constant value in memory that can not be reassigned. If the value is a primitive it is persistent for the entire session.

When the value is a reference object such as an array or object, the structure cannot be reassigned, but the values it contains are still mutable.