JS exercise ''find the mistake'' not understood well

Joined
Feb 16, 2024
Messages
5
Reaction score
0
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
 
Joined
Sep 21, 2022
Messages
122
Reaction score
15
The difference between a constant and a variable is that a constant may be assigned only once.
Code:
// let userName = "Jack";
const userName = "Adam";

// let prefixedUserName;
const prefixedUserName;
*This line needs a value.*

userName = "John";
*This line is trying to reassign.*
 
Joined
Sep 21, 2022
Messages
122
Reaction score
15
1 let userName = "Jack";

This line assigns "Jack" to userName, userName is a variable so it can be reassigned later.

2 const userName = "Adam";

This line assigns "Adam" to userName, userName is a constant so it can not be reassigned later.

3 let prefixedUserName;

This line declares a variable. Declaring a variable without an initial value, is valid.

4 const prefixedUserName;

This line is invalid, it is trying to declare a constant without a value.

5 userName = "John";

This line is valid, if userName is a variable. Invalid if userName is a constant.

But why cannot keep const instead of let?

answer: because of line 5
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,283
Latest member
JoannaGrif

Latest Threads

Top