Sunday, 21 August 2016

JavaScript Number and Array Object

The Number object
Numbers in JavaScript are 64 bit (8 bytes) floating point numbers.

Syntax for creating Number object
var num = new Number(value)

Properties of Number object

Properties
Description
constructor
Holds the value of constructor function that has created the object
MAX VALUE
Returns the maximum numerical value possible in javaScript
MIN VALUE
Returns the minimum numerical value possible in javaScript
NEGATIVE INFINITY
Represents the value of negative infinity
POSITIVE INFINITY
Represents the value of positive infinity
prototype
Adds properties and methods to the number object

Methods of the Number object


Method
Description
toExponential(x)
Converts number into exponential notation
toFixed(x)
Rounds up a number to x digit after the decimal
toPrecision(x)
Rounds up a number to a length of x digits
toString()
Returns a string value of the Number object
valueOf()
Returns a primitive value of the Number object

Example

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript Number Objects</title>
</head>
<body>
    <h1>JavaScript Number Objects</h1>
    <script>
        number = new Number(12.5678);
        document.write(number.toExponential() + '<br/>');
        document.write(number.toFixed() + '<br/>');
        document.write(number.toPrecision(2) + '<br/>');
        document.write(number.toString() + '<br/>');
        document.write(number.valueOf() + '<br/>');
    </script>
</body>
</html>
The Array object
It is store multiple values in a single variable. It can hold different data type value in a single slot.

It can be created using
1.       array constructor
2.       array literal notation

Using array constructor
Creating empty array: var new_array= new array();
Creating array of a given size: var new_array= new array(size);
Creating array with elements: var new_array= new array(“first element”, “second Element”);

Using array literal notation
Array literal notation is a comma separated list of items enclosed by square brackets.
Creating empty array: var new_array=[];
Creating array with elements: var new_array= [“element1”, “element2”];

Properties of array object

Properties
Description
constructor
Holds the value of constructor function that has created the object
length
Holds the number of elements in an array
prototype
Adds properties and methods to the array object

Methods of an array object

Properties
Description
concat()
Joins two or more arrays and returns the joined array
join()
Joins all the elements of an array into a string
pop()
Removes the last element of an array and return the element
push()
Adds new element as last element and returns length of the new array
reverse()
Reverses the order of list of element in an array
shift()
Removes the first element of an array and returns the element
slice()
Selects a part of an array and returns that part of new array.
sort()
Sorts the element of an array
splice()
Adds or removes the element of an array
toString()
Converts an array into a string and returns the string
unshift()
Adds new elements to an array and returns the new length
valueof()
Returns the primitive value of an array object

Example
<!DOCTYPE html>
<html>
<head>
    <title>JavaScript Array Objects</title>
</head>
<body>
    <h1>JavaScript Array Objects</h1>
    <script>
        array_months = new Array('Jan', 'Feb', 'Mar', 'April', 'May', 'June');
        array_num = new Array(1, 2, 3, 4, 5, 6);
        document.write('Array 1: '+array_months + '<br/>');
        document.write('Array 2: ' + array_num + '<br/>');
        document.write('concat two arrays: ' +  array_months.concat(array_num) + '<br/>');
        document.write('join two arrays: ' + array_months.join(array_num) + '<br/>');
        document.write('Array 1 pop out: ' + array_months.pop() + '<br/>');
        document.write('Array 2 revers: ' + array_num.reverse() + '<br/>');
        document.write('Shifted array 1 : ' + array_months.shift() + '<br/>');
        document.write('Sorted array 1: ' + array_months.sort() + '<br/>');
    </script>
</body>
</html>

0 comments:

Post a Comment