AI Reading
Quick summary of this article
TypeScript uses three keywords to declare variables: var, let, and const. Each has different rules about where the variable can be accessed and whether its value can change.
- Variables declared with var are accessible globally if declared outside a function, or locally if inside a function. They also work at the block level.
- Variables declared with let are similar to var but are limited to the block where they are declared. They are not available outside that block.
- Variables declared with const hold a constant value that cannot be changed anywhere in the application. Trying to change it causes a compiler error.
- Use const when you need a value that should stay the same throughout the program, often for global use.
- Use let when you need a variable that only exists within a specific block of code, like inside a loop or condition.
5.3 Difference Between var, let and const in TypeScript
What is difference between Var , Let and Const in TypeScript/JavaScript
1. Use or ‘var’ : TypeScript ‘var’ keyword is used to declare variable in Typescript. 1. If the variable is declared outside of function then it is considered global variable. 2. And if the variable is declared inside the function then its called local variable. 3. Block level accessibility is possible with ‘var’ keyword 2. Use of ‘’let : in TypeScript let keyword is used to declare variable like ‘var’ keyword. Using let keyword, you can declare variable as well as initialize it. 1. Let keyword is like var keyword, but it has limitation. Its declared at block level. 2. So, if a variable is declared inside a function using ‘let’ keyword then the variable will not be available inside the block. 3. Use of const : const keyword is used to declare a constant in typescript whose value cannot be changed throughout the application. 1. Const is normally used for global accessibility. 2. If you try to change value of const, the compiler will throw error.
