Difference Between Constants and Variables

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
$39.99
List Price:  $79.99
You save:  $40
€34.36
List Price:  €68.73
You save:  €34.37
£29.84
List Price:  £59.69
You save:  £29.85
CA$55.08
List Price:  CA$110.18
You save:  CA$55.10
A$61.41
List Price:  A$122.85
You save:  A$61.43
S$51.52
List Price:  S$103.05
You save:  S$51.53
HK$312.13
List Price:  HK$624.35
You save:  HK$312.21
CHF 32.19
List Price:  CHF 64.39
You save:  CHF 32.20
NOK kr400.12
List Price:  NOK kr800.35
You save:  NOK kr400.22
DKK kr256.45
List Price:  DKK kr512.97
You save:  DKK kr256.51
NZ$68.27
List Price:  NZ$136.57
You save:  NZ$68.29
د.إ146.86
List Price:  د.إ293.76
You save:  د.إ146.90
৳4,869.27
List Price:  ৳9,739.76
You save:  ৳4,870.49
₹3,524.71
List Price:  ₹7,050.30
You save:  ₹3,525.59
RM169.27
List Price:  RM338.59
You save:  RM169.32
₦61,275.07
List Price:  ₦122,565.47
You save:  ₦61,290.40
₨11,351.57
List Price:  ₨22,705.99
You save:  ₨11,354.41
฿1,295.43
List Price:  ฿2,591.19
You save:  ฿1,295.76
₺1,645.70
List Price:  ₺3,291.82
You save:  ₺1,646.11
B$217.74
List Price:  B$435.55
You save:  B$217.80
R708.73
List Price:  R1,417.64
You save:  R708.90
Лв67
List Price:  Лв134.02
You save:  Лв67.01
₩55,770.52
List Price:  ₩111,554.98
You save:  ₩55,784.46
₪135.60
List Price:  ₪271.25
You save:  ₪135.64
₱2,298.32
List Price:  ₱4,597.22
You save:  ₱2,298.90
¥5,941.06
List Price:  ¥11,883.61
You save:  ¥5,942.55
MX$750.26
List Price:  MX$1,500.70
You save:  MX$750.44
QR146.03
List Price:  QR292.09
You save:  QR146.06
P574.53
List Price:  P1,149.20
You save:  P574.67
KSh5,166.70
List Price:  KSh10,334.70
You save:  KSh5,168
E£1,939.49
List Price:  E£3,879.47
You save:  E£1,939.98
ብር5,681.79
List Price:  ብር11,365.01
You save:  ብር5,683.21
Kz36,469.08
List Price:  Kz72,947.28
You save:  Kz36,478.20
CLP$38,717.51
List Price:  CLP$77,444.71
You save:  CLP$38,727.20
CN¥285.16
List Price:  CN¥570.39
You save:  CN¥285.23
RD$2,516.45
List Price:  RD$5,033.54
You save:  RD$2,517.08
DA5,193.10
List Price:  DA10,387.51
You save:  DA5,194.40
FJ$90.45
List Price:  FJ$180.93
You save:  FJ$90.48
Q306.92
List Price:  Q613.92
You save:  Q307
GY$8,374.04
List Price:  GY$16,750.18
You save:  GY$8,376.14
ISK kr4,933.96
List Price:  ISK kr9,869.16
You save:  ISK kr4,935.20
DH359.99
List Price:  DH720.08
You save:  DH360.08
L664.67
List Price:  L1,329.51
You save:  L664.84
ден2,102.60
List Price:  ден4,205.72
You save:  ден2,103.12
MOP$321.59
List Price:  MOP$643.27
You save:  MOP$321.67
N$703.83
List Price:  N$1,407.84
You save:  N$704.01
C$1,473.65
List Price:  C$2,947.67
You save:  C$1,474.01
रु5,648.69
List Price:  रु11,298.79
You save:  रु5,650.10
S/141.43
List Price:  S/282.89
You save:  S/141.46
K169.54
List Price:  K339.12
You save:  K169.58
SAR150.05
List Price:  SAR300.14
You save:  SAR150.09
ZK946.07
List Price:  ZK1,892.38
You save:  ZK946.31
L174.54
List Price:  L349.12
You save:  L174.58
Kč841.12
List Price:  Kč1,682.46
You save:  Kč841.33
Ft13,613.08
List Price:  Ft27,229.58
You save:  Ft13,616.49
SEK kr378.09
List Price:  SEK kr756.29
You save:  SEK kr378.19
ARS$55,055.52
List Price:  ARS$110,124.81
You save:  ARS$55,069.29
Bs276.68
List Price:  Bs553.44
You save:  Bs276.75
COP$161,780.26
List Price:  COP$323,600.99
You save:  COP$161,820.72
₡20,244.43
List Price:  ₡40,493.93
You save:  ₡20,249.49
L1,048.40
List Price:  L2,097.07
You save:  L1,048.66
₲289,232.23
List Price:  ₲578,536.79
You save:  ₲289,304.56
$U1,600.17
List Price:  $U3,200.74
You save:  $U1,600.57
zł146.62
List Price:  zł293.27
You save:  zł146.65
Already have an account? Log In

Transcript

Welcome back. In this video tutorial, I am going to show you the difference between constants and variables in JavaScript. First of all, I am going to create a file called example, dot JavaScript. So I'm going to create example, dot j s, and I'm going to save this file on my desktop. Click Save, I'm going to use, I'm going to create a constant and to create a constant in JavaScript is you type const. And this is how we create a constant and then the name of the constants.

So for example, name constantly equals to john, john and then the semicolon. So this is a constant, and a variable in JavaScript can be var can be var name for example, name equals two. Susan, or it can also be a lead. But here I'm going to change this from name to name one so that we have no conflict. And this name to name two equals to Bob. So now we have three now we have three lines of code, the first one is a constant and the other two are variables.

So the difference between constants and variables in JavaScript is that constants cannot be changed later, but variables can be changed in runtime. So for example, now if I try to change the, if I try to change the constant name, I will not be able to So for example, if I change name to first of all, let's just let's just print all of these in the console. So console dot log, and then I'm going to print out all of these Name. And then I'm going to also print I'm going to do the same for, for name one and name two. gonna pick copy and paste and change this to one and two, two. And let's save and run.

And as you can see, after we are on we have john, Susan and Bob. But if I try to change the constant in the runtime, so for example, name equals instead of john, something like Matthew, Matthew with M capital, and if I save and run and if I save and run, as you can see, I get an error because I cannot change it later as you can see assignment to constant variable, you cannot change a constant once you have initialize it. So once you have a constant it cannot be changed. But if for example, I tried to change name one, two from, from Susan to Matthew, Matthew and try to change name to from Bob to to Alice and save and run. As you can see, john is first was first printed out and then Matthew and Alice the name one variable has been changed the the name to variable also has been changed.

So, keep in mind that if you are going to change a variable later you should specify it as a variable or a let do not make it a constant if you want to change it later. So constant always is always used for things that are not are never going to be changed later. Once we run our program

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.