0
Invalid regular expression flag s or invalid token error in ES6 or ECMA script?
code : function printManyTimes (str) { "use strict"; const SENTENCE = str + " is cool!"; for(let i=0; i<str.length; i+=2){ console.log(SENTENCE); } } printManyTimes("xyz");
3 Réponses
+ 4
Your code works fine for me.
Calviղ A const can definitely equal a variable, it's just that you may only assign to it once.
+ 2
const must be a constant, cannot equal to a variable. Use let insteads.
0
`var` and `let` create variable names which can be overwritten.
let mystr = "spam";
mystr = "eggs"; // reassigns!
`const` makes a variable name that stays pointing to that value. Changing it causes an error to be thrown.
const mystr = "123";
mystr = "456"; // ERROR!
What `const` does allow is if it was set to an object, that object still can be manipulated by its properties & methods.
const arr = [1, 2, 3];
arr.push(4, 5, 6);
console.log(arr); // 1,2,3,4,5,6
arr.length = 2;
console.log(arr); // 1,2
The usefulness of `const` is when it should never be reassigned while `var` (and its new `let`) can be reassigned, hence the names "constant" and "variable".