Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

How to use Number Methods in JavaScript?

Number Methods

  • toString ( )
  • toExponential ( )
  • toFixed ( )
  • toPrecision ( )
  • valueOf( )
  • isFinite( )
  • isInteger( )
  • isNan( )
  • isSafeInteger( )

toString( )

toString ( ) method returns a number as a string in other words it converts a number into a string. We can use this method to output numbers as hexadecimal (16), octal(8), binary(2).

Syntax: –
Variable_name.toString( );

toExponential ( )

The toExponential() method converts a number into an exponential notation.

Syntax:-
Variable_name.toExponential(y)
Where y is an integer between 0 and 20 representing the number of digits in the
notation after the decimal point. If omitted, it is set to as many digits as necessary to
represent the value.

Ex: –

toFixed ( )

The toFixed() method converts a number into a string, keeping a specified number of decimals also rounds the decimal. If the desired number of decimals is higher than the actual number, nulls are added to create the desired decimal length.

Syntax: –
a.toFixed(y)
Where y is the number of digits after the decimal point. Default is 0 (no digits after the decimal point)

Syntax: –
a.toFixed(y)
Where y is the number of digits after the decimal point. Default is 0 (no digits after the decimal point)

Ex:-

toPrecision ( )

The toPrecision() method formats a number to a specified length.
A decimal point and nulls are added (if needed), to create the specified length.

Syntax:-
Variable_name.toPrecision(y)
Where y is the number of digits. If omitted, it returns the entire number (without any
formatting)

Syntax:-
Variable_name.toPrecision(y)
Where y is the number of digits. If omitted, it returns the entire number (without any
formatting)

Ex:-

Number.isNaN()

The Number.isNaN() method determines whether a value is NaN (Not-A-Number).
This method returns true if the value is of the type Number, and equates to NaN. Otherwise, it returns false.
Number.isNaN() is different from the global isNaN() function. The global isNaN() function
converts the tested value to a Number, then tests it.
Number.isNaN() does not convert the values to a Number, and will not return true for any value
that is not of the type Number.

Number.isInteger( )

The Number.isInteger() method determines whether a value is an integer.
This method returns true if the value is of the type Number, and an integer, otherwise it
returns false.

Ex: –

Number.isSafeInteger()

The Number.isSafeInteger() method determines whether a value is a safe integer.
A safe integer is an integer that can be exactly all integers from (2^53 – 1) to -(2^53 – 1)
This method returns true if the value is of the type Number and a safe integer.
Otherwise, it returns false.

Tagged : / / /

Private Properties and Methods in JavaScript

Private Properties and Methods

Using var or let or const you can create private properties and methods.

Ex: –
this.price
var price
let price

Prototype

Every object has an internal prototype that gives it its structure. This internal prototype is a reference to an object describing the code and data that all objects of that same type will have in common.

Prototype Object

Every object is associated with another Object in JavaScript

Prototype Object

Every object is associated with another object in JavaScript.

Note – Prototype Object of Object.prototype is null

Prototype Object

Every object is associated with another object in JavaScript.

Note – Prototype Object of Object.prototype is null

Prototype Object

Every object is associated with another object in JavaScript.

Note – Prototype Object of Array.prototype is Object.prototype and
Prototype Object of Object.prototype is null

Prototype Object

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 : / / / / / /