JavaScript Arrays, Loops and Conditionals (Part 1)

I’m really struggling with JavaScript Arrays, Loops and Conditionals.

I decided to record a Loom video with me narrating my thought process and questions, in hopes experienced programmers can try and help me wrap my head around this.

Thanks in advance!

Hello @mega1685931740. At 6:11 minutes in the video, the error thrown is not because of your

console.log(userName);

It is because in your ternary expression, your syntax is wrong. If you look at the 5th line of the error message, you will see that it says TypeError: userName is not a function. This is due to the ternary expression userName() console.log("Hello") etc. Where you have userName(), what you are actually doing is invoking, or calling a function. Instead, you should try a ternary operator like this:

userName === "" ? console.log("string1") : console.log("string2");

What you are doing here is saying if UserName is equal to an empty string, print string1. Otherwise, print string2. Here is a link to website that explains and demonstrates the syntax of ternary operator well: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
I hope that helped!

1 Like

Also, when printing just a variable, you can just write:

console.log(variableName);

With const and let, things like userName and question, which can only be changed, in this project, by the user accessing the code, the program does not change their values, which means they can be assigned const, as it makes it clear that they cannot be changed halfway through the program. For example, if you were to write:

const question = "question1?";
console.log(question);

and ran it, you would have “question1?” printed. If you ran it again, but before you ran it, you changed it:

const question = "question2?";
console.log(question);

it would run perfectly well. However, using const stops question from accidentally being changed halfway through the program.
I hope this helps!

1 Like