Javascript Object and Number

Javascript Object and Number

In this blog, we are going to explore Objects and Numbers in Javascript

·

5 min read

What is an Object in Javascript?

As we have seen before Variables are the container to store day values. Likewise, objects also have a variable that can store many data values in it in name and value pairs(i.e., Property and property value)

Example:-

let obj = {
firstName : "Nandhini",
lastName : "Selvaraj"
};

How to access a property?

Syntax:-

Objectname.property;

Let us take an example to access the firstName property from obj(object)

console.log(obj.firstName);
//Nandhini

In the above example, we are accessing the firstName property from the object named obj and we got the output of Nandhini, which is the property value of the property that we tried to access.

What is the object method?

We can have a function as a property in an object and we can use it outside the object by calling the function.

Let us understand by example,

let obj = { 
fullname: function (){
     //code goes here
}
};

Now we will see how to access a function outside an object. It is the same as a normal function call statement, and we will use it to access a normal function.

let output = obj.fullname();

Here, if you didn't give () it will return the function object. So, we need to call that function to execute the function and return the output.

Let us understand by example, the object method

let details = {
firstName: "Anusha",
lastName: "V",
fullName: function(){
    let fullName = this.firstName.concat(" ", this.lastName);
    return fullName;
}
};

let output = details.fullname();
console.log(output);

Here, we have used this keyword.

What is this keyword?

  • this keyword is nothing but an object.

  • Inside a function, this refers to the global object.

  • More understandably, in our example, this keyword refers to the firstName and lastName objects inside a fullName method, because the fullName method is inside a details object(i.e., we are using a firstName and lastName objects which are properties of details object, and the same we are trying to access in fullName method which is the property of details object, so we are using this keyword to refer that).

What is a Number in Javascript?

  • A number has two Values with decimal and without decimal.

  • Though the number is a primitive value, Javascript considers them as Objects. So, Numbers have methods to access and manipulate them.

Number Methods:-

1.tostring():-

The tostring() method is used to convert numbers into strings with a base value of 16.

Let us understand by example,

let X = 123;
let Y = X.tostring();//123
console.log(typeof Y);//String

In the above example, we are converting a number into a string. If you check for the datatype using the typeof operator of Y, it will return as a string datatype.

2.toExponential():-

The toExponential() method is used to convert Numbers into exponential notation and the return output will be in the string.

Let us understand by example,

let X = 5.64;
console.log(X.toExponential());//5.64e+0

In the above example, we are converting 5.64 a decimal value to the exponential notation and it will return the output in string.

3.tofixed():-

The tofixed() method is used to convert Numbers into strings and we will give a number as a parameter to the function to specify how many decimal places wanted to be returned.

Let us understand by example,

let X = 5.6467;
console.log(X.tofixed(2));//5.65

In the above example, it will convert Numbers into strings by specifying a fixed number of decimal values needed to be returned and it will also round off the value while returning an output.

4.toprecision():-

The toprecision() method is used to convert Numbers into strings and we will give a number as a parameter to the function to specify, how many lengths of value it should be returned.

Let us understand by example,

let X = 5.6467;
console.log(X.toprecision(2));//5.65

In the above example, it will convert Numbers into strings and also specify length value as a parameter.

Converting a variable into a Number:-

There are three methods to convert variables into Numbers are,

  • Number()
  • parseInt()
  • parsefloat()

1.Number():-

This method is used to convert the argument given inside Number() method into the Number.

Let us understand by example,

let X = Number(“10”);
console.log(X); //10
console.log(typeof X);//Number

In the above example, we are converting a string into a number. We can also check its datatype by typeof Operator.

2.parseInt():-

parseInt() method will parse through the string(i.e., the argument passed inside parseInt()) and convert it into an integer value and if the decimal value is given as an argument, it will roundoff and return the output.

Let us understand by example,

let X = parseInt(“10”);
console.log(X);//10
let Y = parseInt(“10.55”);
console.log(Y);//10

In the above example, we are converting a string into a number(i.e., only as an integer).

3.parseFloat():-

parseFloat() is similar to parseInt() method. The only difference if we were given a decimal value inside an argument, it will return the decimal value as it is.

Let us understand by example,

let X = parseFloat(“10”);
console.log(X);//10
let Y = parseFloat(“10.55”);
console.log(Y);//10.55

In the above example, we are converting a string into a number(i.e., both as integer and floating-point).

Note:- All the methods that are used to convert variables into Numbers can convert only which has a number as an argument and they can’t convert a sequence of characters into numbers.

In the next blog, we will explore Arrays in Javascript.