How are objects created in arrays

In real life, a car is an object.A car has properties like weight and color, and methods like start and stop:car.name = Fiatcar.model = 500car.weight = 850kgcar.color = whitecar.start()car.drive()car.brake() car.stop()All cars have the same properties, but the property values differ from car to car. All cars have the same methods, but the methods are performed at different times.You have already learned that JavaScript variables are containers for data values. This code assigns a simple value (Fiat) to a variable named car: var car = "Fiat";Objects are variables too. But objects can contain many values.This code assigns many values (Fiat, 500, white) to a variable named car:var car = {type:"Fiat", model:"500", color:"white"};The values are written as name:value pairs (name and value separated by a colon).var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};Spaces and line breaks are not important. An object definition can span multiple lines:var person = { firstName: "John", lastName: "Doe",  age: 50,  eyeColor: "blue"};You can access object properties in two ways:objectName.propertyName or person.lastName;Below you can see a object being created and and a function being created which calls out the object's properties.var person = { firstName: "John", lastName : "Doe", id      : 5566, fullName : function() {    return this.firstName + " " + this.lastName;  }};

Related Javascript Mentoring answers

All answers ▸

How do you create objects in javascript?


What does the document.onload() function do and why is it useful?


What does the following code do and why? console.log(1+"2")


How to check if value exists in an object in JavaScript?


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2024

Terms & Conditions|Privacy Policy