JavaScript Objects
A javaScript object is an entity having state and behavior (properties and method). For example: car, pen, bike, chair, glass, keyboard, monitor etc.
JavaScript is an object-based language. Everything is an object in JavaScript.
JavaScript is template based not class based. Here, we don't create class to get the object. But, we direct create objects.
Creating Objects in JavaScript
There are 3 ways to create objects.
- By object literal
- By creating instance of Object directly (using new keyword)
- By using an object constructor (using new keyword)
1) JavaScript Object by object literal
The syntax of creating object using object literal is given below:
As you can see, property and value is separated by : (colon).
Let’s see the simple example of creating object in JavaScript.
Output of the above example
2) By creating instance of Object
The syntax of creating object directly is given below:
Here, new keyword is used to create object.
Let’s see the example of creating object directly.
Output of the above example
3) By using an Object constructor
Here, you need to create function with arguments. Each argument value can be assigned in the current object by using this keyword.
The this keyword refers to the current object.
The example of creating object by object constructor is given below.
Output of the above example
Defining method in JavaScript Object
We can define method in JavaScript object. But before defining method, we need to add property in the function with same name as method.
The example of defining method in object is given below.
Output of the above example
103 Sonoo Jaiswal 45000
JavaScript Array
JavaScript array is an object that represents a collection of similar type of elements.
There are 3 ways to construct array in JavaScript
- By array literal
- By creating instance of Array directly (using new keyword)
- By using an Array constructor (using new keyword)
1) JavaScript array literal
The syntax of creating array using array literal is given below:
As you can see, values are contained inside [ ] and separated by , (comma).
Let’s see the simple example of creating and using array in JavaScript.
The .length property returns the length of an array.
Output of the above example
Vimal
Ratan
2) JavaScript Array directly (new keyword)
The syntax of creating array directly is given below:
Here, new keyword is used to create instance of array.
Let’s see the example of creating array directly.
Output of the above example
Varun
John
3) JavaScript array constructor (new keyword)
Here, you need to create instance of array by passing arguments in constructor so that we don't have to provide value explicitely.
The example of creating object by array constructor is given below.
Output of the above example
Vijay
Smith
JavaScript String
The JavaScript string is an object that represents a sequence of characters.
There are 2 ways to create string in JavaScript
- By string literal
- By string object (using new keyword)
1) By string literal
The string literal is created using double quotes. The syntax of creating string using string literal is given below:
Let’s see the simple example of creating string literal.
Output:
2) By string object (using new keyword)
The syntax of creating string object using new keyword is given below:
Here, new keyword is used to create instance of string.
Let’s see the example of creating string in JavaScript by new keyword.
Output:
JavaScript String Methods
Let's see the list of JavaScript string methods with examples.
- charAt(index)
- concat(str)
- indexOf(str)
- lastIndexOf(str)
- toLowerCase()
- toUpperCase()
- slice(beginIndex, endIndex)
- trim()
1) JavaScript String charAt(index) Method
The JavaScript String charAt() method returns the character at the given index.
Output:
2) JavaScript String concat(str) Method
The JavaScript String concat(str) method concatenates or joins two strings.
Output:
3) JavaScript String indexOf(str) Method
The JavaScript String indexOf(str) method returns the index position of the given string.
Output:
4) JavaScript String lastIndexOf(str) Method
The JavaScript String lastIndexOf(str) method returns the last index position of the given string.
Output:
5) JavaScript String toLowerCase() Method
The JavaScript String toLowerCase() method returns the given string in lowercase letters.
Output:
6) JavaScript String toUpperCase() Method
The JavaScript String toUpperCase() method returns the given string in uppercase letters.
Output:
7) JavaScript String slice(beginIndex, endIndex) Method
The JavaScript String slice(beginIndex, endIndex) method returns the parts of string from given beginIndex to endIndex. In slice() method, beginIndex is inclusive and endIndex is exclusive.
Output:
8) JavaScript String trim() Method
The JavaScript String trim() method removes leading and trailing whitespaces from the string.
Output:
JavaScript Date Object
The JavaScript date object can be used to get year, month and day. You can display a timer on the webpage by the help of JavaScript date object.
You can use different Date constructors to create date object. It provides methods to get and set day, month, year, hour, minute and seconds.
Constructor
You can use 4 variant of Date constructor to create date object.
JavaScript Date Methods
The important methods of date object are as follows:
JavaScript Date ExampleLet's see the simple example to print date object. It prints date and time both.
Output:
JavaScript Current Time Example
Let's see the simple example to print current time of system.
Output:
JavaScript Digital Clock Example
Let's see the simple example to display digital clock using JavaScript date object.
There are two ways to set interval in JavaScript: by setTimeout() or setInterval() method.
Output:
JavaScript Math Object
The JavaScript math object provides several constants and methods to perform mathematical operation. Unlike date object, it doesn't have constructors.
Math.sqrt(n)
The JavaScript math.sqrt(n) method returns the square root of the given number.
Output:
Math.random()
The JavaScript math.random() method returns the random number between 0 to 1.
Output:
Math.pow(m,n)
The JavaScript math.pow(m,n) method returns the m to the power of n that is mn.
Output:
Math.floor(n)
The JavaScript math.floor(n) method returns the lowest integer for the given number. For example 3 for 3.7, 5 for 5.9 etc.
Output:
Math.ceil(n)
The JavaScript math.ceil(n) method returns the largest integer for the given number. For example 4 for 3.7, 6 for 5.9 etc.
Output:
Math.round(n)
The JavaScript math.round(n) method returns the rounded integer nearest for the given number. If fractional part is equal or greater than 0.5, it goes to upper value 1 otherwise lower value 0. For example 4 for 3.7, 3 for 3.3, 6 for 5.9 etc.
Output:
Round of 4.7 is: 5
Math.abs(n)
The JavaScript math.abs(n) method returns the absolute value for the given number. For example 4 for -4, 6.6 for -6.6 etc.
Output:
JavaScript Number Object
The JavaScript number object enables you to represent a numeric value. It may be integer or floating-point. JavaScript number object follows IEEE standard to represent the floating-point numbers.
By the help of Number() constructor, you can create number object in JavaScript. For example:
If value can't be converted to number, it returns NaN(Not a Number) that can be checked by isNaN() method.
You can direct assign a number to a variable also. For example:
Output:
JavaScript Number Constants
Let's see the list of JavaScript number constants with description.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgt5Z3gf69pYxgOnmTvXE9NbPAwPxyg3oV46_p2o56gpacEzhHljJCB3cfXYwqyW0shOvv6PQidC8yU_E5bQB0oBm87iTYqZLM3uM5Z6qW6CYQ_IC36ThJ_JU4dWOTBNSCALponTIFaDMw/s320/new+1.jpg)
JavaScript Number Methods
Let's see the list of JavaScript number methods with description.
JavaScript Boolean
JavaScript Boolean is an object that represents value in two states: true or false. You can create the JavaScript Boolean object by Boolean() constructor as given below.
The default value of JavaScript Boolean object is false.
JavaScript Boolean object provides properties and methods also.
JavaScript Boolean Properties
JavaScript Boolean Methods
0