Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Number Object Properties

Property Description
constructor Returns the function that created the Number object's prototype
MAX_VALUE Returns the largest number possible in JavaScript
MIN_VALUE Returns the smallest number possible in JavaScript
NEGATIVE_INFINITY Represents negative infinity (returned on overflow)
NaN Represents a "Not-a-Number" value
POSITIVE_INFINITY Represents infinity (returned on overflow)
prototype Allows you to add properties and methods to an object

Number Object Methods

Method Description
toExponential(x) Converts a number into an exponential notation
toFixed(x) Formats a number with x numbers of digits after the decimal point
toPrecision(x) Formats a number to x length
toString() Converts a Number object to a string
valueOf() Returns the primitive value of a Number object

 

Objects as associative arrays

Associative arrays are arrays that use strings in place of index numbers, for key elements.Using an associative array is quite similar to using a property name with a regular object; you just do it in an array format.

Syntax:

var array_name = new Array();

array_name['key1'] = value1;

array_name['key2'] = value2;

// ...

- "array_name" is the name of the array.
- 'key1', 'key2' are indices (keys) associated with each element (can be written between double or simple quotes, but if they are numbers, do not add quotes).
- "value1", "value2" are the values of the elements (can be: strings, numbers, variables, expressions, or another array). - You can add any number of elements in the Array, with the sintax:

array_name['key'] = value;

• Accessing an associative array is the same as a normal array, except that you use an index string rather than an index number

(array_name['key']).
Example:

<script type="text/javascript"><!--

var arrex = new Array();

arrex['site'] = 'http://coursesweb.net';

arrex['course'] = 'Web programming';

arrex['tutorials'] = 'JavaScript';

 

alert(arrex['site']); // http://coursesweb.net

--></script>

Numerically indexed arrays.

An Array is an enumerated list of variables. It's a programming construct that allows programmers to replace this…

x0=0;

x1=1;

x2=2;

x3=3;

x4=4;

x5=5; …with this…

x[0]=0;

x[1]=1;

x[2]=2;

x[3]=3;

x[4]=4;

x[5]=5;

The index (the number in the brackets [] )can be referenced by a variable, allowing for easy looping through the data structure.

for(i=0; i<6; i++) {

document.writeln(x[i]+'<br>);

} Which will output the following…

Creating A New Array.

var myArray = new Array(10);

If you want to create a new Array simply use brackets [] like this…

var myArray = [];

For instance.

var badArray = new Array(10); // Creates an empty Array that's sized for 10 elements.



var goodArray= [10]; // Creates an Array with 10 as the first element.

. If you had wanted to add more than one item then badArray would be initialized correctly since Javascript would then be smart enough to know that you were initializing the array instead of stating how many elements you wanted to add..

46.Arrays: Methods

Description

An Array is used to store a number of values (called as elements) in order with a single variable.Can be created using javascript array object constructor.

   
   
   
   

Parameters

size : The length of the array. The maximum length allowed for an array is 4,294,967,295.


Date: 2016-01-03; view: 922


<== previous page | next page ==>
Methods and properties. | Array Object Methods
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.008 sec.)