Question about initialized and uninitialized variables in JavaScript

Does initialized mean if variable is a number and uninitialized if variable isnt a number but a string instead?

No, initialized means a variable was declared (i.e. using var, let, or const) and was assigned a value. The value could be of any type (number, string, object, boolean, etc). If you just use the var, let or const in front, the variable is declared, but not initialized.

For example, the following variable is declared but not initialized:

var declaredButNotInitialized;

In the following example, the variable is initialized with a string:

var initializedVariable = 'JavaScript is fun!';

So uninitalized means if variable has no value basicaly? And also undefinited variable is the one without number but with a string instead?

Even if you just declare a value and do not assign it a value, JavaScript will still give it a value of undefined (behind-the-scenes). An uninitialized variable is just a variable that has been declared, but has not been assigned a value when it was declared.

No, an undefined variable is a non-existent variable. It has not even been declared.

Below, I declare a variable, but try to reference a variable that does not exist.

var myDeclaredVariable;
console.log(someOtherVariable);  // results in ReferenceError: someOtherVariable is not defined

This seems somewhat confusing to me, im gonna use some youtube tutorials for beginners in javascript. Thank you for help regardless, i will come back after i get more into this, since html and css were quiet easy to learn, same goes for jquey and bootstrap, which are all basically designs, its time to move to web sites functions and this will take me way more time to comprehend.

What part is still confusing? I will do my best to help you to understand.

Im still confused about undefined variable, it seems easy but hard to grasp what it would exactly mean. Does it mean for example if var has no name given? Thats one thing im thinking considering the word undefined, since we define variable with some name.

An undefined variable does not exist at all. You have not declared it (or initialized it).

If I use the word var in front of a variable name, then the variable is declared (see below).

var myVariable;  // this variable is declared and is "defined"

Even if I do not put the word var in front of a variable name, but I do assign it a value, then I have initialized the variable. See below for example.

anotherVariable = 50;

You may be like "Wait, you did not use the var keyword, so how is it declared? Well, JavaScript assumes you meant to declare it, so it is an assumed var in front. In the above example, anotherVariable is “defined”.

The confusion may be in the use of “not defined” and undefined. Remember in a previous post, I said that if you just declare a variable (i.e. var myVariable;), then JavaScript automatically assigns a value of undefined. When I say assigned a value of undefined, I do not mean a string ‘undefined’, I actually mean an actual value of undefined. undefined is one of JavaScript’s primitive values. A few other primitive types are Number, String, and Boolean.

So let me clarify something I said in the previous reply. In response to one of your questions, I said “No, an undefined variable is a non-existent variable. It has not even been declared.” I should have said a variable which has not been defined (declared or initialized) is a non-existent variable.

I can not reference a variable which has not been defined. So, if the following is my only code:


This above will result the following Reference error:

ReferenceError: myVariable is not defined

If instead I would have had the code below:

var myVariable;

I would have seen undefined displayed in the console. Why? Because I declared myVariable, so it is defined and behind the scenes, JavaScript assigned the value undefined to myVariable.

I always viewed the initial value of a variable declaration of var to be undefined. Any user defined value happened to be an assignment. I suppose you can see it in terms of implicit or explicit initialization; I’m not sure how much weight my opinion is shared within the JavaScript community though.

This “undefined” JS is actually quite simple to learn & initiate once you read the problem well.

Just some feedback for FCC. I think that many of the lessons so far have way too much information. Many times the examples are nothing like the final answers. I am sure by design. But it does hinder progress.

What happens if I try to do an operation on an uninitialized variable?

What kind of operation?

Say, concatenate it with a string, increment it, or use it as a counter in a loop

it will start with undefined, so you may find that it results in NaN or a string with at the beginning the word “undefined”. try, you will see…

1 Like