In JavaScript, objects are king. If you understand objects, you understand JavaScript. All JavaScript values (except primitives) are objects.

JavaScript Primitives:
A primitive value is a value that has no properties or methods.
A primitive data type is a data that has a primitive value.
JavaScript defines 5 types of primitive data types:
- string
- number
- boolean
- null
- undefined
Remember: Do Not Declare Strings, Numbers, and Booleans as Objects!
Real-Life Objects, Properties, and Methods
In real life, a car is an object.
A car has properties like weight and colour, and methods like start and stop:
Object | Properties | Methods |
Mini | car.name = Mini | car.start() |
![]() | car.model = 3doors | car.drive() |
car.weight = 900kg | car.brake() | |
car.color = red | 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.
JavaScript Objects
You have already learned that JavaScript variables are containers for data values (to read more about JavaScript Variables click here).
This code assigns a simple value (Mini) to a variable named car:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo"></p>
<script>
var car = "Mini";
document.getElementById("demo").innerHTML = car;
</script>
</body>
</html>
Output:

Objects are variables too. But objects can contain many values.
This code assigns many values (Mini, 3doors, red) to a variable named car:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Objects</h2>
<p id="demo"></p>
<script>
var car = {type:"Mini", model:"3doors", color:"red"};
document.getElementById("demo").innerHTML = "The car type is " + car.type;
</script>
</body>
</html>
Output:

The values are written as name:value pairs (name and value separated by a colon).
JavaScript objects are containers for named values called properties or methods.
Object Definition
You define (and create) a JavaScript object with an object literal:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Objects</h2>
<p id="demo"></p>
<script>
var person = {firstName:"Adam", lastName:"Smith", age:48, eyeColor:"green"};
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>
</body>
</html>
Output:

Object Properties
The name:values pairs in JavaScript objects are called properties:
Property | Property Value |
firstName | Adam |
lastName | Smith |
age | 48 |
eyeColor | green |
Object Methods
Objects can also have methods.
Methods are actions that can be performed on objects.
Methods are stored in properties as function definitions.
Property | Property Value |
firstName | Adam |
lastName | Smith |
age | 48 |
eyeColor | green |
fullName | function() {return this.firstName + ” ” + this.lastName;} |
Example:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Objects</h2>
<p>An object method is a function definition, stored as a property value.</p>
<p id="demo"></p>
<script>
var person = {
firstName: "Adam",
lastName : "Smith",
id : 1122,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
document.getElementById("demo").innerHTML = person.fullName();
</script>
</body>
</html>
Output:

The this Keyword
In a function definition, this refers to the “owner” of the function.
In the example above, this is the person object that “owns” the fullName function.
In other words, this.firstName means the first.Name property of this object.
Enjoy coding!
Read also: