-
Arrays:
const arr: number[] = [1, 2, 3]; arr.push(4); // Adds 4 at the end arr.pop(); // Removes last element
-
Objects:
const obj = { name: 'Alice', age: 25 }; console.log(obj.name); // 'Alice'
-
Maps:
const map = new Map(); map.set('key', 'value'); console.log(map.get('key')); // 'value'
-
Sets:
const set = new Set([1, 2, 3]); set.add(4); // {1, 2, 3, 4}
- Length:
str.length - Slice:
str.slice(0, 5) - Substring:
str.substring(1, 3) - Split:
str.split(' ') - Includes:
str.includes('word') - Replace:
str.replace('old', 'new')
- map(): Transform each item.
const result = arr.map(x => x * 2);
- filter(): Filter elements.
const even = arr.filter(x => x % 2 === 0);
- reduce(): Aggregate data.
const sum = arr.reduce((acc, val) => acc + val, 0);
- find(): Find an element.
const item = arr.find(x => x > 2);
-
For Loop:
for (let i = 0; i < 5; i++) { console.log(i); }
-
ForEach (Arrays):
arr.forEach(x => console.log(x));
-
For...of:
for (const val of arr) { console.log(val); }
-
Union Types:
let value: string | number; value = 'Hello'; // valid value = 10; // valid
-
Tuple:
let tuple: [string, number] = ['Alice', 25];
-
Enum:
enum Direction { Up, Down, Left, Right }
-
Promises:
const fetchData = new Promise((resolve, reject) => { // Some async task resolve('Data'); }); fetchData.then(data => console.log(data));
-
Async/Await:
async function getData() { const result = await fetchData(); console.log(result); }
- Try/Catch:
try { throw new Error('Something went wrong!'); } catch (error) { console.error(error.message); }
-
Object:
const person = { name: 'John', age: 30 }; const { name, age } = person;
-
Array:
const [first, second] = [10, 20];
-
Spread (copy or expand):
const arr1 = [1, 2, 3]; const arr2 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5]
-
Rest (group arguments):
function sum(...numbers: number[]) { return numbers.reduce((acc, num) => acc + num); }
-
Class:
class Animal { name: string; constructor(name: string) { this.name = name; } makeSound() { console.log('Sound'); } }
-
Inheritance:
class Dog extends Animal { bark() { console.log('Woof!'); } }
- Generic Function:
function identity<T>(arg: T): T { return arg; } const output = identity<number>(10); // 10
-
Named Export:
export const myVar = 10;
-
Default Export:
export default class MyClass { /* ... */ }
-
Importing:
import MyClass, { myVar } from './myModule';
- Type Guards:
function isString(value: unknown): value is string { return typeof value === 'string'; } const input: unknown = 'Hello'; if (isString(input)) { console.log(input.toUpperCase()); // Safe to use string methods }
- Interface:
interface User { name: string; age: number; } const user: User = { name: 'Alice', age: 25 };