JavaScript – Objects


What is an object ?

An object is a person or a thing to which a specifies action or feeling.

What is an object in Javascript ?

An object in javascript has a state and behaviour.

The behaviour may be of properties and methods. ( In Javascript everything is an object )

In Java we create a Class to get Objects, But Javascript is not a class based language, so we create Objects directly.

What do properties look like in javascript ?

The properties in javascript will be in the form of key-value pairs.

Keys are referred to as properties of the object.

What are the Primitive data types in Javascript ?

1. Number,

2. String,

3. Boolean,

4. null,

5. undefined


6. symbol

An Object may contain any combination of these primitive data types as well as reference data types.

The Key and Values may be in any of these data types.

Javascript interview questions basic !!!

Click to Learn More about – Javascript online learning

How to create an object in javascript ?

Syntax :

var obj = {
    key1 : value1,
    key2 : value2,
};

Javascript Object creation

There are three ways of creating javascript objects, they are

1. By literal base.

2. By instance of Object.

3. By object constructor.

1. By Object literal

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var obj = {
					name:"Abdul",
					age:87,
					sex:"Male",
					place:"TamilNadu"
				  };
			document.write("Name = "+obj.name+"<br/>");
			document.write("Age = "+obj.age+"<br/>");
			document.write("Sex = "+obj.sex+"<br/>");
			document.write("Location = "+obj.place+"<br/>");
		
		</script>
	</head>
	<body></body>
</html>

Output :

Name = Abdul

Age = 87

Sex = Male

Location = TamilNadu

2. By instance of Object.

This is done by using “new” keyword.

Example :

<!DOCTYPE html>
<html>
        <head>
                <title>Our title</title>
                <script type="text/javascript">
                        var obj = new Object(); // By using new keywork
                        obj.name = "Abdul";
                        obj.age = 87;
                        obj.sex = "Male";
                        obj.place = "TamilNadu";
                        document.write("Name = "+obj.name+"<br/>");
                        document.write("Age = "+obj.age+"<br/>");
                        document.write("Sex = "+obj.sex+"<br/>");
                        document.write("Location = "+obj.place+"<br/>");
                </script>
        </head>
        <body></body>
</html>

Output :

Name = Abdul

Age = 87

Sex = Male

Location = TamilNadu


3. By object constructor.

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var obj = new doSomething("Abdul",87,"Male","Tamilnadu");
			function doSomething(name,age,sex,place){
				this.name = name;
				this.age = age;	
				this.sex = sex;	
				this.place = place;
			}
			document.write("Name = " + obj.name+ "<br/>");
			document.write("Age = " + obj.age+ "<br/>");
			document.write("Sex = " + obj.sex+ "<br/>");
			document.write("Location = " + obj.place+ "<br/>");
		</script>
	</head>
	<body></body>
</html>

Output :

Name = Abdul

Age = 87

Sex = Male

Location = Tamilnadu

4. Methods inside Javascript Objects.

Javascript allows us to define methods inside object. Some of the methods are listed below,

SnoObject NameDescription
1Object.assign()This is used to copy enumerable and own properties from a source object to a target object.
2Object.create()This is used to create a new object with the specified prototype object and properties.
3Object.defineProperty()This is used to describe some behavioural attributes of the property.
4Object.defineProperties()This is used to create or configure multiple object properties.
5Object.entries()This returns an array with arrays of the key, value pairs.
6Object.freeze()This prevents existing properties from being removed.
7Object.getOwnPropertyDescriptor()This returns a property descriptor for the specified property of the specified object.
8Object.getOwnPropertyDescriptors()This returns all own property descriptors of a given object.
9Object.getOwnPropertyNames()This returns an array of all properties (enumerable or not) found.
10Object.getOwnPropertySymbols()This returns an array of all own symbol key properties.
11.Object.getPrototypeOf()This returns the prototype of the specified object.
12Object.is()This determines whether two values are the same value.
13Object.isExtensible()This determines if an object is extensible
14Object.isFrozen()This determines if an object is frozen.
15Object.isSealed()This determines if an object is sealed.
16Object.keys()This returns an array of a given object’s own property names.
17Object.preventExtensions()This is used to prevent any extensions of an object.
18Object.seal()This prevents new properties from being added and marks all existing properties as non-configurable.
19Object.setPrototypeOf()This sets the prototype of a specified object to another object.
20Object.values()This method returns an array of values.