This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
Object | Properties | Methods |
---|---|---|
car.name = Fiat car.model = 500 car.weight = 850kg car.color = white |
car.start() car.drive() car.brake() car.stop() |
let car = "Fiat";
const car = {type:"Fiat", model:"500", color:"white"};
The values are written as name:value pairs (name and value separated by a colon).
It is a common practice to declare objects with the const keyword.
You define (and create) a JavaScript object with an object literal:
const person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
Spaces and line breaks are not important. An object definition can span multiple lines:
const person = { firstName: "John", lastName: "Doe", age: 50, eyeColor: "blue" };
The name:values pairs in JavaScript objects are called properties:
Property | Property Value |
---|---|
firstName | John |
lastName | Doe |
age | 50 |
eyeColor | blue |
You can access object properties in two ways:
objectName.propertyName
or
objectName["propertyName"]
person.lastName;
person["lastName"];
JavaScript objects are containers for named values called properties.
Property | Property Value |
---|---|
firstName | John |
lastName | Doe |
age | 50 |
eyeColor | blue |
fullName | function() {return this.firstName + " " + this.lastName;} |
A method is a function stored as a property.
const person = { firstName: "John", lastName : "Doe", id : 5566, fullName : function() { return this.firstName + " " + this.lastName; } };
In an object method, | refers to the object.
Alone, | refers to the global object.
In a function, | refers to the global object.
In a function, in strict mode, | is .
In an event, | refers to the element that received the event.
Methods like | , , and can refer to any object.
Note
is not a variable. It is a keyword. You cannot change the value of .You access an object method with the following syntax:
objectName.methodName()
name = person.fullName();
If you access a method without the () parentheses, it will return the function definition:
name = person.fullName;
When a JavaScript variable is declared with the keyword "
", the variable is created as an object:x = new String(); // Declares x as a String object y = new Number(); // Declares y as a Number object z = new Boolean(); // Declares z as a Boolean object
Avoid
, , and objects. They complicate your code and slow down execution speed.You will learn more about objects later in this tutorial.
Eventually the navigation links, above, will be replaced by these (previous) and (next) buttons below.
JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon