Built-in/Predefined Methods in JavaScript

In JavaScript, however, a method is a function that belongs to an object. A JavaScript method is a property containing a function definition.

There is a long list of Built-in or pre-defined methods in JavaScript

Maths Methods in JavaScript

  • min() ->Returns the smallest of zero or more numbers.
  • pow() ->Returns base to the exponent power, that is, base exponent.
  • abs() ->Returns the absolute value of a number.
  • acos() ->Returns the arccosine (in radians) of a number.
  • asin() ->Returns the arcsine (in radians) of a number.
  • sin() ->Returns the sine of a number.
  • sqrt() ->Returns the square root of a number.
  • tan() ->Returns the tangent of a number.
  • atan() ->Returns the arctangent (in radians) of a number.
  • atan2() ->Returns the arctangent of the quotient of its arguments.
  • exp() ->Returns EN, where N is the argument, and E is Euler’s constant, the base of the natural logarithm.
  • 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.
  • ceil() ->Returns the smallest integer greater than or equal to a number.
  • cos() ->Returns the cosine of a number.
  • random() ->Returns a pseudo-random number between 0 and 1.
  • round() ->Returns the value of a number rounded to the nearest integer.
  • toSource() ->Returns the string “Math”.

Boolean Methods in JavaScript

  • toString() ->Returns a string of either “true” or “false” depending upon the value of the object.
  • toSource() ->Returns a string containing the source of the Boolean object; you can use this string to create an equivalent object.
  • valueOf() ->Returns the primitive value of the Boolean object.

String Methods in JavaScript

  • length() ->Returns the length of the string.
  • search() ->Executes the search for a match between a regular expression and a specified string.
  • split() ->Splits a String object into an array of strings by separating the string into substrings.
  • lastIndexOf() ->Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.
  • charAt() ->Returns the character at the specified index.
  • match() ->Used to match a regular expression against a string.
  • charCodeAt() ->Returns a number indicating the Unicode value of the character at the given index.
  • concat()-Combines the text of two strings and returns a new string.
  • replace() ->Used to find a match between a regular expression and a string and to replace the matched substring with a new substring.
  • substr() ->Returns the characters in a string beginning at the specified location through the specified number of characters.
  • toLowerCase() ->Returns the calling string value converted to lower case.
  • toString() ->Returns a string representing the specified object.
  • toUpperCase() ->Returns the calling string value converted to uppercase.
  • valueOf() ->Returns the primitive value of the specified object.
  • toLocaleLowerCase() ->The characters within a string are converted to lower case while respecting the current locale.
  • toLocaleUpperCase() ->The characters within a string are converted to the upper case while respecting the current locale.
  • substring() ->Returns the characters in a string between two indexes into the string.

Array Methods in JavaScript

  • every() ->Returns true if every element in this array satisfies the provided testing function.
  • reduce() ->Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
  • filter() ->Creates a new array with all of the elements of this array for which the provided filtering function returns true.
  • forEach() ->Calls a function for each element in the array.
  • indexOf() ->Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
  • join() ->Joins all elements of an array into a string.
  • slice() ->Extracts a section of an array and returns a new array.
  • some() ->Returns true if at least one element in this array satisfies the provided testing function.
  • toSource() ->Represents the source code of an object
  • lastIndexOf() ->Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
  • map() ->Creates a new array with the results of calling a provided function on every element in this array.
  • reduceRight() ->Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
  • reverse() ->Reverses the order of the elements of an array — the first becomes the last, and the last becomes the first.
  • shift() ->Removes the first element from an array and returns that element.
  • 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.
  • pop() ->Removes the last element from an array and returns that element.
  • push()->Adds one or more elements to the end of an array and returns the new length of the array.

Date Methods in JavaScript

  • getTime() ->Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC.
  • setTime() ->Sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC.
  • getTimezoneOffset() ->Returns the time-zone offset in minutes for the current locale.
  • getUTCDate() ->Returns the day (date) of the month in the specified date according to universal time.
  • setUTCDate() ->Sets the day of the month for a specified date according to universal time.
  • getUTCDay() ->Returns the day of the week in the specified date according to universal time.
  • getUTCFullYear() ->Returns the year in the specified date according to universal time.
  • getUTCHours() ->Returns the hours in the specified date according to universal time.
  • getUTCMilliseconds() ->Returns the milliseconds in the specified date according to universal time.
  • getUTCMinutes() ->Returns the minutes in the specified date according to universal time.
  • getUTCMonth() ->Returns the month in the specified date according to universal time.
  • getUTCSeconds() ->Returns the seconds in the specified date according to universal time.
  • getYear() ->Deprecated – Returns the year in the specified date according to local time. Use getFullYear instead.
  • setYear() ->Deprecated – Sets the year for a specified date according to local time. Use setFullYear instead.
  • setDate() ->Sets the day of the month for a specified date according to local time.
  • getDate() ->Returns the day of the month for the specified date according to local time.
  • setFullYear() ->Sets the full year for a specified date according to local time.
  • setHours() ->Sets the hours for a specified date according to local time.
  • setMilliseconds() ->Sets the milliseconds for a specified date according to local time.
  • setMinutes() ->Sets the minutes for a specified date according to local time.
  • getMonth() ->Returns the month in the specified date according to local time.
  • setMonth() ->Sets the month for a specified date according to local time.
  • getSeconds() ->Returns the seconds in the specified date according to local time.
  • setSeconds() ->Sets the seconds for a specified date according to local time.
  • setUTCFullYear() ->Sets the full year for a specified date according to universal time.
  • setUTCHours() ->Sets the hour for a specified date according to universal time.
  • setUTCMilliseconds() ->Sets the milliseconds for a specified date according to universal time.
  • setUTCMinutes() ->Sets the minutes for a specified date according to universal time.
  • setUTCMonth() ->Sets the month for a specified date according to universal time.
  • setUTCSeconds() ->Sets the seconds for a specified date according to universal time.
  • toDateString() ->Returns the “date” portion of the Date as a human-readable string.
  • toGMTString() ->Deprecated – Converts a date to a string, using the Internet GMT conventions. Use toUTCString instead.
  • toString() ->Returns a string representing the specified Date object.
  • toTimeString() ->Returns the “time” portion of the Date as a human-readable string.
  • valueOf() ->Returns the primitive value of a Date object.
  • Date() ->Returns today’s date and time
  • getDay() ->Returns the day of the week for the specified date according to local time.
  • getFullYear() ->Returns the year of the specified date according to local time.
  • toLocaleDateString() ->Returns the “date” portion of the Date as a string, using the current locale’s conventions.
  • toLocaleFormat() ->Converts a date to a string, using a format string.
  • toLocaleTimeString() ->Returns the “time” portion of the Date as a string, using the current locale’s conventions.
  • getHours() ->Returns the hour in the specified date according to local time.
  • getMilliseconds() ->Returns the milliseconds in the specified date according to local time.
  • getMinutes() ->Returns the minutes in the specified date according to local time.

Few important links for learning:-

  1. Want to know the difference between Function and Method in JavaScript? Just click here.
  2. Want to know what is function and how to use them in JavaScript? Just click here.
  3. Want to know what are variables and how to use them in JavaScript? Just click here.
  4. Let’s see an example for User form validation in JavaScript. click here.
  5. Let’s understand JavaScript. what and how? click here.
Tagged : / / / /

Methods in JavaScript

In JavaScript, however, a method is a function that belongs to an object. A JavaScript method is a property containing a function definition.

Few Important things of Methods:-

  • In JavaScript, however, a method is a function that belongs to an object.
  • JavaScript methods are the actions that can be performed on objects.
  • A JavaScript method is a property containing a function definition.
  • Accessing the object method is done with the following syntax: objectName.methodName()

Just similar to the functions in JavaScript methods are also classified in two categories:-

  1. Predefined
  2. User-defined

Predefined methods can be used directly by calling and giving parameter values (if required). while user-defined methods are first declared, defined, and then used in the program.

Deceleration and definitions of User-defined methods are done together we could understand better seeing the below code.
Syntax of Methods

object = {
    methodName: function() {
        // Content
    }
};

object.methodName()

How to access the Object Methods

we can access the object method with the following syntax stated below:-

objectName.methodName()

How to add the method to Object.

person.name = function () {
    return this.firstName + " " + this.lastName;
};

We could also directly use the built-in method.

var message = "Display message of your choice";
var x = message.toUpperCase();

The above method would just convert the text entered as a message to uppercase.

Few important links for learning:-

  1. Want to know the difference between Function and Method in JavaScript? Just click here.
  2. Want to know what is function and how to use them in JavaScript? Just click here.
  3. Want to know what are variables and how to use them in JavaScript? Just click here.
  4. Let’s see an example for User form validation in JavaScript. click here.
  5. Let’s understand JavaScript. what and how? click here.
Tagged : / / / / / /

Difference between Function and Method in JavaScript

So what’s the different between the two?

  • Methods have a “receiver” while functions do not.
  • Functions and methods are the same in JavaScript, but a method is a function, which is a property of an object.
  • Syntax for calling/ definition/declaration of functions and methods are different.
  • A function can be called directly by its name but methods are called by its object name and its method name using dot notation or square bracket notation.
  • Function passes the data and methods operates the data passed in the class.
  • A function lives on its own but method is a function associated with an object property.
  • Data passed to a function is explicit but method implicitly passes the object on which it was called.

Lets see detailed difference between them:-

A function in JavaScript is similar to a procedure —>a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output

Few Important things of functions:-

  • The function is executed when something calls/invokes it.
  • The name may contain letters, digits, dollar signs, underscore.
  • Parameters are listed inside round parenthesis after the name of the function.
  • Arguments are values a function receives when it is invoked.
  • When the control reaches the return statement, js will stop executing and the value is returned to the caller.

There are three steps of Functions as well as Methods to use in the program.

  1. Declaration
  2. Definition.
  3. Call

In JavaScript function declaration and definition are mostly done together.

A JavaScript method is a property of an object that contains a function definition. Methods are functions stored as object properties.

Few Important things of Methods:-

  • In JavaScript, however, a method is a function that belongs to an object.
  • JavaScript methods are the actions that can be performed on objects.
  • A JavaScript method is a property containing a function definition.
  • Accessing the object method is done with the following syntax: objectName.methodName()

Syntax of Function

var func = function(a, b) {
    var sum = a + b;
    return sum;
}
  
console.log(sum(1, 2));

Syntax of Methods

object = {
    methodName: function() {
        // Content
    }
};

object.methodName()

Few important links for learning:-

  1. Let’s see an example for User form validation in JavaScript. click here.
  2. Let’s understand JavaScript. what and how? click here.
  3. Want to know what is function and how to use them in JavaScript? Just click here.
  4. Want to know what are variables and how to use them in JavaScript? Just click here.

Tagged : / / / /