{"id":1566,"date":"2016-07-29T12:57:27","date_gmt":"2016-07-29T12:57:27","guid":{"rendered":"http:\/\/javascript-tutor.net\/?page_id=1566"},"modified":"2017-11-16T23:14:32","modified_gmt":"2017-11-16T23:14:32","slug":"creating-objects-javascript","status":"publish","type":"page","link":"https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/","title":{"rendered":"JavaScript Lesson 33: Creating an Object Using Literal Notation"},"content":{"rendered":"<h3 style=\"text-align: center;\"><strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/\">&lt;Previous Lesson&gt;<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Table of Contents]<\/a><a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/\">\u00a0&lt;Next Lesson&gt;<\/a><\/strong><\/h3>\n<h2>Introduction<\/h2>\n<p>An object is something that has properties and methods. For example, a car has properties like color, model, and methods such as start, accelerate and stop.<\/p>\n<p>An object in JavaScript also has properties and methods. A JavaScript object actually comprises variables and functions. A variable inside an object is called a property and a function inside an object is called a method.<\/p>\n<h2>Creating an Object<\/h2>\n<p>There are two\u00a0ways to create a JavaScript object, namely using literal notation and using object constructor notation<\/p>\n<h3>1. Using Literal Notation<\/h3>\n<p>The syntax to create an object is<\/p>\n<pre style=\"font-size: 100%;\">var objectName={\r\n\r\nproperty1=\"=value1,\r\nproperty2=\"=value2,\r\n..................\r\n.................\r\nproeprtyn=valuen,\r\nMyMethod: MyFunction(){ }\r\n\r\n};\r\n<\/pre>\n<h4>Example 1.1<\/h4>\n<pre style=\"font-size: 100%;\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;body&gt;\r\n\r\n&lt;p&gt;The Phone Object.&lt;\/p&gt;\r\n\r\n&lt;h3&gt;Model:&lt;b id=\"model\"&gt;&lt;\/b&gt;&lt;\/h3&gt;\r\n&lt;h3&gt; Brand:&lt;b id=\"brand\"&gt;&lt;\/b&gt;&lt;\/h3&gt;\r\n&lt;h3&gt;Price:&lt;b id=\"price\"&gt;&lt;\/b&gt;&lt;\/h3&gt;\r\n\r\n&lt;script&gt;\r\nvar phone= {model:\"Note8\", brand:\"Samsung\", color:\"white\",price:\"$2000\"};\r\ndocument.getElementById(\"model\").innerHTML = phone.model;\r\ndocument.getElementById(\"brand\").innerHTML = phone.brand;\r\ndocument.getElementById(\"price\").innerHTML = phone.price;\r\n&lt;\/script&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>The output<\/p>\n<p>The Phone Object.<\/p>\n<h3>Model:<b id=\"model\"><\/b><\/h3>\n<h3>Brand:<b id=\"brand\"><\/b><\/h3>\n<h3>Price:<b id=\"price\"><\/b><\/h3>\n<p><script>var phone= {model:\"Note8\", brand:\"Samsung\", color:\"white\",price:\"$2000\"}; document.getElementById(\"model\").innerHTML = phone.model; document.getElementById(\"brand\").innerHTML = phone.brand; document.getElementById(\"price\").innerHTML = phone.price; \n<\/script><\/p>\n<p>&nbsp;<\/p>\n<p>The following example comprises a function:<\/p>\n<h4>Example 1.2<\/h4>\n<pre style=\"font-size: 100%;\">var student={\r\nname:'George',\r\nheight:1.78,\r\nweight:75,\r\nbmi: function(){\r\nvar x=this.weight\/Math.pow(this.height,2);\r\nvar y=x.toFixed(2);\r\nreturn y;\r\n}\r\n\r\n};\r\nvar student_Name=document.getElementById('studentname');\r\nstudent_Name.textContent=student.name;\r\nvar student_bmi=document.getElementById('bmi_index');\r\nstudent_bmi.textContent=student.bmi();\r\n<\/pre>\n<p>The object is student that comprise properties name, height, and weight. Its method is bmi that calculates the bmi index of a particular student. Notice that each property comprises\u00a0<strong>name:value<\/strong> pairs and must be separated by a comma.<\/p>\n<p>&nbsp;<\/p>\n<p>The output<\/p>\n<h3>Student Name:<b id=\"studentname\"><\/b><\/h3>\n<h3>BMI : <b id=\"bmi_index\"><\/b><\/h3>\n<p><script>\nvar student={ name:\"George\", height:1.80, weight:80, bmi: function(){ var x=this.weight\/Math.pow(this.height,2); var y=x.toFixed(2); return y; } }; var student_Name=document.getElementById(\"studentname\"); student_Name.textContent=student.name; var student_bmi=document.getElementById(\"bmi_index\"); student_bmi.textContent=student.bmi(); <\/script><br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<!-- Javascript Responsive Ads --><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-3033628290023372\"\n     data-ad-slot=\"9005101105\"\n     data-ad-format=\"auto\"><\/ins><br \/>\n<script>\n(adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3 style=\"text-align: center;\"><strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/\">&lt;Previous Lesson&gt;<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Table of Contents]<\/a><a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-lesson-34-creating-object-using-constructor-notation\/\">\u00a0&lt;Next Lesson&gt;<\/a><\/strong><\/h3>\n<p>&#8220;&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;Previous Lesson&gt;\u00a0[Table of Contents]\u00a0&lt;Next Lesson&gt; Introduction An object is something that has properties and methods. For example, a car has properties like color, model, and methods such as start, accelerate and stop. An object in JavaScript also has properties and methods. A JavaScript object actually comprises variables and functions. A variable inside an object is &hellip; <a href=\"https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JavaScript Lesson 33: Creating an Object Using Literal Notation&#8221;<\/span><\/a><\/p>\n","protected":false},"author":35895,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1566","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Lesson 33: Creating an Object Using Literal Notation - Learn JavaScript Online | Free Interactive JavaScript Tutorials<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Lesson 33: Creating an Object Using Literal Notation - Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"og:description\" content=\"&lt;Previous Lesson&gt;\u00a0[Table of Contents]\u00a0&lt;Next Lesson&gt; Introduction An object is something that has properties and methods. For example, a car has properties like color, model, and methods such as start, accelerate and stop. An object in JavaScript also has properties and methods. A JavaScript object actually comprises variables and functions. A variable inside an object is &hellip; Continue reading &quot;JavaScript Lesson 33: Creating an Object Using Literal Notation&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-16T23:14:32+00:00\" \/>\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\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/\",\"url\":\"https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/\",\"name\":\"JavaScript Lesson 33: Creating an Object Using Literal Notation - Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/javascript-tutor.net\/#website\"},\"datePublished\":\"2016-07-29T12:57:27+00:00\",\"dateModified\":\"2017-11-16T23:14:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/javascript-tutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Lesson 33: Creating an Object Using Literal Notation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/javascript-tutor.net\/#website\",\"url\":\"https:\/\/javascript-tutor.net\/\",\"name\":\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"description\":\"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Lesson 33: Creating an Object Using Literal Notation - Learn JavaScript Online | Free Interactive JavaScript Tutorials","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:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Lesson 33: Creating an Object Using Literal Notation - Learn JavaScript Online | Free Interactive JavaScript Tutorials","og_description":"&lt;Previous Lesson&gt;\u00a0[Table of Contents]\u00a0&lt;Next Lesson&gt; Introduction An object is something that has properties and methods. For example, a car has properties like color, model, and methods such as start, accelerate and stop. An object in JavaScript also has properties and methods. A JavaScript object actually comprises variables and functions. A variable inside an object is &hellip; Continue reading \"JavaScript Lesson 33: Creating an Object Using Literal Notation\"","og_url":"https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/","og_site_name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","article_modified_time":"2017-11-16T23:14:32+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/","url":"https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/","name":"JavaScript Lesson 33: Creating an Object Using Literal Notation - Learn JavaScript Online | Free Interactive JavaScript Tutorials","isPartOf":{"@id":"https:\/\/javascript-tutor.net\/#website"},"datePublished":"2016-07-29T12:57:27+00:00","dateModified":"2017-11-16T23:14:32+00:00","breadcrumb":{"@id":"https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/javascript-tutor.net\/"},{"@type":"ListItem","position":2,"name":"JavaScript Lesson 33: Creating an Object Using Literal Notation"}]},{"@type":"WebSite","@id":"https:\/\/javascript-tutor.net\/#website","url":"https:\/\/javascript-tutor.net\/","name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/users\/35895"}],"replies":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=1566"}],"version-history":[{"count":21,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1566\/revisions"}],"predecessor-version":[{"id":2511,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1566\/revisions\/2511"}],"wp:attachment":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=1566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}