0:03 in this lecture we are going to learn
0:06 what is a variable and how to create a
0:07 variable in
0:10 typescript variables are the fundamental
0:12 concept of any programming language a
0:15 variable is like a name storage in which
0:17 we can store a value this named storage
0:20 then can be used over and over again in
0:22 our code instead of writing the value
0:25 each time we want to use it so let's go
0:28 ahead and let's learn how we can create
0:31 a variable in typescript let's go to vs
0:35 code so here in vs code currently this
0:39 app. TS file is open and in the right
0:41 hand side we also have our browser open
0:43 so whatever typescript code we will type
0:45 here and when we will compile it it will
0:48 be converted into JavaScript code and
0:51 that JavaScript code will run here okay
0:53 so from here I'm going to remove this
0:56 console.log statement I'll simply put a
0:59 comment here and here we are going to
1:02 learn learn about variables in
1:05 typescript and this is lecture
1:10 three all right now in typescript we can
1:12 create a variable using let or const
1:15 keyword so just like JavaScript in
1:16 typescript also we create a variable
1:19 using let and cons keyword let's first
1:21 create a variable using let keyword and
1:24 let's simply call it as num okay and I'm
1:25 going to assign this num variable with
1:29 this value 100 so this is how we create
1:32 a variable using let keyword in the same
1:33 way we can also create a variable using
1:36 const keyword let me call it as STL and
1:37 to this I'm going to assign a string
1:40 variable and I'll simply call it as hello
1:42 hello
1:44 world okay so this is how we create a
1:47 variable using const keyword just like
1:49 how we do it in JavaScript now what is
1:51 the difference between the variable
1:53 created using let keyword and the
1:55 variable created using const keyword
1:57 well when we create a variable using let
2:00 keyword at that time we can simply
2:02 declare the variable without assigning
2:05 any value to it like this and at a
2:07 letter point of time if you want we can
2:08 assign it with some
2:11 value like this so here we are not
2:12 getting any
2:15 error so when we create a variable using
2:18 let keyword we don't need to initialize
2:20 it immediately we can initialize it
2:24 after a letter point of time but when we
2:26 create a variable using const keyword
2:28 like we are creating here we need to
2:30 initialize it immediately
2:32 we cannot create a variable using const
2:35 keyword without initializing it so for
2:38 example if I try this if I try to create
2:39 a variable using const keyword without
2:41 initializing it you see we are getting
2:44 an error and the error says const
2:46 declaration must be initialized that
2:48 means whenever we create a variable
2:50 using const keyword we must initialize
2:54 it immediately like this but that is not
2:55 the case in case of variable created
2:59 using let keyword all right another
3:01 difference is is when we create a
3:03 variable using let keyword we can
3:05 initialize it with some value and at a
3:08 letter point of time if we want we can
3:10 change its value so for example here I'm
3:13 changing the value of num from 100 to
3:16 th000 and we are not getting any error
3:19 but this we cannot do with the variables
3:21 created using const keyword because the
3:22 variable which we create using const
3:24 keyword they are constant their value
3:27 cannot be changed so here when I have
3:29 created this Sr variable and when I have
3:31 initialized it with this value at a
3:33 letter point of time if I try to change
3:36 its value to something else let's say hi
3:40 there here we will get an error because
3:43 this s Str it is a constant its value
3:45 cannot be changed and here we are trying
3:46 to change its value so as you can see we
3:49 getting an error and it says cannot
3:52 assign to SDR because it is a
3:54 constant so this is the difference
3:56 between let and const
3:59 keyword so you can use let keyword to
4:01 create a variable when you know that the
4:04 value of that variable might change in
4:08 future but you can use const keyword to
4:10 create a variable when you know that its
4:13 value is never going to change in the entire
4:14 entire
4:17 program another very important point to
4:20 note about typescript variables is that
4:22 their data type is inferred at the time of
4:23 of
4:27 initialization for example here we have
4:28 created this num variable using let
4:30 keyword and and we have assigned a value
4:33 100 to it so if I hover over this num
4:36 variable you will see that its type is
4:38 number so its data type has been
4:40 inferred based on the value which we
4:43 have stored at it since we are storing a
4:45 numeric value to this num its data type
4:49 has been inferred as number and its data
4:51 type will always be number throughout
4:54 the program so at a letter point of time
4:56 if we try to assign some other type of
4:58 value to this num it is going to throw
5:01 us an error so to this num if I try to
5:04 assign a Boolean value let's say true
5:05 you will see that we have an error and
5:09 the error says that Boolean is not
5:12 assignable to type number because here
5:14 even though we have not explicitly
5:15 specified the data type of this num
5:17 variable but since we have assigned 100
5:20 to it it is a number and that's why it
5:23 has inferred its type as number so we
5:26 can only assign a numeric value to it
5:27 anytime in the
5:30 future but this is not how it works in
5:33 JavaScript in JavaScript when you create
5:35 a variable you assign it with the value
5:37 its data type will be determined based
5:39 on the value which you have stored in it
5:41 but at a letter point of time you can
5:44 change the value of that variable to any
5:46 other type and then the data type of
5:48 that variable will change based on the
5:50 value which you have stored in it so
5:53 JavaScript is dynamically typed but here
5:56 typescript is strongly typed once the
5:59 data type of the variable is determined
6:02 it data type cannot be changed okay
6:05 that's why when we assigned 100 to this
6:06 num variable its data type was
6:09 determined as number so only number can
6:10 be stored in this num variable
6:13 throughout the program no other value
6:15 type can be stored in this num variable
6:16 and that's why when we are trying to
6:18 store a Boolean value to this num it is
6:21 giving us an error so this is another
6:23 important point to remember about typescript
6:25 typescript
6:28 variables now let's also learn what are
6:31 the rules we need to follow in order to
6:34 name a variable so when we are creating
6:36 a variable when we are going to provide
6:38 a name for the variable we need to keep
6:40 few things in mind the first thing which
6:43 we need to keep in mind is that the
6:45 variable name can only contain letters
6:48 digits underscore and dollar sign it
6:51 cannot contain any other character for
6:53 example here I can create a variable
6:57 let's say first name first uncore name
6:59 so this variable name is correct because
7:02 it contains letters and an underscore so
7:04 underscore and letters are allowed in a
7:06 variable name even you can include
7:11 dollar and digits so letters digits
7:15 dollar and underscore these things are
7:17 allowed in a variable name but if you
7:19 try to use any other character for
7:23 example if I try to use hyphen hyphen is
7:25 not allowed in variable name so this is
7:28 not a valid variable name okay in the
7:30 same way if I try to use use
7:32 percentage that is also not allowed in a
7:35 variable name so this is also not a
7:38 valid variable name only letters digits
7:40 underscore and dollar is allowed in a
7:43 variable name this is the first rule the
7:46 second rule is the first character of a
7:49 variable name must not be a digit a
7:52 variable name cannot start with a number
7:55 for example if I say one name this is
7:57 not a valid variable name because a
8:00 variable name cannot start with a number
8:01 this is the second rule which you need
8:04 to remember third rule is a variable
8:07 name can start with a letter under sco
8:09 or dollar sign it cannot start with a
8:12 number but a variable name can start
8:15 with a dollar sign or it can also start
8:18 with a underscore or it can start with
8:21 any letter so this is also a valid variable
8:22 variable
8:24 name and this is also a valid variable
8:28 name it starts with a character a
8:31 letter then the fourth rule is
8:33 typescript variable names are case
8:36 sensitive so for example this variable
8:39 num and if I create another variable num
8:43 with n in uppercase these two are
8:45 completely different variables these are
8:47 not same variables these are different
8:50 variables because variable names are
8:53 case sensitive so a variable name with n
8:55 in lower case is different from a
8:58 variable name num where n is in uppercase
9:00 uppercase
9:03 and finally you cannot use typescript
9:06 reserved keywords as a variable name for
9:10 example I cannot create a variable class
9:13 because class it is a reserved
9:17 keyword okay class is a reserved keyword
9:21 so we cannot use this name as a variable
9:23 name because it is a reserved keyword in
9:25 the same way if is also a reserved
9:28 keyword so I cannot use if as a variable
9:31 name then while is also a reserved
9:34 keyword so we cannot use while as a
9:37 variable name or we cannot use any
9:39 reserved keywords in typescript as a variable
9:41 variable
9:43 name so these are the five rules which
9:47 you need to remember while naming your
9:50 variable so variable name can contain
9:52 only letters digits underscore and
9:54 dollar sign the first character of a
9:57 variable name must not be a digit a
9:59 variable name can start with a letter
10:01 underscore or dollar sign but it cannot
10:03 start with a digit it cannot start with
10:05 a number and typescript variable names
10:08 are case sensitive so a variable name
10:11 num with all characters in uppercase is
10:13 different from a variable name num with
10:17 all characters in lower case and finally
10:20 we cannot use typescripts reserved
10:22 keywords as a variable
10:24 name so these are the five rules you
10:27 need to remember while naming your
10:29 variable so so in this lecture we
10:31 learned that we can create a variable
10:35 using let and const keyword and when we
10:36 create a variable and when we assign it
10:39 with some value that variable is going
10:43 to have a type so in the next lecture
10:46 and in coming few lectures we are going
10:48 to talk about the different data types
10:49 we have in
10:51 typescript this is all from this lecture
10:53 if you have any questions then feel free
10:55 to ask it thank you for listening and
10:58 have a great day