Objects in JavaScript - Programming: Java, JavaScript Coding For Beginners – Learn In A Day (2015)

Programming: Java, JavaScript Coding For Beginners – Learn In A Day (2015)

Chapter 4. Objects in JavaScript

Java Script- objects:

Objects in JavaScript consist of attributes. An attribute is considered a method, if it has a function, or else an attribute is considered a property.

Object Properties:

The properties of the object can be in any of the data types.

The following syntax is used when one has to add property to an object:

objectName.objectProperty = propertyValue;


The next example showcases how to use the "title" property of the document object:

var strg = document.title;

Object Methods:

The functions allowing the object to do a particular task.


The following example shows how the write() method is used:

document.write("This is text");

User-Defined Objects:

The new Operator:

The new operator helps in creating instances of an object. For creating an object, the constructor method follows the new operator.

In the below given example, Object(), Array(), and Date() are the constructor methods, which are in-built functions of JavaScript.

var emp = new Object();

var textbooks = new Array("C", "Python", "Java");

var day = new Date("August 21, 1987");

The Object() Constructor:

An object can be created and initialized by a constructor. Object() is a special constructor function that helps to build an object.

The following example shows to create an object:



<title>objects defined by the user </title>

<script type="text/javascript">

var book = new Object(); // Creates the object

book.subject = "Perl"; // Assign properties to the object

book.author = "Yashwanth";




<script type="text/javascript">

document.write("Book name is : " + book.subject + "<br>");

document.write("Book author is : " + book.author + "<br>");




JavaScript Native Objects:

JavaScript consists of many in-built native objects. Their behavior is not affected by the browser or the platform on which they run.

The following is a list of most widely used native objects in JavaScript:

1. Number Object

2. Boolean Object

3. String Object

4. JavaScript Math Object

The Number Object:

The number objects in JavaScript are used to represent numerical values. It can hold both integers and floating point values.

Whenever encountered with numerical values, the web browser converts those values into the number class instances automatically.


The syntax for creating a number object is as follows:

var value = new Number(number);

Number Methods

The following are the methods that are consisted in the number object.




This method is used to return the function that created the instance of the object.


This methods results in the display of a standard range number in the exponential form.


This method takes a number and returns a string version for the number. It is done in a format that varies with the locale settings of the web browser.


This method defines the number of digits to be displayed in a given number. This includes the digits that are present on either side of the decimal.


This method helps to convert a number into a string value.


This method represents the value of the number.

The Boolean Object:

The Boolean object is used to represent a value that can be either a ‘True” value or a ‘False’ value.


A boolen object can be created using the following syntax:

var bval = new Boolean(value);

The Boolean object’s initial value is false if the value parameter holds any of the following values: empty string, null, 0, Nan, -0, null or undefined.

Boolean Methods:

The following are the methods that are consisted in the Boolean object.




This method is used to return a string which consists of the Boolean object’ source. An equivalent object can be created using that string.


This method is used to return a ‘true’ string or a ‘false’string based on the object’s value.


This method is used to return the Boolean object’s primitive value.

The String Object:

The string object allows the user to work with a group of characters and wraps the string primitives of JavaScript with the helper methods.


A String object can be created using the following syntax

var value = new String(string);

The string parameter represents a group of properly encoded characters.

String Methods

The following are the methods that are consisted in the String object.




This method is used to return a number, that indicates a character’s Unicode value at a given index.


This method is used to combine characters of two strings and return a newly created string.


This method is used for matching a string with a regular expression.


Used to convert a string into lower case while maintaining the present locale.


Used to convert a string into upper case while maintaining the present locale.


Returns a string in which all uppercase letters are converted into lowercase letters


Returns a string in which all lowercase letters are converted into uppercase letters.

Math Object:

It allows the user to perform many common mathematical calculations. The object methods are called by writing the object’s name and then a dot and followed by the method’s name. The argument(s) are written in the method parenthesis.

Ex: document.writeln(Math.sqrt(81));

Following are some widely used functions of Math object:




Absolute value of x


Rounds x to the smallest integer not less than x.


Round x to closest integer.

pow(x , y)

X raised to the power of y


Rounds x to the largest integer not greater than x.


We’ve come to the end of the book on learning the basics of Java and JavaScript programming.

I want to thank you for purchasing this book. I hope you found this book useful and it helped you in understanding the concepts of programming using Java and Java Script. Being a high-level programming language, it makes it easy to learn the basics and then start writing programs on your own.

No language is rocket-science if studied properly. So is the case with the two languages covered in this book. Dive in and know the basics for these languages.

Thank you!