Javascript Objects

  • By
  • November 1, 2022
  • JavaScript
Javascript objects

Javascript Objects

Javascript is an object-based scripting language. A javascript object has different properties and methods. An object groups data and functions needed to manipulate it. Properties and methods of objects are accessed with ‘.’ operator. Javascript uses two types of objects:

  1. Built-in-objects:- Following objects are built-in objects:

Math, String, Array, Date etc.

  1. User defined objects:- The user creates these.

Built-in-objects:- These objects are having different properties and methods that are useful while creating web pages. These objects are as follows:

String Object:- It is used to store characters within single or double quotes. A string object is used to store and manipulate text. It has the following properties and methods:-

Properties:-

It has only the length property of a string object.

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

Methods:-

charAt(), indexOf(), lastIndexOf(), substr(), substring(), trim(), toLowerCase(), toUpperCase() are the methods of string object which are used to perform different operations on string.

e.g. var a=”  Sevenmentor Pvt. Ltd.  “;

        document.write(“Length of string is:   ” +  a.length + “<br>”);

        document.write(“Substring is:    ” +  a.substr(12,7) + “<br>”);

        document.write(“Substring is:    ” +  a.substring(10,15) + “<br>”);

        document.write(“Trimmed string is:    ” +  a.trim() + “<br>”);

        document.write(“Character at particular position is:    ” +  a.charAt(6) + “<br>”);

        document.write(“Index of string is:    ” +  a.indexOf(“e”) + “<br>”);

        document.write(“Last Index of string is:    “+  a.lastIndexOf(“t”) + “<br>”);

        document.write(“String is uppercase letters: ” + a.toUpperCase() + “<br>”);

        document.write(“String is lowercase letters: ” + a.toLowerCase() + “<br>”);

Above code describes all the methods of string object.

Math Object:- It includes mathematical constants and functions. There is no need to create the math object before using it. 

Math object has following methods:

abs(x), cbrt(x), ceil(x), floor(x), max(x,y,…), min(x,y,…), pow(x,y), sqrt(x)

are the methods of math object which are used to perform different operations on numeric data.

e.g. var x=23.6758;

        document.write(“Ceil method: ” + Math.ceil(x) + “<br>”);

        document.write(“Floor method: ” + Math.floor(x) + “<br>”);

        document.write(“Abs method: ” + Math.abs(-45) + “<br>”);

        document.write(“Cube root method: ” + Math.cbrt(27) + “<br>”);

        document.write(“Square root method: ” + Math.sqrt(81) + “<br>”);

        document.write(“Power method: ” + Math.pow(4,5) + “<br>”);

        document.write(“Min method: ” + Math.min(23,56,78,12,9,8) + “<br>”);

        document.write(“Max method: ” + Math.max(23,56,78,12,9,8) + “<br>”);

Above code describes all the methods of math object.

Date object:- It is used to create date and time values. It is created using ‘new’ keyword. 

Date object has following methods:

getDate(), getDay(), getFullYear(), getHours(), getMinutes(), getMonth(), getSeconds(), getTime(), now(), setDate(), setFullYear(), setHours(), setMinutes(), setMonth(), setSeconds(), setTime() are the methods of date object which are used to set and get the different values of date and time.

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

e.g.

var d;

        d=new Date();

        document.write(“Today’s date is:” + d.getDate() + “<br>”);

        document.write(“Today’s day is:” + d.getDay() + “<br>”);

        document.write(“Today’s Full year is:” + d.getFullYear() + “<br>”);

        document.write(“Current Hour is:” + d.getHours() + “<br>”);

        document.write(“Minutes are:” + d.getMinutes() + “<br>”);

        document.write(“Current Month is:” + d.getMonth() + “<br>”);

        document.write(“Seconds are:” + d.getSeconds() + “<br>”);

        document.write(“Current time is:” + d.getTime() + “<br>”);

        d.setDate(15);

        document.write(d + “<br>”);

        d.setFullYear(1990);

        document.write(d + “<br>”);

        d.setHours(20);

        document.write(d + “<br>”);

        d.setMinutes(50);

        document.write(d + “<br>”);

        d.setMonth(5);

        document.write(d + “<br>”);

        d.setSeconds(45);

        document.write(d + “<br>”);

        d.setTime(10000000);

        document.write(d + “<br>”);

Number Object:- It has following properties and methods:

Properties:-

MIN_VALUE and MAX_VALUE.

Methods:-

isNaN(), isInteger(), parseFloat(), parseInt(), isFixed().

Array object:- It can store a number of items of same datatype. Arrays become very useful when you need to store large amounts of data of same type. You can create an array in javascript as following:

var vege = [“Tomato”, ”Potato”, ”Drum Sticks”, ”Cabbage”];

or

var vege = new Array(“Tomato”, ”Potato”, ”Drum Sticks”, ”Cabbage”);

You can access and set items in an array by using its index number and the index of the first element of an array are zero. arrayname[0] is the first element, arrayname[1] is the second element and so on. Start Learning with SevenMentor’s  Java Classes in Pune

e.g. var vegename = vege[0];

document.getElementById(“demo”).innerHTML = vege[1];

Array object has the following properties and methods:

Properties:- Index and length are properties of array object.

Methods:-

concat(), copyWithin(), find(), forEach(), indexOf(), isArray(), pop(), push(), reverse(), sort() are methods of array object.

e.g.

var fruits = [“Apple”, “Banana”, “Orange”, “Mango”];

        var vege = [“Tomato”, “Capsicum”, “Drum sticks”];

        var qty = [10,45,23,67,89];

        var e = qty.find(f);

        function f(q)

        {

            if(q>40)

            {

                document.write(q + “<br>”);

            }

        }

        vege.forEach(fe);

        function fe(item,index)

        {

            document.write(index + “:” + item + “<br>”);

        }

        var c = fruits.concat(vege);

        var a = fruits.copyWithin(1,2);

        document.write(“Length of fruits array is:” + fruits.length + “<br>”);

        document.write(“Length of vege array is:” + vege.length + “<br>”);

        document.write(“Length of qty array is:” + qty.length + “<br>”);

        document.write(“Concatenated array is:” + c + “<br>”);

        document.write(“Copied array is:” + a + “<br>”);

        var I = fruits.indexOf(“Orange”);

        document.write(i + “<br>”);

        fruits.pop();

        document.write(fruits + “<br>”);

        fruits.push(“Grapes”);

        document.write(fruits + “<br>”);

        document.write(fruits.reverse() + “<br>”);

        document.write(fruits.sort() + “<br>”);

        document.write(fruits.reverse() + “<br>”);

User-defined objects in Javascript:-

User can define their own objects in javascript. User-defined objects contain multiple values. Those values can be accessed using dot(.) operator.

e.g. cons employee = {

id: 1,

name: ”ABC”,

city: ”Pune”,

salary: 50000

}

Display these values using following statements:

document.write(“Employee Name:” + employee.name + ”<br>” + ”Employee Salary:” + employee.salary);

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

You can create array of objects also. 

e.g. const employee = [{

            id: 1,

            name: “ABC”,

            city: “Pune”,

            salary: 50000

        },

       {

        id: 2,

        name: “PQR”,

        city: “Mumbai”,

        salary: 40000

       }

    ]

    for(let i=0; i<employee.length; i++)

    {

        document.write(“Employee ID:” + employee[i].id + “<br>”);

        document.write(“Employee Name:” + employee[i].name + “<br>”);

        document.write(“Employee Salary:” + employee[i].salary + “<br>”);

    }

 

Author:

Sarika Ganesh Kore
Call the Trainer and Book your free demo Class  Call now!!!

| SevenMentor Pvt Ltd.

© Copyright 2021 | Sevenmentor Pvt Ltd.

 

Submit Comment

Your email address will not be published. Required fields are marked *

*
*