JavaScript Native Object

JS Native Objects

JavaScript has several built-in or native objects. These objects are accessible anywhere in the program as the other objects. The following are some of the important JavaScript Native Objects:

JavaScript Number Object

The Number object represents numerical date, either integers or floating-point numbers. The browser automatically converts number literals to instances of the number class.

Syntax for creating a number object:

       
  var val = new Number(number);
    

If the argument cannot be converted into a number, it returns NaN(Not-a-Number).

Number Properties

Property Description
MAX_VALUE Returns the maximum number value supported in JavaScript and largest number possible is (1.7976931348623157E+308)
MIN_VALUE Returns the smallest number value supported in JavaScript and smallest number possible is (5E-324)
NEGATIVE_INFINITY Returns negative infinity (-Infinity)
NaN Represents a value that is not a number.
POSITIVE_INFINITY Represents positive infinity (Infinity).

Number Methods:

Method Description
toExponential(fractionDigits) Returns exponential value as a string.

Example:
var num = 100; Num.toExponential(2); // returns '1.00e+2'
toFixed(fractionDigits) Returns string of decimal value of a number based on specified fractionDigits.

Example:
var num = 100; Num.toFixed(2); // returns '100.00'
toLocaleString() Returns a number as a string value according to a browser's locale settings.

Example:
var num = 100; Num.toLocaleString(); // returns '100'
toPrecision(precisionNumber) Returns number as a string with specified total digits.

Example:
var num = 100; Num.toPrecision(4); // returns '100.0'
toString() Returns the string representation of the number value.

Example:
var num = 100; Num.toString(); // returns '100'
valueOf() Returns the value of Number object.

Example: var num = new Number(100); Num.valueOf(); // returns '100'


JavaScript Boolean Object

Boolean Object is a primitive data type in JavaScript. Boolean can have only two values, true or false.

Boolean Method:

Method Description
toLocaleString() Returns string of boolean value in local browser environment.

Example: var result = (1 > 2); result.toLocaleString(); // returns "false"
toString() Returns a string of Boolean.

Example: var result = (1 > 2); result.toString(); // returns "false"
valueOf() Returns the value of the Boolean object.

Example: var result = (1 > 2); result.valueOf(); // returns false


JavaScript String object:

the String object has a number of methods.

Syntax for creating a String object

 
  var val = new String(string);

String Properties

  • Length-Returns the length of the string.
  • Prototype-the prototype allow you to add properties and methods to an object.

  • String Methods:

    Method Description
    charAt(position) Returns the character at the specified position (in Number).
    charCodeAt(position) Returns a number indicating the Unicode value of the character at the given position (in Number).
    concat([string,,]) Joins specified string literal values (specify multiple strings separated by comma) and returns a new string.
    indexOf(SearchString, Position) Returns the index of first occurrence of specified String starting from specified number index. Returns -1 if not found.
    lastIndexOf(SearchString, Position) Returns the last occurrence index of specified SearchString, starting from specified position. Returns -1 if not found.
    localeCompare(string,position) Compares two strings in the current locale.
    match(RegExp) Search a string for a match using specified regular expression. Returns a matching array.
    replace(searchValue, replaceValue) Search specified string value and replace with specified replace Value string and return new string. Regular expression can also be used as searchValue.
    search(RegExp) Search for a match based on specified regular expression.
    slice(startNumber, endNumber) Extracts a section of a string based on specified starting and ending index and returns a new string.
    split(separatorString, limitNumber) Splits a String into an array of strings by separating the string into substrings based on specified separator. Regular expression can also be used as separator.
    substr(start, length) Returns the characters in a string from specified starting position through the specified number of characters (length).
    substring(start, end) Returns the characters in a string between start and end indexes.
    toLocaleLowerCase() Converts a string to lower case according to current locale.
    toLocaleUpperCase() Converts a sting to upper case according to current locale.
    toLowerCase() Returns lower case string value.
    toString() Returns the value of String object.
    toUpperCase() Returns upper case string value.
    valueOf() Returns the primitive value of the specified string object.


    String HTML wrappers

    Here is a list of methods which returns the string wrapped inside the appropriate HTML tag

    Method Description
    anchor() Creaters an HTML anchor that is used as a hypertext target.
    big() Creates a string to be displayed in a big font as if it were in <big> tag.
    blink() Creates a string to blink as if it were in a <blink> tag.
    bold() Creates a string to be displayed as bold as if it were in a <b> tag.
    fixed() Causes a string to e displayed in fixed-pitch font as if it were in a <tt> tag.
    fontcolor() Causes a string to be displayed in the specified color as if it were in a <font color="color"> tag.
    fontsize() Causes a string to be displayed in the specified font size a if it were in a <font size="size"> tag.
    italics() Causes a string to be italic, as if it were in an <i> tag.
    link() Creates an HTML hypertext link that requests another URL.
    small() Causes a string to be displayed in a small font, as if it were in a <small>tag.
    strike() Causes a string to e displayed as struck-out text, as if it were in a <strike> tag.
    sub() Causes a string to be displayed as a suscript, as itf it were <sub>tag.
    sup() Causes a string to be displayed as a superscript, as if it were in a <sup>tag.


    JavaScript Array object

    The Array object is used store multiple values in a single variable.

    Example:

    var fruits = new Array("apple","orange","mango");
    

    The Array parameter is a list of strings or integers. The maximum length allowed for an array is 4,294,967,295.

    Array Properties

    • Inext - The property represents the zero-based index of the match in the string.
    • Index - This property is only present in arrays created by regular expression matches.
    • Length - Reflects the number of elements in an array.
    • Prototype - The prototype property allows you to add properties and methods to an object.

    Array Methods

    Method Description
    concat() Returns new array by combining values of an array that is specified as parameter with existing array values.
    every() Returns true or false if every element in the specified array satisfies a condition specified in the callback function. Returns false even if single element does not satisfy the condition.
    filter() Returns a new array with all the elements that satisfy a condition specified in the callback function.
    forEach() Executes a callback function for each elements of an array.
    indexOf() Returns the index of the first occurrence of the specified element in the array, or -1 if it is not found.
    join() Returns string of all the elements separated by the specified separator
    lastIndexOf() Returns the index of the last occurrence of the specified element in the array, or -1 if it is not found.
    map() Creates a new array with the results of calling a provided function on every element in this array.
    pop() Removes the last element from an array and returns that element.
    push() Adds one or more elements at the end of an array and returns the new length of the array.
    reduce() Pass two elements simultaneously in the callback function (till it reaches the last element) and returns a single value.
    reduceRight() Pass two elements simultaneously in the callback function from right-to-left (till it reaches the last element) and returns a single value.
    reverse() Reverses the elements of an array. Element at last index will be first and element at 0 index will be last.
    shift() Removes the first element from an array and returns that element.
    slice() Returns a new array with specified start to end elements.
    some() Returns true if at least one element in this array satisfies the condition in the callback function.
    sort() Sorts the elements of an array.
    splice() Adds and/or removes elements from an array.
    toString() Returns a string representing the array and its elements.
    unshift() Adds one or more elements to the front of an array and returns the new length of the array.


    JavaScript Date Object

    The Date object is a data type built into the JavaScript language. Once a Date object is created, a number of methods are available to operate on it.

    Syntax:

    • new Date() - This constructor creates a Date object set to the current date and time.
    • new Date(milliseconds) - When one numeric argument is passed, it is taken as the internal numeric representation of the date in milliseconds, as returned by the getTime() method.
    • new Date(datestring) - When one string argument is passed, it is a string representation of a date, in the format accepted by the Date.parse() method.
    • new Date(year,month,date[,hour,minut,second,millisecond])-The parameters on square brackets are optional.

    Date Properties:

  • Constructor - Specifies the function that creates an object's prototype.
  • Prototype - The prototype property allows you to add properties and methods to an object
  • Method Description
    getDate() Returns the day of the month (from 1-31)
    getDay() Returns the day of the week (from 0-6)
    getFullYear() Returns the year
    getHours() Returns the hour (from 0-23)
    getMilliseconds() Returns the milliseconds (from 0-999)
    getMinutes() Returns the minutes (from 0-59)
    getMonth() Returns the month (from 0-11)
    getSeconds() Returns the seconds (from 0-59)
    getTime() Returns the number of milliseconds since midnight Jan 1 1970, and a specified date
    getTimezoneOffset() Returns the time difference between UTC time and local time, in minutes
    getUTCDate() Returns the day of the month, according to universal time (from 1-31)
    getUTCDay() Returns the day of the week, according to universal time (from 0-6)
    getUTCFullYear() Returns the year, according to universal time
    getUTCHours() Returns the hour, according to universal time (from 0-23)
    getUTCMilliseconds() Returns the milliseconds, according to universal time (from 0-999)
    getUTCMinutes() Returns the minutes, according to universal time (from 0-59)
    getUTCMonth() Returns the month, according to universal time (from 0-11)
    getUTCSeconds() Returns the seconds, according to universal time (from 0-59)
    getYear() Deprecated. Use the getFullYear() method instead
    now() Returns the number of milliseconds since midnight Jan 1, 1970
    parse() Parses a date string and returns the number of milliseconds
    setDate() Sets the day of the month of a date object
    setFullYear() Sets the year of a date object
    setHours() Sets the hour of a date object
    setMilliseconds() Sets the milliseconds of a date object
    setMinutes() Set the minutes of a date object
    setMonth() Sets the month of a date object
    setSeconds() Sets the seconds of a date object
    setTime() Sets a date to a specified number of milliseconds after/before January 1, 1970
    setUTCDate() Sets the day of the month of a date object, according to universal time
    setUTCFullYear() Sets the year of a date object, according to universal time
    setUTCHours() Sets the hour of a date object, according to universal time
    setUTCMilliseconds() Sets the milliseconds of a date object, according to universal time
    setUTCMinutes() Set the minutes of a date object, according to universal time
    setUTCMonth() Sets the month of a date object, according to universal time
    setUTCSeconds() Set the seconds of a date object, according to universal time
    setYear() Deprecated. Use the setFullYear() method instead
    toDateString() Converts the date portion of a Date object into a readable string
    toGMTString() Deprecated. Use the toUTCString() method instead
    toISOString() Returns the date as a string, using the ISO standard
    toJSON() Returns the date as a string, formatted as a JSON date
    toLocaleDateString() Returns the date portion of a Date object as a string, using locale conventions
    toLocaleTimeString() Returns the time portion of a Date object as a string, using locale conventions
    toLocaleString() Converts a Date object to a string, using locale conventions
    toString() Converts a Date object to a string
    toTimeString() Converts the time portion of a Date object to a string
    toUTCString() Converts a Date object to a string, according to universal time
    UTC() Returns the number of milliseconds in a date since midnight of January 1, 1970, according to UTC time
    valueOf() Returns the primitive value of a Date object


    JavaScript Math Object

    The math object provides the properties and methods for mathematical constants and functions.

    Unlike the other global objects, Math is not a constructor.

    All properties and methods of Math are static and can be called by using Math as an objcet without creating it.

    Example:

     
      var pi_val = Math.PI;//Math object need not be created
      var sine_val = Math.sin(30);
     

    Math Properties

    Property Description
    E Returns Euler's number, the base of natural logarithms, e, approximately 2.718
    LN2 Returns the natural logarithm of 2, approximately 0.693
    LN10 Returns the natural logarithm of 10, approximately 2.302
    LOG2E Returns the base 2 logarithm of e, approximately 1.442
    LOG10E Returns the base 10 logarithm of e, approximately 0.434
    PI Returns the ratio of the circumference of a circle to its diameter (i.e. π). The approximate value of PI is 3.14159
    SQRT1_2 Returns the square root of 1/2, approximately 0.707
    SQRT2 Returns the square root of 2, approximately 1.414


    JavaScript Math Methods

    Method Description
    abs() Returns the absolute value of a number.
    acos() Returns the arccosine of a number, in radians.
    acosh() Returns the hyperbolic arccosine of a number.
    asin() Returns the arcsine of a number, in radians
    asinh() Returns the hyperbolic arcsine of a number.
    atan() Returns the arctangent of a number, in radians.
    atan2(y, x) Returns the arctangent of the quotient of its arguments.
    atanh() Returns the hyperbolic arctangent of a number.
    cbrt() Returns the cube root of a number.
    ceil() Returns the next integer greater than or equal to a given number (rounding up).
    cos() Returns the cosine of the specified angle. The angle must be specified in radians.
    cosh() Returns the hyperbolic cosine of a number.
    exp(x) Returns ex, where x is the argument, and e is Euler's number (also known as Napier's constant), the base of the natural logarithms.
    floor() Returns the next integer less than or equal to a given number (rounding down).
    log() Returns the natural logarithm (base e) of a number.
    max(x, y, ...) Returns the highest-valued number in a list of numbers.
    min(x, y, ...) Returns the lowest-valued number in a list of numbers.
    pow(x, y) Returns the base to the exponent power, that is, xy.
    random() Returns a random number between 0 and 1 (including 0, but not 1).
    round() Returns the value of a number rounded to the nearest integer.
    sin() Returns the sign of a number (given in radians).
    sinh() Returns the hyperbolic sine of a number.
    sqrt() Returns the square root of a number.
    tan() Returns the tangent of a number.
    tanh() Returns the hyperbolic tangent of a number.
    trunc(x) Returns the integer part of a number by removing any fractional digits.


    Regular expressions and regular objects

    A regular expression is an object that describes a pattern of characters.

    The JavaScript RegExp class represents regular expressions, And both String and RegExp define methods that use regular expressions to perform powerful pattern-matching and search-and-replace functions on text.

    Syntax:

      var pattern = new RegExp(pattern, attributes);
      var pattern = /pattern/attributes;
    
  • pattern: A string that specifies the pattern of the regular expression or another regular expression.
  • attributes: An optional string containing any of the "g","i", and "m" attributes that specify global, case-insensitive, and multiline matches, respectively.
  • Bracketts:

    Brackets ([]) have a special meaning when used in the context of regualr expression.

    they are used to find a range of characters.

    • [...] - Any one character between the brackets.
    • [^...] - Any one character not between the brackets.
    • [0-9] - It matches any decimal digit from 0 through 9.
    • [a-z] - it matches any character from lowercase a through lowercase z.
    • [A-Z] - it matches any character from uppercase A through uppercase Z.
    • [a-Z] - It matches any character from lowercase a through uppercase Z.

    Quantifiers:

    The frequency or position of bracketed character sequences and single characters can be denoted by a special character.

    Each special character have a specific connotation. The +,*,?, and $ flags all follow a character sequence.

    • p+ -It matches any string containing at least one p.
    • p* -It matches nay string containing zero or more p's.
    • p? -It matches any string containing one or more p's.
    • p{N} -It matches any string containing a sequence of N p's.
    • p{2,3} -It matches any string containing a sequence of two or three p's.
    • p{2,} -It matches any string containing a sequence of at least two p's.
    • p$ -It matches any string with p at the end of it.
    • ^p -It matches any string with p at the beginning of it.

    • RefExp Properties

      Property Description
      Global Specifies if the "g" modifier is set
      ingnoreCase Specifies if the "i" modifies is set.
      LastIndex The index at which to start the next match.
      Multiline Specifies if the "m" modifier is set.
      Source The text of the pattern.
      Global Specifies if the "g" modifier is set.


      RefExp Methods

      Property Description
      exec() Executes a search for a match in its string parameter.
      test() Tests for a match in its string parameter.
      toSource() Returns an object literal representing the specified object; you can use this value to create a new object.
      toString() Returns a string representing the specified object.