Home » JavaScript Tutorial

Objects in JavaScript

In this article, we are going to learn what is object in JavaScript? Methods in Objects and the functions that take objects.
Submitted by Himanshu Bhatt, on September 04, 2018

An object is a pool of properties, where a property is a link between a name (key) and a value. A property's value can also be a function, then the property is called the method.

Let’s begin with creating an Object.

let object = {
	member:'IncludeHelp',
	type: 'Admin'
}
console.log(object)
console.log(object.member,'is the',object.attib);

Remember: Here the name of the object is Object and contains two properties member and type. Important to note here that all properties will be separated by a comma (,).

The output of the above program will:

Js output file 4

Now we see how to create a method.

let user = {
	name: 'Adam',
	type: 'Guest',
	changeType:function(type){
			this.type= type
		}
}

console.log(user)
user.changeType('RegisterUser')
console.log(user);

Here we created a method to the user object which changes the type of user. Here is this snippets output:

Js output file 5

We learned two things how to create an object and methods in objects now we will discuss how a function can be used to manipulate an object, let’s see in next code snippet.

let myArticle = {
	articleName: 'Objects in Javascript',
	author: 'Himanshu',
	publisher: 'IncludeHelp',
	wordCount: 700
}

let myAnotherArticle = {
	articleName: 'Javascript Introduction',
	author: 'HB',
	publisher: 'Ih',
	wordCount: 500
}

let increasesWordCount = function(object){
	object.wordCount =object.wordCount + 200
}

increasesWordCount(myArticle)
console.log(myArticle);
increasesWordCount(myAnotherArticle)
console.log(myAnotherArticle);

Above, we created two objects namely myArticle and myAnotherArticle. Both contain similar properties that are articleName, author, publisher and wordCount. We also have an increaseWordCount function which takes an object as its argument and increment in its wordcount by 200.

Let’s see what’s in its output:

Js output file 6

We created two objects, myArticle and myAnotherArticle with wordCount 700 and 500 respectively, after calling increasesWordCount both objects' wordCount went to 900 and 700 respectively ( see output).

In the function we created, increasesWordCount we put the object as a parameter and we change its property’s value by accessing using the dot operator.

Objects can be used for checking password strength, a great implementation is provided on IncludeHelp for the same: Password strength checker in JavaScript





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.