- Typescript is the superset of Javascript
- It compiles into regular Javascript, so it runs anywhere Javascript does
- File ends with .ts
- You write code in Typescript
- Typescript compiler convers it into Javascript
- Javascript runs in browser, Node.js or any JS environment
- Helps catch mistakes early
- Makes large projects easier to manage
- Type Saftey
- Node.js
- Typescript Complier
- Run it on root as admin
npm install -g typescript
to install tsc fileName.ts
> Compiles and creates the JS filenode fileName.js
> Run the JS file
- Run it on root as admin
- Any IDE
- Typescript executor
- Run it on root as admin
npm install -g tsx
to install - tsx
fileName.ts
> Runs the ts file without creating the js file
- Run it on root as admin
- Signle Line (//sample code) > cmd + /
- Multi Line (/_ sample story _/) > shift+alt(option)+a
- Container which holds the Value/data
- var, let and const
- syntax > ex:
let age:number = 30
- Scope
- Functional
- Block
Keyword | Scope | Redeclaration | Reassignment | Hoisting | Usage Recommendation |
---|---|---|---|---|---|
var | Function | Yes | Yes | Yes | Avoid in modern code |
let | Block | No | Yes | No | Use if value changes |
const | Block | No | No | No | Use by default |
- Javascript is the Dynamically typed language
- Typescript is the Statically typed language
- Premitive Datatypes
- Can store only one single value a. Number b. String c. Boolean d. Null e. undefined f. Any g. Union h. Void
- Non-Premitive Datatypes
- Can store group of Objects a. Array b. Class c. Function d. Interface, etc..
let age:number = 32
- Type/Data Type
- Annotations
- Type Inference
- Arithmetic
- Assignment
- Relational
- Logical
- Increment/Decrement
- Conditional(Ternary)
- Conditional Statements
- if
- if...else
- if...else if...else
- switch
- While
console.log("1 to 10");
let count = 1;
while (count < 11) {
console.log("Count is: " + count);
count++;
}
- Do-while
console.log("5 to 1");
let j = 5;
do {
console.log(j);
j--;
} while (j >= 1);
- For Loop
console.log("1 to 10");
for (let i = 1; i <= 10; i++) {
console.log(i);
}
- Break and Continue
console.log("1 to 10 but skip 5");
for (let i = 1; i <= 10; i++) {
if (i === 5) {
continue;
}
console.log(i);
}
console.log("1 to 10 but stop at 5");
for (let i = 1; i <= 10; i++) {
if (i === 5) {
break;
}
console.log(i);
}
- Named Function
- Arrow/Lambda function
- Default Parameters
- Optional Parameters
- Rest Parameter
function addGivenNumbers(...num: number[]): void {
let i = 0;
let sum: number = 0;
for (let i = 0; i < num.length; i++) {
sum = sum + num[i];
}
console.log(sum);
}
addGivenNumbers(1, 3, 43, 24, 56, 7, 8, 76, 2);