{"id":168,"date":"2026-05-06T16:23:21","date_gmt":"2026-05-06T16:23:21","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=168"},"modified":"2026-05-06T16:23:23","modified_gmt":"2026-05-06T16:23:23","slug":"encapsulation","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=encapsulation","title":{"rendered":"Encapsulation"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<br>Encapsulation is a fundamental concept in object oriented programming. It refers to the practice of bundling data and the methods that operate on that data into a single unit such as an object. It also restricts direct access to some parts of the object to protect the data from unintended changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Core Concept<br>Encapsulation helps you control how data is accessed and modified. Instead of allowing direct access to variables, you use methods to interact with them. This improves security, flexibility, and maintainability of code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Why Encapsulation is Important<br>Encapsulation protects sensitive data from accidental modification<br>It improves code organization and readability<br>It allows controlled access through functions<br>It makes applications easier to maintain and update<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Encapsulation in JavaScript<br>In JavaScript, encapsulation can be achieved using functions, closures, and classes. Modern JavaScript also provides private fields using the hash symbol.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example Using Class<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person {<br>  #name;  <br><br>  constructor(name) {<br>    this.#name = name;<br>  }<br><br>  getName() {<br>    return this.#name;<br>  }<br><br>  setName(newName) {<br>    this.#name = newName;<br>  }<br>}<br><br>const person1 = new Person(\"Ali\");<br>console.log(person1.getName());<br><br>person1.setName(\"Ahmed\");<br>console.log(person1.getName());<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Explanation<br>The variable name is private and cannot be accessed directly from outside the class<br>The methods getName and setName are used to access and modify the value safely<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Real World Example<br>Think of a bank account. You cannot directly access your balance variable. Instead, you use methods like deposit and withdraw. This ensures proper validation and security.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Benefits of Encapsulation<br>Improves data security<br>Reduces complexity in large applications<br>Enhances code reusability<br>Allows better debugging and testing<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Best Practices<br>Always keep sensitive data private<br>Use getter and setter methods for controlled access<br>Avoid exposing internal object details directly<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion<br>Encapsulation is a key principle that helps developers write secure and organized code. By controlling how data is accessed, it ensures better reliability and maintainability in JavaScript applications.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/javascript\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Professional JavaScript > OOP in JavaScript > Encapsulation<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1778084184438\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1778084184258\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":0,"template":"","class_list":["post-168","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Encapsulation - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn encapsulation in JavaScript with examples to protect data improve security and build clean maintainable code easily\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/javascript\/?lesson=encapsulation\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Encapsulation - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn encapsulation in JavaScript with examples to protect data improve security and build clean maintainable code easily\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=encapsulation\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-06T16:23:23+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=encapsulation\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=encapsulation\",\"name\":\"Encapsulation - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-06T16:23:21+00:00\",\"dateModified\":\"2026-05-06T16:23:23+00:00\",\"description\":\"Learn encapsulation in JavaScript with examples to protect data improve security and build clean maintainable code easily\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=encapsulation#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=encapsulation\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=encapsulation#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Professional JavaScript > OOP in JavaScript > Encapsulation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/\",\"name\":\"Learn JavaScript with GIGZ.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Encapsulation - Learn JavaScript with GIGZ.PK","description":"Learn encapsulation in JavaScript with examples to protect data improve security and build clean maintainable code easily","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":"https:\/\/gigz.pk\/javascript\/?lesson=encapsulation","og_locale":"en_US","og_type":"article","og_title":"Encapsulation - Learn JavaScript with GIGZ.PK","og_description":"Learn encapsulation in JavaScript with examples to protect data improve security and build clean maintainable code easily","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=encapsulation","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-06T16:23:23+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/javascript\/?lesson=encapsulation","url":"https:\/\/gigz.pk\/javascript\/?lesson=encapsulation","name":"Encapsulation - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-06T16:23:21+00:00","dateModified":"2026-05-06T16:23:23+00:00","description":"Learn encapsulation in JavaScript with examples to protect data improve security and build clean maintainable code easily","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=encapsulation#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=encapsulation"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=encapsulation#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"Professional JavaScript > OOP in JavaScript > Encapsulation"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/javascript\/#website","url":"https:\/\/gigz.pk\/javascript\/","name":"Learn JavaScript with GIGZ.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/javascript\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson\/168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}