This lecture introduces variables in TypeScript, explaining their fundamental role as named storage for values and detailing how to declare and use them with let and const keywords, emphasizing TypeScript's strong typing and variable naming conventions.
Mind Map
Click to expand
Click to explore the full interactive mind map • Zoom, pan, and navigate
in this lecture we are going to learn
what is a variable and how to create a
variable in
typescript variables are the fundamental
concept of any programming language a
variable is like a name storage in which
we can store a value this named storage
then can be used over and over again in
our code instead of writing the value
each time we want to use it so let's go
ahead and let's learn how we can create
a variable in typescript let's go to vs
code so here in vs code currently this
app. TS file is open and in the right
hand side we also have our browser open
so whatever typescript code we will type
here and when we will compile it it will
be converted into JavaScript code and
that JavaScript code will run here okay
so from here I'm going to remove this
console.log statement I'll simply put a
comment here and here we are going to
learn learn about variables in
typescript and this is lecture
three all right now in typescript we can
create a variable using let or const
keyword so just like JavaScript in
typescript also we create a variable
using let and cons keyword let's first
create a variable using let keyword and
let's simply call it as num okay and I'm
going to assign this num variable with
this value 100 so this is how we create
a variable using let keyword in the same
way we can also create a variable using
const keyword let me call it as STL and
to this I'm going to assign a string
variable and I'll simply call it as hello
hello
world okay so this is how we create a
variable using const keyword just like
how we do it in JavaScript now what is
the difference between the variable
created using let keyword and the
variable created using const keyword
well when we create a variable using let
keyword at that time we can simply
declare the variable without assigning
any value to it like this and at a
letter point of time if you want we can
assign it with some
value like this so here we are not
getting any
error so when we create a variable using
let keyword we don't need to initialize
it immediately we can initialize it
after a letter point of time but when we
create a variable using const keyword
like we are creating here we need to
initialize it immediately
we cannot create a variable using const
keyword without initializing it so for
example if I try this if I try to create
a variable using const keyword without
initializing it you see we are getting
an error and the error says const
declaration must be initialized that
means whenever we create a variable
using const keyword we must initialize
it immediately like this but that is not
the case in case of variable created
using let keyword all right another
difference is is when we create a
variable using let keyword we can
initialize it with some value and at a
letter point of time if we want we can
change its value so for example here I'm
changing the value of num from 100 to
th000 and we are not getting any error
but this we cannot do with the variables
created using const keyword because the
variable which we create using const
keyword they are constant their value
cannot be changed so here when I have
created this Sr variable and when I have
initialized it with this value at a
letter point of time if I try to change
its value to something else let's say hi
there here we will get an error because
this s Str it is a constant its value
cannot be changed and here we are trying
to change its value so as you can see we
getting an error and it says cannot
assign to SDR because it is a
constant so this is the difference
between let and const
keyword so you can use let keyword to
create a variable when you know that the
value of that variable might change in
future but you can use const keyword to
create a variable when you know that its
value is never going to change in the entire
entire
program another very important point to
note about typescript variables is that
their data type is inferred at the time of
of
initialization for example here we have
created this num variable using let
keyword and and we have assigned a value
100 to it so if I hover over this num
variable you will see that its type is
number so its data type has been
inferred based on the value which we
have stored at it since we are storing a
numeric value to this num its data type
has been inferred as number and its data
type will always be number throughout
the program so at a letter point of time
if we try to assign some other type of
value to this num it is going to throw
us an error so to this num if I try to
assign a Boolean value let's say true
you will see that we have an error and
the error says that Boolean is not
assignable to type number because here
even though we have not explicitly
specified the data type of this num
variable but since we have assigned 100
to it it is a number and that's why it
has inferred its type as number so we
can only assign a numeric value to it
anytime in the
future but this is not how it works in
JavaScript in JavaScript when you create
a variable you assign it with the value
its data type will be determined based
on the value which you have stored in it
but at a letter point of time you can
change the value of that variable to any
other type and then the data type of
that variable will change based on the
value which you have stored in it so
JavaScript is dynamically typed but here
typescript is strongly typed once the
data type of the variable is determined
it data type cannot be changed okay
that's why when we assigned 100 to this
num variable its data type was
determined as number so only number can
be stored in this num variable
throughout the program no other value
type can be stored in this num variable
and that's why when we are trying to
store a Boolean value to this num it is
giving us an error so this is another
important point to remember about typescript
typescript
variables now let's also learn what are
the rules we need to follow in order to
name a variable so when we are creating
a variable when we are going to provide
a name for the variable we need to keep
few things in mind the first thing which
we need to keep in mind is that the
variable name can only contain letters
digits underscore and dollar sign it
cannot contain any other character for
example here I can create a variable
let's say first name first uncore name
so this variable name is correct because
it contains letters and an underscore so
underscore and letters are allowed in a
variable name even you can include
dollar and digits so letters digits
dollar and underscore these things are
allowed in a variable name but if you
try to use any other character for
example if I try to use hyphen hyphen is
not allowed in variable name so this is
not a valid variable name okay in the
same way if I try to use use
percentage that is also not allowed in a
variable name so this is also not a
valid variable name only letters digits
underscore and dollar is allowed in a
variable name this is the first rule the
second rule is the first character of a
variable name must not be a digit a
variable name cannot start with a number
for example if I say one name this is
not a valid variable name because a
variable name cannot start with a number
this is the second rule which you need
to remember third rule is a variable
name can start with a letter under sco
or dollar sign it cannot start with a
number but a variable name can start
with a dollar sign or it can also start
with a underscore or it can start with
any letter so this is also a valid variable
variable
name and this is also a valid variable
name it starts with a character a
letter then the fourth rule is
typescript variable names are case
sensitive so for example this variable
num and if I create another variable num
with n in uppercase these two are
completely different variables these are
not same variables these are different
variables because variable names are
case sensitive so a variable name with n
in lower case is different from a
variable name num where n is in uppercase
uppercase
and finally you cannot use typescript
reserved keywords as a variable name for
example I cannot create a variable class
because class it is a reserved
keyword okay class is a reserved keyword
so we cannot use this name as a variable
name because it is a reserved keyword in
the same way if is also a reserved
keyword so I cannot use if as a variable
name then while is also a reserved
keyword so we cannot use while as a
variable name or we cannot use any
reserved keywords in typescript as a variable
variable
name so these are the five rules which
you need to remember while naming your
variable so variable name can contain
only letters digits underscore and
dollar sign the first character of a
variable name must not be a digit a
variable name can start with a letter
underscore or dollar sign but it cannot
start with a digit it cannot start with
a number and typescript variable names
are case sensitive so a variable name
num with all characters in uppercase is
different from a variable name num with
all characters in lower case and finally
we cannot use typescripts reserved
keywords as a variable
name so these are the five rules you
need to remember while naming your
variable so so in this lecture we
learned that we can create a variable
using let and const keyword and when we
create a variable and when we assign it
with some value that variable is going
to have a type so in the next lecture
and in coming few lectures we are going
to talk about the different data types
we have in
typescript this is all from this lecture
if you have any questions then feel free
to ask it thank you for listening and
have a great day
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.