Typescript Variables. Variable declaration and Type annotation
Variable are keyword which is used to store a value in typescript or hold result value of an operation.
Var type is introduced first for variable installation
In Es6 updation of var is came that is let and const
Which variable type should i pick
If you need to have constant value, then you should choose const, which can’t be altered once its assigned.
For the rest of purpose you can use let or var. I prefer let
Variable Declaration
let [var_name] : [type_annotation or type] = value as same as type;
let value: number = 27Main thing what type annotation is entered, same type value should be placed on variable.(if number then 033, if string then “coder 033”).
Get Narayanan’s stories in your inbox
Join Medium for free to get updates from this writer.
type annotation is nothing but, you need to mention the type of value that place in that variable like string, boolean, number, any …
let admin: string;
admin = "name"
// It will pop-up error because i try to assign number to string
// admin = 15Rules for creating variable
- Start with letter or underscore or dollar.
let age: string;
let _age: string;
let $age: string;wrong formats
let 1age: string;
let !age: string;
let @age: string;You can use number but not in beginning
let age1: string;If variable name is bigger then use camel case or underscore for readablity purposes.
let ageOfPerson: string;
let age_of_person: string;Types of assigning variable
// Declare and assign same time
let username : string = "name"
// Declare first assign later
let username1 : string
username1= "name"
// Declare without type annotation
let username2 = "name"
//If you run this file the output will be shown as string string string
console.log(typeof(username),typeof(username1),typeof(username2));See the full course on Skillshare, click the link below, and get 1 month free subscription.
https://www.skillshare.com/en/r/profile/Coder033/26410885?gr_tch_ref=on&gr_trp=on
Follow for more interesting topics :-)
Follow us on
Instagram:- https://www.instagram.com/Coder_033/
Youtube:= https://www.youtube.com/@coder033
Github:- https://github.com/Coder033
Skillshare https://www.skillshare.com/profile/Coder033/26410885









