burme-coder-max / data /knowledge /skills /javascript_skills.md
amkyawdev's picture
Upload folder using huggingface_hub
a7d7463 verified
|
Raw
History Blame Contribute Delete
2.19 kB

JavaScript Skills

Variables & Types

Variables

// Variables
let name = "JavaScript";
const PI = 3.14159;
var oldStyle = "deprecated";

// Types
typeof name; // "string"
typeof 42;   // "number"
typeof true;  // "boolean"

Functions

Regular Function

function greet(name) {
    return `Hello, ${name}!`;
}

Arrow Function

const greet = (name) => `Hello, ${name}!`;

// Multi-line
const multiply = (a, b) => {
    return a * b;
};

Callback

const processData = (data, callback) => {
    const result = doSomething(data);
    callback(result);
};

Arrays

Array Methods

const arr = [1, 2, 3, 4, 5];

// map - transform
arr.map(x => x * 2); // [2, 4, 6, 8, 10]

// filter - select
arr.filter(x => x > 2); // [3, 4, 5]

// reduce - accumulate
arr.reduce((sum, x) => sum + x, 0); // 15

// find - search
arr.find(x => x === 3); // 3

// forEach - iterate
arr.forEach(x => console.log(x));

Objects & Destructuring

Object

const person = {
    name: "John",
    age: 30,
    greet() {
        return `Hello, I'm ${this.name}`;
    }
};

// Access
person.name;
person['age'];

Destructuring

const { name, age } = person;
const [first, second] = arr;

Async/Await & Promise

Promise

const fetchData = () => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("data");
        }, 1000);
    });
};

Async/Await

const getData = async () => {
    try {
        const response = await fetch(url);
        const data = await response.json();
        return data;
    } catch (error) {
        console.error(error);
    }
};

Promise.all

const [result1, result2] = await Promise.all([
    fetch(url1),
    fetch(url2)
]);

Common Patterns

Optional Chaining

const value = obj?.nested?.property ?? "default";

Nullish Coalescing

const value = input ?? "default value";

Spread Operator

const merged = { ...obj1, ...obj2 };
const combined = [...arr1, ...arr2];