* Update: Added info re uppercase and lowercase Included information to explain that lowercase and camelCase can be used for const identifiers. * Fix: corrected mutable to immutable I corrected the error that uppercase is used for mutable values. * Fix: changed ** ** to <strong></strong> I changed the markdown ** ** to <strong></strong>, as I was informed that ** didin't work.
3.4 KiB
3.4 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
587d7b87367417b2b2512b41 | Declare a Read-Only Variable with the const Keyword | 1 |
Description
let
is not the only new way to declare variables. In ES6, you can also declare variables using the const
keyword.
const
has all the awesome features that let
has, with the added bonus that variables declared using const
are read-only. They are a constant value, which means that once a variable is assigned with const
, it cannot be reassigned.
"use strict"As you can see, trying to reassign a variable declared with
const FAV_PET = "Cats";
FAV_PET = "Dogs"; // returns error
const
will throw an error. You should always name variables you don't want to reassign using the const
keyword. This helps when you accidentally attempt to reassign a variable that is meant to stay constant. A common practice when naming constants is to use all uppercase letters, with words separated by an underscore.
Note: It is common for developers to use uppercase variable identifiers for immutable values and lowercase or camelCase for mutable values (objects and arrays). In a later challenge you will see an example of a lowercase variable identifier being used for an array.
Instructions
let
or const
. Use let
when you want the variable to change, and const
when you want the variable to remain constant. Also, rename variables declared with const
to conform to common practices, meaning constants should be in all caps.
Tests
tests:
- text: <code>var</code> does not exist in your code.
testString: getUserInput => assert(!getUserInput('index').match(/var/g),'<code>var</code> does not exist in your code.');
- text: <code>SENTENCE</code> should be a constant variable declared with <code>const</code>.
testString: getUserInput => assert(getUserInput('index').match(/(const SENTENCE)/g), '<code>SENTENCE</code> should be a constant variable declared with <code>const</code>.');
- text: <code>i</code> should be declared with <code>let</code>.
testString: getUserInput => assert(getUserInput('index').match(/(let i)/g), '<code>i</code> should be declared with <code>let</code>.');
- text: <code>console.log</code> should be changed to print the <code>SENTENCE</code> variable.
testString: getUserInput => assert(getUserInput('index').match(/console\.log\(\s*SENTENCE\s*\)\s*;?/g), '<code>console.log</code> should be adjusted to print the variable <code>SENTENCE</code>.');
Challenge Seed
function printManyTimes(str) {
"use strict";
// change code below this line
var sentence = str + " is cool!";
for(var i = 0; i < str.length; i+=2) {
console.log(sentence);
}
// change code above this line
}
printManyTimes("freeCodeCamp");
Solution
function printManyTimes(str) {
"use strict";
// change code below this line
const SENTENCE = str + " is cool!";
for(let i = 0; i < str.length; i+=2) {
console.log(SENTENCE);
}
// change code above this line
}
printManyTimes("freeCodeCamp");