{"id":24527637,"url":"https://github.com/iamabhaytiwari343/javascript","last_synced_at":"2025-06-26T02:35:53.263Z","repository":{"id":151331039,"uuid":"619418244","full_name":"iamabhaytiwari343/javascript","owner":"iamabhaytiwari343","description":"this repository contains the basic fundamentals of most widely used language of web \"javascript\" ","archived":false,"fork":false,"pushed_at":"2023-12-15T17:01:17.000Z","size":156,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-15T16:35:14.962Z","etag":null,"topics":["basic","fundamentals","javascript","programming"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iamabhaytiwari343.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-03-27T05:22:31.000Z","updated_at":"2023-04-06T04:10:40.000Z","dependencies_parsed_at":"2023-12-06T13:42:25.404Z","dependency_job_id":"e22035f9-fc41-4bdb-ba2e-9e10cc442d08","html_url":"https://github.com/iamabhaytiwari343/javascript","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/iamabhaytiwari343/javascript","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamabhaytiwari343%2Fjavascript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamabhaytiwari343%2Fjavascript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamabhaytiwari343%2Fjavascript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamabhaytiwari343%2Fjavascript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iamabhaytiwari343","download_url":"https://codeload.github.com/iamabhaytiwari343/javascript/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamabhaytiwari343%2Fjavascript/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261987580,"owners_count":23240868,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["basic","fundamentals","javascript","programming"],"created_at":"2025-01-22T06:19:51.954Z","updated_at":"2025-06-26T02:35:53.229Z","avatar_url":"https://github.com/iamabhaytiwari343.png","language":"JavaScript","readme":"# javascript\n\nrefer to: https://roadmap.sh/javascript\n\n# Understanding JavaScript Closures\n\nClosures are a fundamental concept in JavaScript that may initially appear complex, but I'll explain them in simple terms with an example to make it more accessible.\n\n## What are Closures?\n\nImagine you have a function within another function. The inner function has access to the variables and parameters of the outer function, even after the outer function has finished running. This is what we call a closure.\n\n## Example\n\nLet's illustrate closures with a simple example:\n\n```javascript\nfunction outerFunction(x) {\n  // This is the outer function\n  return function innerFunction(y) {\n    // This is the inner function\n    return x + y;\n  };\n}\n\nconst closure = outerFunction(10); // 'closure' now stores the inner function\nconst result = closure(5); // When we call 'closure', it still remembers 'x'\nconsole.log(result); // Outputs 15\n```\n\n# Understanding Anonymous Functions and Arrow Functions in JavaScript\n\nAnonymous functions and arrow functions are two different ways of writing functions in JavaScript. Let's explain them in simple terms with examples.\n\n## Anonymous Function\n\nAn anonymous function is a function without a name. It's often used when you need a function for a short task, like passing it as an argument to another function. Here's how it looks:\n\n```javascript\nconst add = function(x, y) {\n  return x + y;\n};\n\nconst result = add(3, 5);\nconsole.log(result); // Outputs 8\n```\n## Arrow Functions\n\nArrow functions are a more concise way of writing functions, introduced in ES6 (ECMAScript 2015). They are especially handy for shorter functions. Here's how an arrow function works:\n\n```javascript\nconst add = (x, y) =\u003e x + y;\n\nconst result = add(3, 5);\nconsole.log(result); // Outputs 8\n```\nIn this example, the arrow function add is defined using (x, y) =\u003e x + y, which is a shorter way of writing a function. It takes two parameters and returns their sum. The result is the same as with the anonymous function.\n\nThe key difference between the two is that arrow functions have a shorter syntax and automatically capture the value of this from their surrounding context, which can be beneficial in certain situations. Anonymous functions, on the other hand, are more flexible and allow you to name the function if needed.\n\n# Promises and Their Methods (then, catch, finally)\n\nPromises are a fundamental concept in JavaScript for handling asynchronous operations. They provide a way to work with data or perform actions that may take some time to complete. Promises have three important methods: `then`, `catch`, and `finally`.\n\n## `then`\n\nThe `then` method is used to handle the successful completion of a Promise. It takes one or two arguments:\n\n1. A callback function that will be executed when the Promise is resolved (i.e., when the asynchronous operation succeeds).\n2. An optional callback function that will be executed when the Promise is rejected (if there's an error).\n\n```javascript\nconst promise = new Promise((resolve, reject) =\u003e {\n  // Simulate an asynchronous operation\n  setTimeout(() =\u003e {\n    const data = \"Success!\";\n    resolve(data); // Resolve the Promise with the data\n  }, 1000);\n});\n\npromise.then((data) =\u003e {\n  console.log(\"Resolved:\", data); // Output: Resolved: Success!\n}).catch((error) =\u003e {\n  console.error(\"Rejected:\", error);\n});\n```\n## catch\nThe catch method is used to handle errors or rejections of a Promise. It takes a callback function that will be executed when the Promise is rejected. If an error occurs in the Promise, it will jump to the nearest catch block.\n```\nconst promise = new Promise((resolve, reject) =\u003e {\n  setTimeout(() =\u003e {\n    reject(\"Something went wrong!\"); // Reject the Promise with an error message\n  }, 1000);\n});\n\npromise.then((data) =\u003e {\n  console.log(\"Resolved:\", data);\n}).catch((error) =\u003e {\n  console.error(\"Rejected:\", error); // Output: Rejected: Something went wrong!\n});\n\n```\n## finally \nThe finally method is used to specify a callback function that will be executed regardless of whether the Promise is resolved or rejected. It's useful for cleanup operations that need to be performed in any case.\n```\nconst promise = new Promise((resolve, reject) =\u003e {\n  setTimeout(() =\u003e {\n    resolve(\"Success!\");\n  }, 1000);\n});\n\npromise\n  .then((data) =\u003e {\n    console.log(\"Resolved:\", data);\n  })\n  .catch((error) =\u003e {\n    console.error(\"Rejected:\", error);\n  })\n  .finally(() =\u003e {\n    console.log(\"Promise completed, regardless of outcome.\");\n  });\n\n\n```\nPromises, along with their then, catch, and finally methods, provide a structured way to work with asynchronous operations in JavaScript, making it easier to handle both successful results and errors.\n\nPlease let me know if you would like more details on any specific aspect or have additional questions.\n\n### Promise.all and Promise.race\n\nPromise.all and Promise.race are two methods for handling multiple promises in JavaScript. They are useful when you have several asynchronous tasks that need to be coordinated or when you want to deal with the first result among several promises.\n\n#### Promise.all\n- Promise.all takes an array of promises as input and returns a new promise.\n- This new promise resolves when all the input promises have resolved or rejects as soon as any one of them rejects.\n- It returns an array of resolved values corresponding to the input promises in the same order.\n\n```\nconst promise1 = fetch('https://api.example.com/data1');\nconst promise2 = fetch('https://api.example.com/data2');\n\nPromise.all([promise1, promise2])\n  .then((results) =\u003e {\n    console.log('All promises resolved:', results);\n  })\n  .catch((error) =\u003e {\n    console.error('At least one promise rejected:', error);\n  });\n\n\n```\n#### Promise.race\n\n- Promise.race also takes an array of promises as input and returns a new promise.\n- This new promise resolves or rejects as soon as the first promise in the input array resolves or rejects. It doesn't wait for all promises to complete.\n\n```\nconst promise1 = fetch('https://api.example.com/data1');\nconst promise2 = fetch('https://api.example.com/data2');\n\nPromise.race([promise1, promise2])\n  .then((result) =\u003e {\n    console.log('The first promise to resolve:', result);\n  })\n  .catch((error) =\u003e {\n    console.error('The first promise to reject:', error);\n  });\n\n\n```\n\nIn the case of Promise.all, you ensure that all promises have completed successfully, and you get an array of their results. In the case of Promise.race, you get the result (or error) of the first promise to resolve or reject. These methods are handy for scenarios where you need to coordinate multiple asynchronous operations.\n\n### Handling Async Operations\n\nHandling asynchronous operations in JavaScript involves working with promises and other asynchronous patterns to ensure that your code doesn't block and remains responsive to other tasks.\n\n#### Using Promises\n\nPromises are a way to manage asynchronous operations in a more structured and readable manner. You can create a promise and specify what should happen when the operation is complete (resolved) or encounters an error (rejected).\n\n```\nconst fetchData = () =\u003e {\n  return new Promise((resolve, reject) =\u003e {\n    setTimeout(() =\u003e {\n      // Simulate data retrieval\n      const data = 'Some data';\n      resolve(data); // Resolve with the data\n    }, 1000);\n  });\n};\n\nfetchData()\n  .then((data) =\u003e {\n    console.log('Data received:', data);\n  })\n  .catch((error) =\u003e {\n    console.error('Error:', error);\n  });\n\n\n```\n#### Async/Await\nAsync/await is a more recent addition to JavaScript that simplifies working with promises. It allows you to write asynchronous code in a more synchronous-like fashion, making it easier to read and maintain.\n\n```\nconst fetchData = async () =\u003e {\n  try {\n    const data = await fetch('https://api.example.com/data');\n    console.log('Data received:', data);\n  } catch (error) {\n    console.error('Error:', error);\n  }\n};\n\nfetchData();\n\n\n```\n\n#### CallBacks\n\nCallbacks are a traditional way to handle asynchronous operations. You pass a function (the callback) that should be executed when the operation is complete. However, this can lead to callback hell or deeply nested functions when dealing with multiple async tasks.\n\n```\nconst fetchData = (callback) =\u003e {\n  setTimeout(() =\u003e {\n    const data = 'Some data';\n    callback(data);\n  }, 1000);\n};\n\nfetchData((data) =\u003e {\n  console.log('Data received:', data);\n});\n\n\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamabhaytiwari343%2Fjavascript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiamabhaytiwari343%2Fjavascript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamabhaytiwari343%2Fjavascript/lists"}