Object Methods in JavaScript

Objects are amazing, as these could encapsulate data inside them in form of property-value pairs. If in case nothing can be done with these objects then these would be useless in code. But there does exist many methods/functions which can be invoked on objects in JavaScripts. For example – hasOwnProperty() method.

Moreover if your not much familiar with JavaScript Objects then please first go through following articles before moving on further reading this article.

Some Common JavaScript Object’s Methods
toString() method
toLocaleString() method
toJSON() method
valueOf() method
isPrototypeOf() method
Examples of JavaScript Object’s Methods

toString() Method

JavaScript’s toString() method return value of JavaScript Object on which it’s invoked, it doesn’t take any parameter itself. Moreover toString() method can in general be used for converting JavaScript object to string.

• If identity upon which toString() method is called is Primitive Data Type itself, then JavaScript would take its value.
• But if identity on which toString() method is called is a Wrapper Object then JavaScript will unwrap that first into some primitive type and then will return value of that.

Below are some code examples explaining How to use toString() method in JavaScript?

var box = [1, 4, 5, 10, 11];
box.toString();

// This will return "1, 4, 5, 10, 11"

Note – Almost every object made from some built-in JavaScript Constructor have toString() method. Not only that a Developer can also define a toString() method for custom object.

How to define toString() method for an object?

Defining a toString() method is super easy just need to have toString() => { return statment; } inside Object’s constructor itself. Below is a code example explaining this –

var website_details = {
    name: "Computer Science Hub",
    description: "Programming Website",
    toString: () => {
        return "Computer Science Hub is a coding website";
    }
}

userDetails.toString(); 
// This will return "Computer Science Hub is a coding website"

toLocaleString() method

Some JavaScript object’s does have internally defined toLocaleString() method, which formats string inside object into human readable text.

• toLocaleString() method for Date JavaScript Object return human readable data like in YYYY-MM-DD format.

• toLocaleString() method for Number class format numbers in such a way that it easy to read by human being. Like 0.000000001 will be formated as
10-9.

Owing to making object’s values more readable toLocaleString() method is quite useful. Below are some code examples explaining How to use toLocaleString() method in JavaScript?

let date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));

// toLocaleString() without arguments depends on the
// implementation, the default locale, and the default time zone
console.log(date.toLocaleString());
// → "12/11/2012, 7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles

toJSON method in JavaScript

toJSON() method returns a JSON object when called upon a JavaScript Object(only if object have toJSON() method). For example – Date Object in JavaScript.

JavaScript valueOf() method

valueOf method is kind of similar to toString() method in JavaScript, but instead of returning string value of an object, valueOf returns number. Even if some function requires a parameter and it need to a number but an object is passed onto it, then JavaScript internally would manage this by calling valueOf() method on object first afterthat uses value returned from valueOf() as parameter of function. (Seems Quite Interesting) Below is a code example explaining How to use valueOf() method in JavaScript?

// parameter need to be a primitive data type(string, number or Boolean)
function i_am_function(parameter) {

}

i_am_function(some_object);
// But parameter being passed is a object
// So JavaScript internally will call some_object.valueOf() first
// take that value and pass that to i_am_function as a parameter

Moreover if object upon which valueOf() method is called is a Wrapper Object, then valueOf() will convert it to equivalent primitive data type.
• String Object will return string value
• Number Object will return number value
• Boolean Object will return boolean value

If your not aware of JavaScript’s Primitive Data types, then please go through following articles –
String Object in JavaScript
• JavaScript’s Number Objects
Boolean Objects in JavaScript

Calling valueOf() method on String Object

var website = "Computer Science Hub";
var name = website.valueOf();
console.log(name);
// name will be Computer Science Hub

Calling valueOf() method on Number Object

var age = 100;
var your_age = age.valueOf();
console.log(your_age);
// your_age will be 100

Calling valueOf() method on Boolean Object

var bool = 1;
var is_it_true = bool.valueOf();
console.log(is_it_true);
// is_it_true will be true

Conclusion

Methods like toString(), toLocaleString(), toJSON(), valueOf() are quite handy and easy to use in JavaScript for getting information out of JavaScript Objects. Depending upon what you want your code to do, you can use any one of these. Moreover if you got stuck anywhere or just unable to figure out which is appropriate then please comment down below, I would be happy to solve your issue.

Also if your learning JavaScript Programming Language, then I have put together many other articles related to it. If your interested then you can check those out here – Computer Science Hub JavaScript Programming Language.

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts