Variable with value of "" (ex. let answer = "")

Okay trying to understand JavaScript and doing okay so far. One issue I have with FCC is that the curriculum seems to give a great overview of practical knowledge you need to get started without necessarily explaining WHY things happen or work…and that is killing me because if I don’t understand “why” my brain has a hard time not just throwing the info out as soon as it comes in. For example:

Basic JavaScript working on switch statements and in part of a lesson example and practice problem I noticed something I have noticed before and it’s something I just don’t understand. When you have a line of code, like a “let” variable like this:

let answer = “”;

Why is the value of that answer empty quotes? What is the purpose of this? What does that tell JavaScript? I’ve tried googling it and I’m just not getting any answers. I’ve taken notes so far don’t have anything about it so I don’t think FCC had an explanation and it is killing me not knowing.

It’s just a placeholder your whatever answer you fill in.

2 Likes

This doesn’t tell javascript anything. It’s more of a convention, a way of signaling other developers later.

We could as easily have just done this:

let answer;

and left it be. What that line tells javascript is “eventually, we might need a variable named answer, so could you please hold a space for that? kthxbye!”

And later, we might do something like

answer = "Charlie Chaplin";

…we’ve now told javascript “that variable we mentioned? Yeah. Well, we’re gonna need it after all. So could you put the string "Charlie Chaplin" somewhere in the browser’s available RAM, and then tell that variable that it refers to that memory location? thxciao!”

So we can define a let variableName; and leave it empty. By doing that, we are letting javascript leave that connected to nothing, or undefined. If you log it before then, it will show as:

console.log( variableName ); // undefined

undefined is a special value, used to indicate that something is… not defined. It either hasn’t been defined yet, or its definition has been removed. This is not the same as null, though they’re similar.

But your question is about why let answer = "". Why we do that, what it means, what it tells javascript or what it might tell other developers.

Welp, it tells javascript "create this variable, and also place an empty string (the "") into the browser’s allocated RAM, and then wire those two things together.

But for a developer… we used to do this as an indication. Before we had tools to help us with Types, it would often be useful to know what kind of information we wanted to put into a variable. So when we declared the variable, we also hinted at the type of data that variable might contain:

var answer  = ""; // okay, so eventually answer should contain a string
var score = 0; // okay, so we don't have a value yet, but it should be a number
var isWinning = false; // okay, so this will be a true/false boolean value.

We don’t need to, but we might do that as a way of remembering what type of data answer should eventually hold.

Additionally, if we are going to monkey with answer later in other ways, defining it as a string could be handy.

let answer = "";

// and later...
answer += " some text. ";

// and even later...
answer += "You are right!";

By defining that as a string, we can perform string functions on it as we need. But this is all explanation that will come with time. When first learning about variable declaration and assignment, this can be overwhelming.

2 Likes

Thank you for the detail! I really wish this curriculum would explain things like this sometimes…in plain English! You da bomb!

It has been our experience that writing massive walls of text by default tends to lead to lower understanding on the average. Those long explanations are available if you ask questions here on the forum.