Hello Coding Forums Community,
I have to identify the errors in the piece of code below and I have the correction but I don't fully understand it:
"use strict";
const prefix = "username_";
let userName = "Jack";
const userName = "Adam";
let prefixedUserName;
const prefixedUserName;
userName = "John";
prefixedUserName = prefix + userName;
console.log(prefixedUserName + prefixedUserName2);
console.log(prefixedUserName2);
Uncaught SyntaxError: Identifier 'userName' has already been declared
****************************************************************************************************
The answer:
"use strict";
const prefix = "username_";
let userName = "Jack";
// const userName = "Adam";
let prefixedUserName;
// const prefixedUserName;
userName = "John";
prefixedUserName = prefix + userName;
console.log(prefixedUserName /+ prefixedUserName2/);
// console.log(prefixedUserName2);
****************************************************************************************************
I got prefixedUserName2 was never declared, thus error. For const it seems to be a redundant declaration, right?
But why cannot keep const instead of let?
"use strict";
const prefix = "username_";
// let userName = "Jack";
const userName = "Adam";
// let prefixedUserName;
const prefixedUserName;
userName = "John";
prefixedUserName = prefix + userName;
console.log(prefixedUserName /+ prefixedUserName2/);
// console.log(prefixedUserName2);
Uncaught SyntaxError: Missing initializer in const declaration
I have to identify the errors in the piece of code below and I have the correction but I don't fully understand it:
"use strict";
const prefix = "username_";
let userName = "Jack";
const userName = "Adam";
let prefixedUserName;
const prefixedUserName;
userName = "John";
prefixedUserName = prefix + userName;
console.log(prefixedUserName + prefixedUserName2);
console.log(prefixedUserName2);
Uncaught SyntaxError: Identifier 'userName' has already been declared
****************************************************************************************************
The answer:
"use strict";
const prefix = "username_";
let userName = "Jack";
// const userName = "Adam";
let prefixedUserName;
// const prefixedUserName;
userName = "John";
prefixedUserName = prefix + userName;
console.log(prefixedUserName /+ prefixedUserName2/);
// console.log(prefixedUserName2);
****************************************************************************************************
I got prefixedUserName2 was never declared, thus error. For const it seems to be a redundant declaration, right?
But why cannot keep const instead of let?
"use strict";
const prefix = "username_";
// let userName = "Jack";
const userName = "Adam";
// let prefixedUserName;
const prefixedUserName;
userName = "John";
prefixedUserName = prefix + userName;
console.log(prefixedUserName /+ prefixedUserName2/);
// console.log(prefixedUserName2);
Uncaught SyntaxError: Missing initializer in const declaration