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.