{"id":162,"date":"2026-05-06T09:20:25","date_gmt":"2026-05-06T09:20:25","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=162"},"modified":"2026-05-06T09:20:26","modified_gmt":"2026-05-06T09:20:26","slug":"constructors","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=constructors","title":{"rendered":"Constructors"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Constructors in JavaScript are special functions used to create and initialize objects. They help developers generate multiple objects with similar structure and behavior, making code more organized and reusable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Constructor<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A constructor is a function that is used with the keyword new to create an object. It defines properties and methods that will belong to each object created from it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Constructors<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Constructors allow you to create multiple objects efficiently<br>They reduce code repetition<br>They make your code more structured and easier to maintain<br>They are useful when working with object oriented programming concepts<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Constructor<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A constructor function is written like a regular function but follows a naming convention where the first letter is capitalized.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Person(name, age) {<br>  this.name = name;<br>  this.age = age;<br>}<br><br>const person1 = new Person(\"Ali\", 20);<br>const person2 = new Person(\"Sara\", 25);<br><br>console.log(person1.name);<br>console.log(person2.age);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Constructors Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you use the new keyword<br>A new empty object is created<br>The constructor function is executed<br>The this keyword refers to the new object<br>The object is returned automatically<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Adding Methods in Constructors<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can also define methods inside constructors<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Car(brand, model) {<br>  this.brand = brand;<br>  this.model = model;<br><br>  this.displayInfo = function() {<br>    console.log(this.brand + \" \" + this.model);<br>  };<br>}<br><br>const car1 = new Car(\"Toyota\", \"Corolla\");<br>car1.displayInfo();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Always start constructor names with a capital letter<br>Use constructors when creating multiple similar objects<br>Avoid defining methods inside constructors for large projects instead use prototypes<br>Keep constructors simple and focused<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Constructors vs Classes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">JavaScript also provides classes which are a modern way to create objects. Constructors are still important because classes are built on top of constructor functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Constructors are a fundamental concept in JavaScript that help developers create reusable and scalable code. Understanding constructors is essential for mastering object oriented programming in JavaScript.<\/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 > Constructors<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1778059199931\"><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-1778059199687\"><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-162","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>Constructors - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn JavaScript constructors to create objects efficiently and write reusable code with simple examples for beginners\" \/>\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=constructors\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Constructors - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn JavaScript constructors to create objects efficiently and write reusable code with simple examples for beginners\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=constructors\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-06T09:20:26+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=constructors\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=constructors\",\"name\":\"Constructors - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-06T09:20:25+00:00\",\"dateModified\":\"2026-05-06T09:20:26+00:00\",\"description\":\"Learn JavaScript constructors to create objects efficiently and write reusable code with simple examples for beginners\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=constructors#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=constructors\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=constructors#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Professional JavaScript > OOP in JavaScript > Constructors\"}]},{\"@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":"Constructors - Learn JavaScript with GIGZ.PK","description":"Learn JavaScript constructors to create objects efficiently and write reusable code with simple examples for beginners","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=constructors","og_locale":"en_US","og_type":"article","og_title":"Constructors - Learn JavaScript with GIGZ.PK","og_description":"Learn JavaScript constructors to create objects efficiently and write reusable code with simple examples for beginners","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=constructors","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-06T09:20:26+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=constructors","url":"https:\/\/gigz.pk\/javascript\/?lesson=constructors","name":"Constructors - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-06T09:20:25+00:00","dateModified":"2026-05-06T09:20:26+00:00","description":"Learn JavaScript constructors to create objects efficiently and write reusable code with simple examples for beginners","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=constructors#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=constructors"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=constructors#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"Professional JavaScript > OOP in JavaScript > Constructors"}]},{"@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\/162","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=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}