Understanding null and undefined

Modern JavaScript Learn JavaScript Fundamentals
3 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€85.49
List Price:  €119.69
You save:  €34.19
£73.94
List Price:  £103.53
You save:  £29.58
CA$136.98
List Price:  CA$191.77
You save:  CA$54.79
A$151.99
List Price:  A$212.79
You save:  A$60.80
S$127.85
List Price:  S$179
You save:  S$51.14
HK$784.80
List Price:  HK$1,098.75
You save:  HK$313.95
CHF 79.48
List Price:  CHF 111.28
You save:  CHF 31.79
NOK kr1,012.69
List Price:  NOK kr1,417.81
You save:  NOK kr405.12
DKK kr637.93
List Price:  DKK kr893.13
You save:  DKK kr255.20
NZ$165.94
List Price:  NZ$232.32
You save:  NZ$66.38
د.إ367.21
List Price:  د.إ514.11
You save:  د.إ146.90
৳12,155.75
List Price:  ৳17,018.53
You save:  ৳4,862.78
₹8,582.32
List Price:  ₹12,015.60
You save:  ₹3,433.27
RM425.20
List Price:  RM595.30
You save:  RM170.10
₦152,836.71
List Price:  ₦213,977.51
You save:  ₦61,140.80
₨28,434.65
List Price:  ₨39,809.65
You save:  ₨11,375
฿3,244.17
List Price:  ฿4,541.96
You save:  ฿1,297.79
₺4,016.46
List Price:  ₺5,623.20
You save:  ₺1,606.74
B$555.95
List Price:  B$778.35
You save:  B$222.40
R1,792.06
List Price:  R2,508.95
You save:  R716.89
Лв167.31
List Price:  Лв234.24
You save:  Лв66.93
₩137,902.20
List Price:  ₩193,068.60
You save:  ₩55,166.40
₪333.48
List Price:  ₪466.89
You save:  ₪133.40
₱5,648.28
List Price:  ₱7,907.82
You save:  ₱2,259.53
¥14,654.02
List Price:  ¥20,516.22
You save:  ¥5,862.19
MX$1,863.83
List Price:  MX$2,609.44
You save:  MX$745.60
QR364.02
List Price:  QR509.64
You save:  QR145.62
P1,334.67
List Price:  P1,868.59
You save:  P533.92
KSh12,948.70
List Price:  KSh18,128.70
You save:  KSh5,180
E£4,948.92
List Price:  E£6,928.69
You save:  E£1,979.76
ብር13,688.63
List Price:  ብር19,164.63
You save:  ብር5,476
Kz91,690.83
List Price:  Kz128,370.83
You save:  Kz36,680
CLP$95,713.42
List Price:  CLP$134,002.62
You save:  CLP$38,289.20
CN¥716.78
List Price:  CN¥1,003.52
You save:  CN¥286.74
RD$6,029.39
List Price:  RD$8,441.39
You save:  RD$2,412
DA12,974.97
List Price:  DA18,165.48
You save:  DA5,190.50
FJ$224.34
List Price:  FJ$314.09
You save:  FJ$89.74
Q767.92
List Price:  Q1,075.11
You save:  Q307.19
GY$20,914.76
List Price:  GY$29,281.51
You save:  GY$8,366.74
ISK kr12,174.40
List Price:  ISK kr17,044.65
You save:  ISK kr4,870.24
DH900.15
List Price:  DH1,260.25
You save:  DH360.10
L1,687.60
List Price:  L2,362.71
You save:  L675.10
ден5,265.96
List Price:  ден7,372.56
You save:  ден2,106.59
MOP$808.30
List Price:  MOP$1,131.65
You save:  MOP$323.35
N$1,772.82
List Price:  N$2,482.02
You save:  N$709.20
C$3,674.63
List Price:  C$5,144.63
You save:  C$1,470
रु13,711.85
List Price:  रु19,197.15
You save:  रु5,485.29
S/354.61
List Price:  S/496.47
You save:  S/141.86
K412.45
List Price:  K577.45
You save:  K165
SAR375.01
List Price:  SAR525.03
You save:  SAR150.02
ZK2,314.16
List Price:  ZK3,239.92
You save:  ZK925.75
L434.63
List Price:  L608.50
You save:  L173.87
Kč2,109.72
List Price:  Kč2,953.70
You save:  Kč843.97
Ft34,123.42
List Price:  Ft47,774.16
You save:  Ft13,650.73
SEK kr955.94
List Price:  SEK kr1,338.36
You save:  SEK kr382.41
ARS$126,192.13
List Price:  ARS$176,674.04
You save:  ARS$50,481.90
Bs692.26
List Price:  Bs969.19
You save:  Bs276.93
COP$401,297.58
List Price:  COP$561,832.67
You save:  COP$160,535.08
₡50,416.70
List Price:  ₡70,585.39
You save:  ₡20,168.69
L2,634.73
List Price:  L3,688.73
You save:  L1,054
₲774,763.12
List Price:  ₲1,084,699.36
You save:  ₲309,936.24
$U4,064.43
List Price:  $U5,690.36
You save:  $U1,625.93
zł365.74
List Price:  zł512.05
You save:  zł146.31
Already have an account? Log In

Transcript

I mentioned when we were talking about primitives and first introduced Nolan undefined, that we would visit them again. Well, now is the time. Null and undefined. Both mean nothingness. So they're a way in JavaScript indicate that nothing is in a particular variable. But what's the difference between the two?

Well, undefined is a state that is established by the system itself. So if we declare a variable but do not assign a value to it, it is undefined. Where null is the state that we would assign as a programmer if we want it to indicate nothing us. Let's look at some examples. So first, let's take a look at undefined if I create a variable current user Don't set it to a value. When I reference current user, it returns undefined.

Now that is different than a variable that has never been declared before. For example, let's see if we can find out what user contains user is a variable i have not declared yet. Notice we get an error at that time. A reference error basically tells us that user is not defined yet. So JavaScript knows the current user has been defined. But there's no value in it.

Therefore it indicates that undefined is its value. Now, how is no different than that? Well, let's say that we have now given current user value, say this is a web application. The current user is now Steven So when we reference current user, that's what it shows us. But let's say that Stephen has left that the current user is left. And so we need to indicate that there is no current user a state of nothingness.

So to do that, we would set that equal to null. Now when we reference current user, it returns null. So that's what I mean by the difference between the system assigning value as nothingness or undefined, or the programmer or the program itself defining the value as null. And that's a great way to keep the difference between the two, undefined and null. It's important to understand that difference as there are situations where in your programming code you need to check to see if there is a value In a variable and if that value is undefined you know it has not been established yet and if is set to null then you know that you have you have set it to nothing as it was something previously but now the value is no longer there. Alright, let's move on to the next topic.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.