Pizza910
Pizza910

Reputation: 11

why am I getting undefined instead of null

Hey guys today is my first day of learning to code, so I'm sure this is a super dumb question. I'm following along freecodecamp beginner javascript video https://www.youtube.com/watch?v=PkZNo7MFNFg&t=678 and at 11:20, when after writing console.log(a) on line 3 and running the script he gets null then 7 but i get undefined then 7.

Anybody know why?

enter image description here

Upvotes: 1

Views: 445

Answers (3)

CherryDT
CherryDT

Reputation: 29011

The tutorial video is wrong. I don't know why it shows null for the tutor, it shouldn't.

Uninitialized variables are undefined in JavaScript, not null.

var a;
console.log(a); // prints undefined

It would only be null if explictly set to null:

var a = null;
console.log(a); // prints null

Upvotes: 1

Adrian
Adrian

Reputation: 8597

Javascript will never assign null, in order to get null you'd actually need to do it yourself from code.

undefined is the default value for a variable that has been declared but not assigned a value to.

null is an assignment value, you can assign it to any variable explicitly making it "empty".

Upvotes: 0

kmoser
kmoser

Reputation: 9273

var a; declares the variable a but does not assign it value. Its value is undefined, which is why you see "undefined" in the console.

Once you assign it a value (e.g. a = 7;) then it has a value.

Upvotes: 0

Related Questions