Naming conventions in typeScript
A developer must follow a few naming conventions to declare the typescript variables. They are:
- The variable must contain an alphabet or digits.
- The variable cannot start with digits.
- The variable cannot contain special characters other than underscore(_) and dollar($).
Types of variables
The variables in typescript can also be declared using let and const according to their scope and necessity in code.
let:
The keyword let is similar to var. The let keyword has scope restrictions in typescript. The variables declared with let can only be used within their block scope and cannot be redeclared within the same block. The variables with the same name can be declared in different blocks.
let age: number = 20;
Redeclaring any let variable will display an error like:
Compiler Error: Cannot redeclared block-scoped variable ‘age’.
const:
The keyword const prevents the compiler from re-assigning any value to the variable.
const name = “coding ninjas”;
Re-assigning a value to a const variable will display an error like:
Compiler Error: Cannot assign to 'name' because it is a constant or read-only property
Variable Scope
The variables have a scope in typescript. The scope is a block or area where the variable declared will be identified and used. The availability of a variable can be determined by its scope. The types of scopes are:
Global Scope
The variables with the global scope are declared outside the programming constructs and can be accessed anywhere from the code.
Class Scope
The variables with the class scope are also called fields. They are declared inside a class nut outside the methods and blocks. They can be used anywhere within the class.
Local Scope
The variables with local scope belong to the methods or blocks. They can be used and accessed only inside the block they are declared.
var num = 20; // global variable
class Example {
var value = 10; // class or instance variable
console.log(“value in class: ” +value);
function() {
let value = 5; // local variable
console.log(“value in function: ” +value);
}
}
Output
value in class: 10
value in function: 5
In the above code, the function considers the local values first and prints them, and similarly, the class considers the class variable. If no local variable is available, it checks outside the block for a value.
FAQs
-
How do I declare variables in TypeScript?
The variables can be declared in typescript using var keyword before the variable name and a type annotation (: type) after the variable (type annotation is not mandatory).
-
What is the difference between var and let in TypeScript?
The keyword var is function scoped, whereas the keyword let is block scoped. The variable declared with var has more scope than let.
-
What are variable scopes available in TypeScript?
The scopes available in typescript are; global, class, and local.
Key Takeaways
Let’s look at what we discussed in this article, in brief,
- A variable is a storage location of any value to be used or referenced by programs. It stores the values that we need throughout the program and allows us to use them in any context when necessary.
- The variables are declared before we use them in typescript. The variables are declared using the var keyword before the variable name and a type annotation (: type) after the variable.
- Every variable must be declared by following the naming conventions in typescript.
- The variables in typescript can also be declared using var, let, and const according to their scope and necessity in code.
-
The variables have a scope in typescript. The availability of a variable can be determined by its scope. The scopes available in typescript are; global, class, and local.
Recommended Readings:
Hey ninjas! If you like typescript and want to explore it, we have the best web development course for you. Check it out and share things you learned on social media by tagging coding ninjas.
Happy Learning!