JavaScript Objects

JavaScript Objects
JavaScript Objects

JavaScript Objects

JavaScript Objects are non-primitive data type. Unlike variables, object holds multiple values in terms of properties and methods. Here Properties can hold values of primitive data types and methods are JavaScript functions. Examples of JavaScript objects are – Student, pen, Board, chair, glass, keyboard etc.

JavaScript is an object-based language. JavaScript treats everything as an object .

JavaScript doesn’t follow all the OOPs concepts , hence it is not class based but is template based. So, you don’t need to create class to get the object. But, direct objects are created.

How to Create Objects in JavaScript

JavaScript Objects can be created in 3 different ways-

  1. By object literal
  2. using new keyword, By creating instance of Object directly
  3. Using an object constructor with new keyword

1) Creating JavaScript Objects by object literal

Using object literal following syntax is used to  create an object:

object={property1:value1,property2:value2…..propertyN:valueN}

Here : (colon) is used to separate property and value when creating an object . These properties and values can be accessed by object using (.) dot operator.

Example

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
emp={id:102,name:"Shyam Kumar",salary:40000}  
document.write(emp.id+" "+emp.name+" "+emp.salary);  
</script>
</body>
</html>

2) Creating JavaScript Objects by instance of Object

The new keyword is used to create object directly as shown below:

var obj1=new Object();

Example

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var emp=new Object();  
emp.id=101;  
emp.name="Ram";  
emp.salary=50000;  
document.write(emp.id+" "+emp.name+" "+emp.salary);  
</script>
</body>
</html>

 

3) Creating JavaScript Objects By using an Object constructor

To create JavaScript objects by using object constructor a function with arguments needs to be defined. Each argument value of function is assigned to the current object by using this keyword.

This keyword means the current object.

Example

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
function emp(id,name,salary){  
this.id=id;  
this.name=name;  
this.salary=salary;  
}  
e=new emp(103,"Vimal Jaiswal",30000);  
document.write(e.id+" "+e.name+" "+e.salary);  
</script> 
</body>
</html>

Defining method in JavaScript Objects

A method can be defined in JavaScript objects. But before doing so  a property needs to be added to the function with same name as method.

Example

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
function emp(id,name,salary){  
this.id=id;  
this.name=name;  
this.salary=salary;  
this.changeSalary=changeSalary;  
function changeSalary(otherSalary){  
this.salary=otherSalary;  
}  
}  
e=new emp(103,"Ram Kumar",30000);  
document.write(e.id+" "+e.name+" "+e.salary);  
e.changeSalary(45000);  
document.write("<br>"+e.id+" "+e.name+" "+e.salary);  
</script>  
</body>
</html>

JavaScript Objects Methods

The various methods of Object are as follows:

JavaScript Array

JavaScript array is a collection of similar type of elements. JavaScript arrays is also an Object. JavaScript Arrays can save more than one elements under one array name, which differs only by its index or subscript. The index starts from 0 to size -1 of a JavaScript array. For example

var num=[5,9,3,6,7];

Here to address first number num[0]=5; second as num[1]=9; and so on.

JavaScript Arrays can be defined in 3 different ways-

  1. By array literal
  2. Creating instance of Array directly with new keyword
  3. Using an Array constructor using with keyword

1) JavaScript array using literal

This is the simplest way to create an array, all the values of array are enclosed within [] separated by comma. It’s syntax is as under :

var arrayname=[val1,val2…..valN];

Example

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var emp=["Ram","Sham","Ratan"];  
for (i=0;i<emp.length;i++){  
document.write(emp[i] + "<br/>");  
}  
</script>
</body>
</html>

The arrayname.length property returns the length of an array.

2) JavaScript Array directly using (new keyword)

This way an instance of an array is created and values are assigned to it using array index, which starts as ArrayName[0] to indicate the first element in the array. For loop is used to access the elements of an array. The syntax is as under:

var arrayname=new Array();

Example

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var i;  
var emp = new Array();  
emp[0] = "Arun";  
emp[1] = "Varun";  
emp[2] = "John";  
for (i=0;i<emp.length;i++){  
document.write(emp[i] + "<br>");  
}  
</script>
</body>
</html>

3) JavaScript array as an Array constructor using new keyword

JavaScript Array can be created using an instance of array by passing arguments in constructor which removes the values to be provided explicitly.

Example

<!DOCTYPE html>
<html>
<head> <title>test </title>
<head>
<body>
<script>  
var emp=new Array("Jai","Vijay","Smith");  
for (i=0;i<emp.length;i++){  
document.write(emp[i] + "<br>");  
}  
</script>  
</body>
</html>

JavaScript Array Methods

JavaScript array methods with their description.

Methods Description
concat() returns a new array object which contains two or more merged arrays.
copywithin() It copies the part of the given array with its own elements and returns the modified array.
entries() It creates an iterator object and a loop that iterates over each key/value pair.
every() It determines whether all the elements of an array are satisfying the provided function conditions.
flat() It creates a new array carrying sub-array elements concatenated recursively till the specified depth.
flatMap() It maps all array elements via mapping function, then flattens the result into a new array.
fill() It fills elements into an array with static values.
from() It creates a new array carrying the exact copy of another array element.
filter() It returns the new array containing the elements that pass the provided function conditions.
find() It returns the value of the first element in the given array that satisfies the specified condition.
findIndex() It returns the index value of the first element in the given array that satisfies the specified condition.
forEach() It invokes the provided function once for each element of an array.
includes() It checks whether the given array contains the specified element.
indexOf() It searches the specified element in the given array and returns the index of the first match.
isArray() It tests if the passed value ia an array.
join() It joins the elements of an array as a string.
keys() It creates an iterator object that contains only the keys of the array, then loops through these keys.
lastIndexOf() It searches the specified element in the given array and returns the index of the last match.
map() It calls the specified function for every array element and returns the new array
of() It creates a new array from a variable number of arguments, holding any type of argument.
pop() It removes and returns the last element of an array.
push() It adds one or more elements to the end of an array.
reverse() It reverses the elements of given array.
reduce(function, initial) It executes a provided function for each value from left to right and reduces the array to a single value.
reduceRight() It executes a provided function for each value from right to left and reduces the array to a single value.
some() It determines if any element of the array passes the test of the implemented function.
shift() Used to remove and return the first element of an array.
slice() It returns a new array containing the copy of the part of the given array.
sort() It returns the element of the given array in a sorted order.
splice() It add/remove elements to/from the given array.
toLocaleString() It returns a string containing all the elements of a specified array.
toString() It converts the elements of a specified array into string form, without affecting the original array.
unshift() It adds one or more elements in the beginning of the given array.
values() It creates a new iterator object carrying values for each index in the array.

If you find the content on JavaScript Objects useful pl do like, share and Comment 

 

To visit the  Official website of JavaScript Click here

 

Similar Posts:  JavaScript Variable and its Scope,        How To Enable JavaScript in Browsers,    

                           Where to write JavaScript Code ,         How to Generate Output in JavaScript

What are JavaScript Objects ?

JavaScript Object is a non-primitive data type. Unlike variables, object holds multiple values in terms of properties and methods. Here Properties can hold values of primitive data types and methods are JavaScript functions. Examples of JavaScript object are – Student, pen, Board, chair, glass, keyboard etc.

What are JavaScript Arrays

JavaScript array is a collection of similar type of elements. JavaScript arrays is also an Object. JavaScript Arrays can save more than one elements under one array name, which differs only by its index or subscript. The index starts from 0 to size -1 of a JavaScript array. For example
var num=[5,9,3,6,7];
Here to address first number num[0]=5; second as num[1]=9; and so on.

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback
1 year ago

[…] posts : JavaScript Objects ,    JavaScript Functions and Loops    JavaScript Control Statements and Operators  Data Types […]

Please Subscribe to Stay Connected

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

DigitalSanjiv will use the information you provide on this form to be in touch with you and to provide updates and marketing.
Share via
Copy link
Powered by Social Snap
%d bloggers like this: