Built-in Objects

·         Built-in objects are not related to any Window or DOM object model.

·         These objects are used for simple data processing in the JavaScript.

 

Math Object

·         Math object is a built-in static object.

·         It is used for performing complex math operations.

Math Properties

Math Property

Description

SQRT2

Returns square root of 2.

PI

Returns Π value.

E \

Returns Euler's Constant.

LN2

Returns natural logarithm of 2.

LN10

Returns natural logarithm of 10.

LOG2E

Returns base 2 logarithm of E.

LOG10E

Returns 10 logarithm of E.


Math Methods

Methods

Description

abs()

Returns the absolute value of a number.

acos()

Returns the arccosine (in radians) of a number.

ceil()

Returns the smallest integer greater than or equal to a number.

cos()

Returns cosine of a number.

floor()

Returns the largest integer less than or equal to a number.

log()

Returns the natural logarithm (base E) of a number.

max()

Returns the largest of zero or more numbers.

min()

Returns the smallest of zero or more numbers.

pow()

Returns base to the exponent power, that is base exponent.

 

Example: Simple Program on Math Object Methods

<html>
     <head>
          <title>JavaScript Math Object Methods</title>
     </head>
     <body>
          <script type="text/javascript">
         
               var value = Math.abs(20);
               document.write("ABS Test Value : " + value +"<br>");
         
               var value = Math.acos(-1);
               document.write("ACOS Test Value : " + value +"<br>");
         
               var value = Math.asin(1);
               document.write("ASIN Test Value : " + value +"<br>");
         
               var value = Math.atan(.5);
               document.write("ATAN Test Value : " + value +"<br>");
          </script>
     </body>
</html>


Output

ABS Test Value : 20
ACOS Test Value : 3.141592653589793
ASIN Test Value : 1.5707963267948966
ATAN Test Value : 0.4636476090008061

Example: Simple Program on Math Object Properties

<html>
     <head>
          <title>JavaScript Math Object Properties</title>
     </head>
     <body>
          <script type="text/javascript">
               var value1 = Math.E
               document.write("E Value is :" + value1 + "<br>");
         
               var value2 = Math.LN2
               document.write("LN2 Value is :" + value2 + "<br>");
         
               var value3 = Math.LN10
               document.write("LN10 Value is :" + value3 + "<br>");
         
               var value4 = Math.PI
               document.write("PI Value is :" + value4 + "<br>");
          </script>
     </body>
</html>


Output:

E Value is :2.718281828459045
LN2 Value is :0.6931471805599453
LN10 Value is :2.302585092994046
PI Value is :3.141592653589793

Date Object

·         Date is a data type.

·         Date object manipulates date and time.

·         Date() constructor takes no arguments.

·         Date object allows you to get and set the year, month, day, hour, minute, second and millisecond fields.

Syntax:
var variable_name = new Date();

Example:
var current_date = new Date();

Date Methods

Methods

Description

Date()

Returns current date and time.

getDate()

Returns the day of the month.

getDay()

Returns the day of the week.

getFullYear()

Returns the year.

getHours()

Returns the hour.

getMinutes()

Returns the minutes.

getSeconds()

Returns the seconds.

getMilliseconds()

Returns the milliseconds.

getTime()

Returns the number of milliseconds since January 1, 1970 at 12:00 AM.

getTimezoneOffset()

Returns the timezone offset in minutes for the current locale.

getMonth()

Returns the month.

setDate()

Sets the day of the month.

setFullYear()

Sets the full year.

setHours()

Sets the hours.

setMinutes()

Sets the minutes.

setSeconds()

Sets the seconds.

setMilliseconds()

Sets the milliseconds.

setTime()

Sets the number of milliseconds since January 1, 1970 at 12:00 AM.

setMonth()

Sets the month.

toDateString()

Returns the date portion of the Date as a human-readable string.

toLocaleString()

Returns the Date object as a string.

toGMTString()

Returns the Date object as a string in GMT timezone.

valueOf()

Returns the primitive value of a Date object.

 

Example : JavaScript Date() Methods Program

<html>
     <body>
     <center>
          <h2>Date Methods</h2>
          <script type="text/javascript">
               var d = new Date();
               document.write("<b>Locale String:</b> " + d.toLocaleString()+"<br>");
               document.write("<b>Hours:</b> " + d.getHours()+"<br>");
               document.write("<b>Day:</b> " + d.getDay()+"<br>");
               document.write("<b>Month:</b> " + d.getMonth()+"<br>");
               document.write("<b>FullYear:</b> " + d.getFullYear()+"<br>");
               document.write("<b>Minutes:</b> " + d.getMinutes()+"<br>");
          </script>
     </center>
     </body>
</html>


Output:

String Object

·         String objects are used to work with text.

·         It works with a series of characters.

Syntax:
var variable_name = new String(string);

Example:
var s = new String(string);

String Properties

Properties

Description

length

It returns the length of the string.

prototype

It allows you to add properties and methods to an object.

constructor

It returns the reference to the String function that created the object.


String Methods

Methods

Description

charAt()

It returns the character at the specified index.

charCodeAt()

It returns the ASCII code of the character at the specified position.

concat()

It combines the text of two strings and returns a new string.

indexOf()

It returns the index within the calling String object.

match()

It is used to match a regular expression against a string.

replace()

It is used to replace the matched substring with a new substring.

search()

It executes the search for a match between a regular expression.

slice()

It extracts a session of a string and returns a new string.

split()

It splits a string object into an array of strings by separating the string into the substrings.

toLowerCase()

It returns the calling string value converted lower case.

toUpperCase()

Returns the calling string value converted to uppercase.

 

Example : JavaScript String() Methods Program

<html>
     <body>
     <center>
          <script type="text/javascript">
               var str = "CareerRide Info";
               var s = str.split();
               document.write("<b>Char At:</b> " + str.charAt(1)+"<br>");
               document.write("<b>CharCode At:</b> " + str.charCodeAt(2)+"<br>");
               document.write("<b>Index of:</b> " + str.indexOf("ide")+"<br>");
               document.write("<b>Lower Case:</b> " + str.toLowerCase()+"<br>");
               document.write("<b>Upper Case:</b> " + str.toUpperCase()+"<br>");
          </script>
     <center>
     </body>
</html>


Output: