{"id":29149954,"url":"https://github.com/ajay-develops/es6","last_synced_at":"2025-07-09T16:32:42.144Z","repository":{"id":117636335,"uuid":"532457503","full_name":"ajay-develops/ES6","owner":"ajay-develops","description":null,"archived":false,"fork":false,"pushed_at":"2022-09-06T16:58:56.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-30T23:03:38.850Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/ajay-develops.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-04T06:28:26.000Z","updated_at":"2022-09-04T06:28:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"c077477c-6700-4de7-987a-a4c6df015451","html_url":"https://github.com/ajay-develops/ES6","commit_stats":null,"previous_names":["templar-ajay/es6","ajay-develops/es6"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ajay-develops/ES6","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajay-develops%2FES6","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajay-develops%2FES6/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajay-develops%2FES6/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajay-develops%2FES6/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ajay-develops","download_url":"https://codeload.github.com/ajay-develops/ES6/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajay-develops%2FES6/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262863710,"owners_count":23376453,"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":[],"created_at":"2025-06-30T23:03:38.269Z","updated_at":"2025-06-30T23:03:39.014Z","avatar_url":"https://github.com/ajay-develops.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ES6 syntax\n\n## ECMAScript Variables and Data Structures\n\n### String Methods\n\n```js\nstring.startsWith(\"xyz\"); // returns true or false\nstring.endsWith(\"xyz\"); // returns true or false\nstring.includes(\"xyz\"); // returns true or false\nstring.search(\"xyz\"); // returns index of the first instance of xyz in string\n```\n\n### Symbols\n\n```js\nconst id = Symbol()\n\nconst bookObj = {\n    title : \"JavaScript\",\n    topics:[\"strings\",\"arrays\",\"objects\"]\n    id = \"the-javascript-book\"\n}\nbookObj[id] = 412336\nconsole.log(bookObj)\n\n/* OUTPUT\n {\n  title: 'JavaScript',\n  topics: [ 'strings', 'arrays', 'objects' ],\n  id: 'the-javascript-book',\n  [Symbol()]: 412336\n}\n*/\n```\n\n### Writing Maps\n\n```js\nlet course = new Map();\n\ncourse.set(\"react\", { description: \"ui\" });\ncourse.set(\"jest\", { description: \"testing\" });\n\nconsole.log(course); // returns the map\nconsole.log(course.react || course[\"react\"]); // returns undefined\n\nlet details = new Map([\n  [new Date(), \"today\"],\n  [2, { javascript: [\"js\", \"node\", \"react\"] }],\n  [\"items\", [1, 2]],\n]);\n\nconsole.log(details);\nconsole.log(details.size);\n\ndetails.forEach((item) =\u003e {\n  console.log(item);\n}); // logs out items in their insertion order\n\nlet detailsObj = {\n  3: \"hello\",\n  2: \"bye\",\n};\n\nfor (item in detailsObj) {\n  console.log(item, detailsObj[item]);\n} // logs out items in ascending order\n\n/* OUTPUT\nMap(2) {\n  'react' =\u003e { description: 'ui' },\n  'jest' =\u003e { description: 'testing' }\n}\nundefined\nMap(3) {\n  2022-09-01T18:01:21.278Z =\u003e 'today',\n  2 =\u003e { javascript: [ 'js', 'node', 'react' ] },\n  'items' =\u003e [ 1, 2 ]\n}\n3\ntoday\n{ javascript: [ 'js', 'node', 'react' ] }\n[ 1, 2 ]\n2 bye\n3 hello\n*/\n```\n\n### Working with sets\n\n```js\nlet books = new Set();\nbooks.add(\"Pride and prejudice\");\nbooks.add(\"War and Peace\").add(\"Oliver Twist\");\nbooks.add(\"War and Peace\");\n\nconsole.log(books); // logs the set of books\nconsole.log(books.size); // logs 3\nconsole.log(\"has Oliver Twist\", books.has(\"Oliver Twist\")); //  logs -\u003e has Oliver Twist true\nbooks.delete(\"Oliver Twist\");\nconsole.log(\"has Oliver Twist\", books.has(\"Oliver Twist\")); //  logs -\u003e has Oliver Twist false\n\nbooks.map((item) =\u003e {\n  console.log(item);\n}); // books.map is not a function , .map is not available for sets\n\nbooks.forEach((item) =\u003e {\n  console.log(item);\n}); // for Each is available for sets\n```\n\n## Arrays and Array Methods\n\n### Using the array spread operator\n\n```js\nlet cats = [\"biscuit\", \"Junglee\"];\nlet dogs = [\"Stella\", \"Camper\"];\n\nlet animals = [\"smokie\", \"Kurama\", \"neuay\", ...cats, ...dogs];\nconsole.log(animals); // logs [\"smokie\",\"Kurama\",\"neuay\",\"biscuit\",\"Junglee\",\"Stella\",\"Camper\"]\n```\n\n### Destructuring Arrays\n\n```js\nlet [first, , , , fifth] = [\n  \"Spokane\",\n  \"Boston\",\n  \"Los Angeles\",\n  \"Seattle\",\n  \"Portland\",\n];\nlet [firstCity, ...otherCities] = [\n  \"Spokane\",\n  \"Boston\",\n  \"Los Angeles\",\n  \"Seattle\",\n  \"Portland\",\n];\n\nconsole.log(first);\n// logs Spokane\nconsole.log(fifth);\n// logs Portland\nconsole.log(firstCity);\n// logs Spokane\nconsole.log(otherCities);\n// logs [ 'Boston', 'Los Angeles', 'Seattle', 'Portland' ]\n```\n\n## ECMASCRIPT OBJECTS\n\n### Enhancing Object Literals\n\n```js\n// old way\nfunction skier(name, sound) {\n  return {\n    name: name,\n    sound: sound,\n    powderYell: function () {\n      let yell = this.sound.toUpperCase();\n      console.log(`${yell}! ${yell}!`);\n    },\n  };\n}\nskier(\"Andy\", \"yeah\").powderYell();\n\n// neuay\nfunction skier(name, sound) {\n  return {\n    name,\n    sound,\n    powderYell: function () {\n      let yell = this.sound.toUpperCase();\n      console.log(`${yell}! ${yell}!`);\n    },\n  };\n}\nskier(\"Andy\", \"yeah\").powderYell();\n```\n\n### Creating Objects with Spread Operator\n\n```js\nconst daytime = {\n  breakfast: \"oatmeal\",\n  lunch: \"peanut butter and jelly\",\n};\n\nconst nighttime = \"mac and cheese\";\n\nconst backPacking = {\n  ...daytime, // will spread the daytime object\n  nighttime,\n};\n```\n\n### Destructuring Objects\n\n```js\n// Object Destructuring\nconst vacation = {\n  destination: \"Chile\",\n  travelers: 2,\n  activity: \"skiing\",\n  cost: \"so much\",\n};\n\nfunction marketing({ destination, activity }) {\n  return `come to ${destination} and do some ${activity}`;\n}\n\nconsole.log(marketing(vacation)); // logs come to Chile and do some skiing\n\n// another example\nconst { title, price } = {\n  title: \"Jalebi\",\n  price: \"20₹\", // rupee sign (right-Alt + 4)\n  type: \"sweet\",\n  ingredients: [\"sweet\", \"sweet\", \"sweet\", \"sweet\"],\n};\nconsole.log(title, price); // logs Jalebi 20₹\n```\n\n### For/Of loop\n\n```js\nfor (let letter of \"JavaScript\") {\n  console.log(letter);\n}\n\nconst topics = [\"javaScript\", \"HTML\", \"Node\", \"CSS\"];\n\nfor (let topic of topics) {\n  console.log(topic);\n}\n\nconst topicRoutes = new Map();\n\ntopicRoutes.set(\"JavaScript\", \"/topic/JavaScript\");\ntopicRoutes.set(\"HTML\", \"/topic/html\");\ntopicRoutes.set(\"Node\", \"/topic/Node\");\ntopicRoutes.set(\"CSS\", \"/topic/css\");\n\nfor (let topicRoute of topicRoutes) {\n  console.log(topicRoute);\n}\nfor (let topicRoute of topicRoutes.keys()) {\n  console.log(topicRoute);\n}\nfor (let topicRoute of topicRoutes.values()) {\n  console.log(topicRoute);\n}\nfor (let topicRoute of topicRoutes.entries()) {\n  console.log(topicRoute);\n}\n\n// logs out\n/*\nJ\na\nv\na\nS\nc\nr\ni\np\nt\njavaScript\nHTML\nNode\nCSS\n['JavaScript', '/topic/JavaScript']\n['HTML', '/topic/html']\n['Node', '/topic/Node']\n['CSS', '/topic/css']\nJavaScript\nHTML\nNode\nCSS\n    / topic / JavaScript\n    / topic / html\n    / topic / Node\n    / topic / css\n    ['JavaScript', '/topic/JavaScript']\n    ['HTML', '/topic/html']\n    ['Node', '/topic/Node']\n    ['CSS', '/topic/css']\n*/\n```\n\n### Classes in Js\n\n```js\nclass Vehicle {\n  constructor(description, wheels) {\n    this.description = description;\n    this.wheels = wheels;\n  }\n  describeYourself() {\n    console.log(`I am a ${this.description} with ${this.wheels} wheels`);\n  }\n}\n\nlet coolSkiVan = new Vehicle(\"cool Ski Van\", 4);\ncoolSkiVan.describeYourself(); // logs out - I am a cool Ski Van with 4 wheels\n```\n\n### Inheritance in Js Classes\n\n```js\nclass SemiTruck extends Vehicle {\n  constructor() {\n    super(\"Semi Truck\", 18);\n  }\n}\n\nlet groceryStoreSemiTruck = new SemiTruck();\ngroceryStoreSemiTruck.describeYourself(); // logs out - I am a Semi Truck with 18 wheels\n```\n\n### Getting and Setting Class Values\n\n#### in objects\n\n```js\nlet attendance = {\n  _list: [],\n\n  // setter\n  set addName(name) {\n    this._list.push(name);\n  },\n\n  //getter\n  get list() {\n    return this._list.join(\", \");\n  },\n};\n\n// using setter\nattendance.addName = \"John\"; //  note this is wrong syntax for setter -\u003e attendance.addName(\"John\")\n\n//using getter\nconsole.log(attendance.list); // logs out - John\n\n// using setter\nattendance.addName = \"Ajay\";\nattendance.addName = \"xCommand0x\";\nattendance.addName = \"MadManOP\";\n\n// using getter\nconsole.log(attendance.list); // logs out - John, Ajay, xCommand0x, MadManOP\n```\n\n#### in classes\n\n```js\n// in classes\nclass Hike {\n  constructor(distance) {\n    this.distance = distance;\n  }\n  set setPace(pace) {\n    this.pace = pace;\n  }\n  get lengthInHours() {\n    return this.pace ? `${this.calcLength()} hours` : \"please set a pace first\";\n  }\n  calcLength() {\n    return this.distance / this.pace;\n  }\n}\n\nconst mtEverest = new Hike(8.849);\n\n// using setter\nmtEverest.setPace = 0.1;\n\n// using getter\nconsole.log(mtEverest.lengthInHours); // logs out 88.49 hours\n```\n\n## ECMAScript Functions\n\n### using the string.repeat() function\n\n```js\nlet yell = \"woo!\";\n\nlet party = yell.repeat(20);\n\nconsole.log(party);\n\nlet dog = {\n  hrr(times) {\n    console.log(\"H\" + \"r\".repeat(times));\n  },\n  bark(times) {\n    console.log(\"Bhow\".repeat(times));\n  },\n  snore(times) {\n    console.log(\"Zzzz\".repeat(times));\n  },\n};\n\ndog.hrr(6); //  logs out -\u003e Hrrrrrr\ndog.bark(2); //  logs out -\u003e BhowBhow\ndog.snore(6); // logs out -\u003e ZzzzZzzzZzzzZzzzZzzzZzzz\n```\n\n### setting default parameters\n\n```js\nfunction add(x = 3, y = 4) {\n  return x + y;\n}\nconsole.log(add());\nconsole.log(add(10));\nconsole.log(add(10, 20));\n```\n\n### Arrow Function\n\n```js\nlet studentList = (...students) =\u003e {\n  return students.join(\", \");\n};\n\nstudentList(\"Ajay\", \"Ben10\", \"Naruto\");\n\n// we can skip the return statement by skipping the curly braces {} and putting the function in one line\nlet fruitsList = (...fruits) =\u003e fruits.join(\", \");\n\nconsole.log(fruitsList([\"apple\", \"banana\", \"mango\"]));\n```\n\n### Understanding this in arrow functions\n\n```js\n// old ways of using this - using normal functions - function(){}\nlet person = {\n  first: \"Ajay\",\n  hobbies: [\"Chess\", \"trekking\", \"Run\", \"Gym\"],\n  printHobbies: function () {\n    const _this = this;\n    this.hobbies.forEach(function (hobby) {\n      let string = `${_this.first} loves to ${hobby}`;\n      console.log(string);\n    });\n  },\n};\n\nperson.printHobbies();\n\n// new method by using arrow functions ()=\u003e{}\nperson = {\n  first: \"Ajay\",\n  hobbies: [\"Chess\", \"trekking\", \"Run\", \"Gym\"],\n  printHobbies: function () {\n    this.hobbies.forEach((hobby) =\u003e {\n      let string = `${this.first} loves to ${hobby}`;\n      console.log(string);\n    });\n  },\n};\nperson.printHobbies();\n\n/* output\nAjay loves to Chess\nAjay loves to trekking\nAjay loves to Run\nAjay loves to Gym\nAjay loves to Chess\nAjay loves to trekking\nAjay loves to Run\nAjay loves to Gym\n*/\n```\n\n### working with Generators\n\n```js\nfunction* director() {\n  yield \"three\";\n  yield \"Two\";\n  yield \"One\";\n  yield \"Action\";\n}\n\nlet countdown = director();\n\nconsole.log(countdown.next());\nconsole.log(countdown.next());\nconsole.log(countdown.next().value);\nconsole.log(countdown.next());\nconsole.log(countdown.next());\n/*\n{ value: 'three', done: false }\n{ value: 'Two', done: false }\nOne\n{ value: 'Action', done: false }\n{ value: undefined, done: true }\n*/\n```\n\n## Asynchronous JavaScript\n\n### Building Promises\n\n```js\nconst delay = (seconds) =\u003e\n  new Promise((resolve, reject) =\u003e\n    typeof seconds === \"number\"\n      ? setTimeout(resolve, seconds * 1000)\n      : reject(new Error(\"seconds must be number\"))\n  );\n\ndelay(1).then(() =\u003e console.log(\"one second\"));\n// logs out- one second\n\ndelay(\"two seconds\")\n  .then(() =\u003e console.log(`two seconds`))\n  .catch((err) =\u003e console.log(`error ocurred`, err));\n// returns an error message- error ocurred Error: seconds must be a number\n```\n\n### Loading remote data with promises\n\n```js\nconst spacePeople = () =\u003e {\n  return new Promise((resolves, rejects) =\u003e {\n    const api = \"http://api.open-notify.org/astros.json\";\n    const request = new XMLHttpRequest();\n    request.open(\"GET\", api);\n    request.onload = () =\u003e {\n      request.status === 200\n        ? resolves(JSON.parse(request.response))\n        : rejects(Error(request.statusText));\n    };\n    request.onerror = (err) =\u003e {\n      rejects(err);\n    };\n    request.send();\n  });\n};\n\nspacePeople()\n  .then((spaceData) =\u003e {\n    console.log(spaceData);\n  })\n  .catch((err) =\u003e {\n    console.log(err);\n  });\n```\n\n### Returning Promises with Fetch\n\n```js\nlet getSpacePeople = () =\u003e\n  fetch(\"http://api.open-notify.org/astros.json\").then((res) =\u003e res.json());\n\nlet getSpaceNames = () =\u003e\n  getSpacePeople()\n    .then((json) =\u003e json.people)\n    .then((people) =\u003e people.map((p) =\u003e p.name))\n    .then((namesArr) =\u003e namesArr.join(\", \"))\n    .then(console.log); // smaller syntax of \"names =\u003e console.log(names)\"\n\ngetSpaceNames();\n// logs out- Oleg Artemyev, Denis Matveev, Sergey Korsakov, Kjell Lindgren, Bob Hines,\n//  Samantha Cristoforetti, Jessica Watkins, Cai Xuzhe, Chen Dong, Liu Yang\n```\n\n### Using Async / Await\n\n```js\nconst delay = (seconds) =\u003e\n  new Promise((resolves) =\u003e setTimeout(resolves, seconds * 1000));\nconst countToFour = async () =\u003e {\n  console.log(\"zero seconds\");\n  await delay(1);\n  console.log(`1 second`);\n  await delay(2);\n  console.log(`3 seconds`);\n  await delay(1);\n  console.log(`4 seconds`);\n};\n\ncountToFour();\n// logs out \"zero seconds\" instantly, waits 1 second and logs out \"1 second\", waits 2 more seconds and logs out \"3 seconds\", waits 1 more second and logs out \"4 seconds\"\n```\n\n### incorporating Fetch with Async Await\n\n```js\nconst githubRequest = async (userName) =\u003e {\n  const response = await fetch(`https://api.github.com/users/${userName}`);\n  const json = await response.json();\n  console.log(`${json.name} works at ${json.company}`);\n};\ngithubRequest(\"templar-command0\");\n// logs out \"command0 works at null\" , cuz I haven't updated it on github\n```\n\n## Useful links\n\n- [ECMAScript github profile](https://github.com/tc39)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajay-develops%2Fes6","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fajay-develops%2Fes6","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajay-develops%2Fes6/lists"}