{"id":3989,"date":"2023-11-04T23:13:58","date_gmt":"2023-11-04T23:13:58","guid":{"rendered":"http:\/\/localhost:10003\/advanced-javascript-concepts\/"},"modified":"2023-11-05T05:48:25","modified_gmt":"2023-11-05T05:48:25","slug":"advanced-javascript-concepts","status":"publish","type":"post","link":"http:\/\/localhost:10003\/advanced-javascript-concepts\/","title":{"rendered":"Advanced JavaScript Concepts"},"content":{"rendered":"

Introduction<\/h2>\n

JavaScript is a popular programming language used for web development. Among its many features are advanced concepts that make it a versatile language. In this article, we will explore these concepts, including array methods, closures, classes, promises, and generators.<\/p>\n

Array Methods<\/h2>\n

JavaScript arrays are versatile data structures that can hold any type of data, including other arrays and objects. They also offer several built-in methods for manipulating data. Here are four advanced array methods you should know:<\/p>\n

map<\/h3>\n

The map<\/code> method creates a new array with the results of calling a provided function on every element in the calling array. This method does not modify the original array.<\/p>\n

const numbers = [1, 2, 3, 4];\nconst doubledNumbers = numbers.map(number => number * 2);\nconsole.log(doubledNumbers); \/\/ [2, 4, 6, 8]\n<\/code><\/pre>\n

filter<\/h3>\n

The filter<\/code> method creates a new array with all elements that pass the test implemented by the provided function. This method does not modify the original array.<\/p>\n

const numbers = [1, 2, 3, 4];\nconst evenNumbers = numbers.filter(number => number % 2 === 0);\nconsole.log(evenNumbers); \/\/ [2, 4]\n<\/code><\/pre>\n

reduce<\/h3>\n

The reduce<\/code> method applies a function against an accumulator and each element in the array to reduce it to a single value. This method does not modify the original array.<\/p>\n

const numbers = [1, 2, 3, 4];\nconst sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue);\nconsole.log(sum); \/\/ 10\n<\/code><\/pre>\n

forEach<\/h3>\n

The forEach<\/code> method executes a provided function once for each array element. This method does not create a new array and does not modify the original array.<\/p>\n

const numbers = [1, 2, 3, 4];\nnumbers.forEach(number => console.log(number * 2)); \/\/ 2, 4, 6, 8\n<\/code><\/pre>\n

Closures<\/h2>\n

A closure is a function that has access to variables in its outer (enclosing) function’s scope chain, even after the outer function has returned.<\/p>\n

const outerFunction = () => {\n  const outerVariable = 'outer variable';\n\n  const innerFunction = () => {\n    console.log(outerVariable);\n  };\n\n  return innerFunction;\n};\n\nconst innerFunction = outerFunction();\ninnerFunction(); \/\/ 'outer variable'\n<\/code><\/pre>\n

In the above example, innerFunction<\/code> has access to the outerVariable<\/code>, even though it is not directly declared inside the innerFunction<\/code>.<\/p>\n

Classes<\/h2>\n

JavaScript classes are a version of JavaScript’s prototypical inheritance. They provide a way to define complex objects with their own methods and properties, much like traditional object-oriented programming (OOP) languages.<\/p>\n

Defining classes<\/h3>\n
class Dog {\n  constructor(name, breed) {\n    this.name = name;\n    this.breed = breed;\n  }\n\n  bark() {\n    console.log('Woof!');\n  }\n\n  displayInfo() {\n    console.log(`Name: ${this.name}, Breed: ${this.breed}`);\n  }\n}\n\nconst myDog = new Dog('Rufus', 'Labrador');\nmyDog.displayInfo();\nmyDog.bark();\n<\/code><\/pre>\n

In the above example, we define a Dog<\/code> class with a constructor function that takes a name<\/code> and breed<\/code> argument and sets them as properties of the new instance. We also define a bark<\/code> method and a displayInfo<\/code> method that logs information about the dog instance.<\/p>\n

Extending classes<\/h3>\n

We can also extend existing classes to create new classes with added functionality.<\/p>\n

class ServiceDog extends Dog {\n  constructor(name, breed, serviceType) {\n    super(name, breed);\n    this.serviceType = serviceType;\n  }\n\n  performService() {\n    console.log(`Performing ${this.serviceType} service.`);\n  }\n}\n\nconst myServiceDog = new ServiceDog('Lacey', 'Golden Retriever', 'Therapy');\nmyServiceDog.displayInfo();\nmyServiceDog.bark();\nmyServiceDog.performService();\n<\/code><\/pre>\n

In the above example, we define a ServiceDog<\/code> class that extends the Dog<\/code> class. We use the super()<\/code> method to call the Dog<\/code> class constructor and add a serviceType<\/code> property. We also define a performService<\/code> method that is specific to the ServiceDog<\/code> class.<\/p>\n

Promises<\/h2>\n

Promises are objects that represent the eventual completion (or failure) of an asynchronous operation and its resulting value. They provide an alternative to working with callbacks.<\/p>\n

Creating promises<\/h3>\n
const myPromise = new Promise((resolve, reject) => {\n  setTimeout(() => {\n    resolve('Success!');\n  }, 1000);\n});\n<\/code><\/pre>\n

In the above example, we define a myPromise<\/code> object with a constructor function that takes a resolve<\/code> and reject<\/code> argument. We use the setTimeout()<\/code> function to simulate an asynchronous operation that resolves after one second and returns the string 'Success!'<\/code>.<\/p>\n

Working with promises<\/h3>\n
myPromise\n  .then(result => console.log(result))\n  .catch(error => console.log(error));\n<\/code><\/pre>\n

In the above example, we use the .then()<\/code> method to handle the resolved myPromise<\/code> result and the .catch()<\/code> method to handle any errors. This results in the string 'Success!'<\/code> being logged to the console after one second.<\/p>\n

Generators<\/h2>\n

Generators are functions that can be paused and resumed, allowing for the creation of iterators. They offer a powerful way to write asynchronous and concurrent code in a synchronous style.<\/p>\n

Defining generators<\/h3>\n
function* myGenerator() {\n  yield 1;\n  yield 2;\n  yield 3;\n}\n\nconst generator = myGenerator();\n\nconsole.log(generator.next().value); \/\/ 1\nconsole.log(generator.next().value); \/\/ 2\nconsole.log(generator.next().value); \/\/ 3\n<\/code><\/pre>\n

In the above example, we define a myGenerator<\/code> function that uses the yield<\/code> keyword to define a set of values to be returned by the generator. We create an instance of the generator and use the .next()<\/code> method to iterate through the values.<\/p>\n

Sending data to generators<\/h3>\n
function* myGenerator() {\n  const greeting = yield;\n  console.log(greeting);\n}\n\nconst generator = myGenerator();\n\ngenerator.next();\ngenerator.next('Hello!'); \/\/ 'Hello!'\n<\/code><\/pre>\n

In the above example, we define a myGenerator<\/code> function that assigns a greeting<\/code> variable to the value passed to the generator using the yield<\/code> keyword. We create an instance of the generator and use the .next()<\/code> method to start it and send the value 'Hello!'<\/code> to the generator.<\/p>\n

Conclusion<\/h2>\n

In this article, we explored several advanced JavaScript concepts, including array methods, closures, classes, promises, and generators. These concepts provide powerful ways to manipulate and work with data, as well as write asynchronous and concurrent code. Keep exploring these concepts and you’ll be on your way to mastering JavaScript!<\/p>\n","protected":false},"excerpt":{"rendered":"

Introduction JavaScript is a popular programming language used for web development. Among its many features are advanced concepts that make it a versatile language. In this article, we will explore these concepts, including array methods, closures, classes, promises, and generators. Array Methods JavaScript arrays are versatile data structures that can Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[50,725,726,49,724],"yoast_head":"\nAdvanced JavaScript Concepts - Pantherax Blogs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/localhost:10003\/advanced-javascript-concepts\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Advanced JavaScript Concepts\" \/>\n<meta property=\"og:description\" content=\"Introduction JavaScript is a popular programming language used for web development. Among its many features are advanced concepts that make it a versatile language. In this article, we will explore these concepts, including array methods, closures, classes, promises, and generators. Array Methods JavaScript arrays are versatile data structures that can Continue Reading\" \/>\n<meta property=\"og:url\" content=\"http:\/\/localhost:10003\/advanced-javascript-concepts\/\" \/>\n<meta property=\"og:site_name\" content=\"Pantherax Blogs\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-04T23:13:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-05T05:48:25+00:00\" \/>\n<meta name=\"author\" content=\"Panther\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Panther\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t \"@context\": \"https:\/\/schema.org\",\n\t \"@graph\": [\n\t {\n\t \"@type\": \"Article\",\n\t \"@id\": \"http:\/\/localhost:10003\/advanced-javascript-concepts\/#article\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/advanced-javascript-concepts\/\"\n\t },\n\t \"author\": {\n\t \"name\": \"Panther\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\"\n\t },\n\t \"headline\": \"Advanced JavaScript Concepts\",\n\t \"datePublished\": \"2023-11-04T23:13:58+00:00\",\n\t \"dateModified\": \"2023-11-05T05:48:25+00:00\",\n\t \"mainEntityOfPage\": {\n\t \"@id\": \"http:\/\/localhost:10003\/advanced-javascript-concepts\/\"\n\t },\n\t \"wordCount\": 635,\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"keywords\": [\n\t \"\\\"Front-end development\\\"]\",\n\t \"\\\"JavaScript programming\\\"\",\n\t \"\\\"JavaScript tips\\\"\",\n\t \"\\\"Web development\\\"\",\n\t \"[\\\"Advanced JavaScript Concepts\\\"\"\n\t ],\n\t \"inLanguage\": \"en-US\"\n\t },\n\t {\n\t \"@type\": \"WebPage\",\n\t \"@id\": \"http:\/\/localhost:10003\/advanced-javascript-concepts\/\",\n\t \"url\": \"http:\/\/localhost:10003\/advanced-javascript-concepts\/\",\n\t \"name\": \"Advanced JavaScript Concepts - Pantherax Blogs\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#website\"\n\t },\n\t \"datePublished\": \"2023-11-04T23:13:58+00:00\",\n\t \"dateModified\": \"2023-11-05T05:48:25+00:00\",\n\t \"breadcrumb\": {\n\t \"@id\": \"http:\/\/localhost:10003\/advanced-javascript-concepts\/#breadcrumb\"\n\t },\n\t \"inLanguage\": \"en-US\",\n\t \"potentialAction\": [\n\t {\n\t \"@type\": \"ReadAction\",\n\t \"target\": [\n\t \"http:\/\/localhost:10003\/advanced-javascript-concepts\/\"\n\t ]\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"BreadcrumbList\",\n\t \"@id\": \"http:\/\/localhost:10003\/advanced-javascript-concepts\/#breadcrumb\",\n\t \"itemListElement\": [\n\t {\n\t \"@type\": \"ListItem\",\n\t \"position\": 1,\n\t \"name\": \"Home\",\n\t \"item\": \"http:\/\/localhost:10003\/\"\n\t },\n\t {\n\t \"@type\": \"ListItem\",\n\t \"position\": 2,\n\t \"name\": \"Advanced JavaScript Concepts\"\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"WebSite\",\n\t \"@id\": \"http:\/\/localhost:10003\/#website\",\n\t \"url\": \"http:\/\/localhost:10003\/\",\n\t \"name\": \"Pantherax Blogs\",\n\t \"description\": \"\",\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"potentialAction\": [\n\t {\n\t \"@type\": \"SearchAction\",\n\t \"target\": {\n\t \"@type\": \"EntryPoint\",\n\t \"urlTemplate\": \"http:\/\/localhost:10003\/?s={search_term_string}\"\n\t },\n\t \"query-input\": \"required name=search_term_string\"\n\t }\n\t ],\n\t \"inLanguage\": \"en-US\"\n\t },\n\t {\n\t \"@type\": \"Organization\",\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\",\n\t \"name\": \"Pantherax Blogs\",\n\t \"url\": \"http:\/\/localhost:10003\/\",\n\t \"logo\": {\n\t \"@type\": \"ImageObject\",\n\t \"inLanguage\": \"en-US\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/logo\/image\/\",\n\t \"url\": \"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg\",\n\t \"contentUrl\": \"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg\",\n\t \"width\": 1024,\n\t \"height\": 1024,\n\t \"caption\": \"Pantherax Blogs\"\n\t },\n\t \"image\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/logo\/image\/\"\n\t }\n\t },\n\t {\n\t \"@type\": \"Person\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\",\n\t \"name\": \"Panther\",\n\t \"image\": {\n\t \"@type\": \"ImageObject\",\n\t \"inLanguage\": \"en-US\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/image\/\",\n\t \"url\": \"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g\",\n\t \"contentUrl\": \"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g\",\n\t \"caption\": \"Panther\"\n\t },\n\t \"sameAs\": [\n\t \"http:\/\/localhost:10003\"\n\t ],\n\t \"url\": \"http:\/\/localhost:10003\/author\/pepethefrog\/\"\n\t }\n\t ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Advanced JavaScript Concepts - Pantherax Blogs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/localhost:10003\/advanced-javascript-concepts\/","og_locale":"en_US","og_type":"article","og_title":"Advanced JavaScript Concepts","og_description":"Introduction JavaScript is a popular programming language used for web development. Among its many features are advanced concepts that make it a versatile language. In this article, we will explore these concepts, including array methods, closures, classes, promises, and generators. Array Methods JavaScript arrays are versatile data structures that can Continue Reading","og_url":"http:\/\/localhost:10003\/advanced-javascript-concepts\/","og_site_name":"Pantherax Blogs","article_published_time":"2023-11-04T23:13:58+00:00","article_modified_time":"2023-11-05T05:48:25+00:00","author":"Panther","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Panther","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/localhost:10003\/advanced-javascript-concepts\/#article","isPartOf":{"@id":"http:\/\/localhost:10003\/advanced-javascript-concepts\/"},"author":{"name":"Panther","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7"},"headline":"Advanced JavaScript Concepts","datePublished":"2023-11-04T23:13:58+00:00","dateModified":"2023-11-05T05:48:25+00:00","mainEntityOfPage":{"@id":"http:\/\/localhost:10003\/advanced-javascript-concepts\/"},"wordCount":635,"publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"keywords":["\"Front-end development\"]","\"JavaScript programming\"","\"JavaScript tips\"","\"Web development\"","[\"Advanced JavaScript Concepts\""],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/localhost:10003\/advanced-javascript-concepts\/","url":"http:\/\/localhost:10003\/advanced-javascript-concepts\/","name":"Advanced JavaScript Concepts - Pantherax Blogs","isPartOf":{"@id":"http:\/\/localhost:10003\/#website"},"datePublished":"2023-11-04T23:13:58+00:00","dateModified":"2023-11-05T05:48:25+00:00","breadcrumb":{"@id":"http:\/\/localhost:10003\/advanced-javascript-concepts\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/localhost:10003\/advanced-javascript-concepts\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/localhost:10003\/advanced-javascript-concepts\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/localhost:10003\/"},{"@type":"ListItem","position":2,"name":"Advanced JavaScript Concepts"}]},{"@type":"WebSite","@id":"http:\/\/localhost:10003\/#website","url":"http:\/\/localhost:10003\/","name":"Pantherax Blogs","description":"","publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/localhost:10003\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/localhost:10003\/#organization","name":"Pantherax Blogs","url":"http:\/\/localhost:10003\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/localhost:10003\/#\/schema\/logo\/image\/","url":"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg","contentUrl":"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg","width":1024,"height":1024,"caption":"Pantherax Blogs"},"image":{"@id":"http:\/\/localhost:10003\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7","name":"Panther","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/localhost:10003\/#\/schema\/person\/image\/","url":"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g","caption":"Panther"},"sameAs":["http:\/\/localhost:10003"],"url":"http:\/\/localhost:10003\/author\/pepethefrog\/"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3989"}],"collection":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/comments?post=3989"}],"version-history":[{"count":1,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3989\/revisions"}],"predecessor-version":[{"id":4565,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3989\/revisions\/4565"}],"wp:attachment":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/media?parent=3989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/categories?post=3989"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/tags?post=3989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}