JavaScript Built-in Classes and Their Useful Methods

In this article, we intent to give you a list of built-in classes from JavaScript and their useful methods.

Advertisements

You can click on each one of them to understand how those methods work.

Array Class

You can create an array using following constructor.

var myArray = new Array(23,44,5.5,'tomato');
console.log(myArray);
//Output = [23,44, 5.5,'tomato']

Popular Methods

Number Class

This class converts string to numbers, and some other conversions. It uses the constructor called Number();

The Number constructor takes a number of type string and then convert into a number of type number.

var num = Number('34.5');
//typeof '34.5' is string.
console.log(num);
//output = 34.5 and typeof 34.5 is Number.

Popular Methods: Number Class

  • parseInt()
  • parseFloat()
  • num.toFixed()
  • num.toPrecision()
  • num.toString()
  • num.toExponential()

String Class

The string class has the constructor called String that used to create string objects.

Remember that it is different from the regular declaration of a string. For example

Advertisements
var name = "Spiderman";
var myFriend = new String('Ganesh');
console.log(typeof name); //output = String
console.log(typeof myFriend); //output = Object

Popular Methods: String Class

Math Class

The math class has no constructors to create math object. However, it has many useful constants and functions that can be used in mathematical expressions.

Math Constants

Math.PI;
3.141592653589793 
Math.SQRT2;
1.4142135623730951 
Math.E; // Euler constant
2.718281828459045 
Math.LN2; // Neperian log of 2
0.6931471805599453 
Math.LN10; // Neperian log of 10
2.302585092994046

Popular Math Functions

  • Math.random()
  • Math.min(a, b) or Math.max(a,b)
  • Math.round()
  • Math.ceil()
  • Math.floor()
  • Math.pow()
  • Math.sqrt()

Trigonometric Functions

  • sin()
  • cos()
  • tan()
  • atan()
  • atan2(dy,dx)

Date Class

The date class has a constructor that returns current date. You can change the format of the dates using this parameters.

var date = new Date();
console.log(date.toString());
//output= "Tue Nov 05 2019 23:10:35 GMT+0530 (India //Standard Time)"

Useful Method of Date Class

  • Date() // constructor returns current date and time
  • date.getDay()
  • date.getMonth()
  • date.getHours()
  • date.getFullYear()
  • date.getMinutes()
  • date.getSeconds()
  • date.getTime()
  • date.getMilliseconds()

All of the above methods have an equivalent set method.

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.