2018-10-12 15:37:13 -04:00
---
title: Declare a Read-Only Variable with the const Keyword
---
2019-07-24 00:59:27 -07:00
# Declare a Read-Only Variable with the const Keyword
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
Change all the variables to `let` or `const` and rename `sentence` .
2019-07-24 00:59:27 -07:00
---
## Hints
### Hint 1
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
* Replace `var` for string with read-only `const` .
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
### Hint 1
2018-10-12 15:37:13 -04:00
* Replace `var` in `for` loop to `let` .
2019-07-24 00:59:27 -07:00
### Hint 1
2018-10-12 15:37:13 -04:00
* Common convention is to name `const` variables with ALL CAPS.
2019-07-24 00:59:27 -07:00
---
## Solutions
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
```javascript
2019-07-24 00:59:27 -07:00
function printManyTimes(str) {
"use strict";
const SENTENCE = str + " is cool!";
for (let i = 0; i < str.length ; i + = 2 ) {
console.log(SENTENCE);
}
}
printManyTimes("freeCodeCamp");
2018-10-12 15:37:13 -04:00
```
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-12 15:37:13 -04:00
By using `const` on `sentence` we can make it read-only and by using `let` on `i` inside the for loop we can avoid using `var` all together. For added code clarity we can also change `sentence` to `SENTENCE` to show that it is a constant.
#### Relevant Links
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const' target = '_blank' rel = 'nofollow' > const</ a >
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let' target = '_blank' rel = 'nofollow' > let</ a >
2019-07-24 00:59:27 -07:00
< / details >