Skip to content

Latest commit

 

History

History
229 lines (187 loc) · 4.06 KB

javascript.md

File metadata and controls

229 lines (187 loc) · 4.06 KB

1. Data Structures

  • 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}

2. String Methods

  • 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')

3. Array Methods

  • 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);

4. Loops

  • 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);
    }

5. TypeScript Types

  • 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 }

6. Asynchronous Programming

  • 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);
    }

7. Error Handling

  • Try/Catch:
    try {
      throw new Error('Something went wrong!');
    } catch (error) {
      console.error(error.message);
    }

8. Object/Array Destructuring

  • Object:

    const person = { name: 'John', age: 30 };
    const { name, age } = person;
  • Array:

    const [first, second] = [10, 20];

9. Spread and Rest Operators

  • 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);
    }

10. Classes and Inheritance

  • Class:

    class Animal {
      name: string;
      constructor(name: string) {
        this.name = name;
      }
      makeSound() {
        console.log('Sound');
      }
    }
  • Inheritance:

    class Dog extends Animal {
      bark() {
        console.log('Woof!');
      }
    }

11. TypeScript Generics

  • Generic Function:
    function identity<T>(arg: T): T {
      return arg;
    }
    const output = identity<number>(10); // 10

12. Modules and Imports/Exports

  • Named Export:

    export const myVar = 10;
  • Default Export:

    export default class MyClass { /* ... */ }
  • Importing:

    import MyClass, { myVar } from './myModule';

13. Type Guards

  • 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
    }

14. Interfaces

  • Interface:
    interface User {
      name: string;
      age: number;
    }
    
    const user: User = { name: 'Alice', age: 25 };