Skip to content
the poor coder

javascript toString and call

Poor Man
Published
Reading time
2 min read
On this page

What are javascript toString and call methods?#

JavaScript is a programming language that allows developers to manipulate data in various ways. It provides many built-in methods that help developers to perform different operations. Two of these methods are the toString() and call() methods.

The toString() Method#

The toString() method is a built-in method in JavaScript that converts a value to a string. This method can be used on any type of data, such as numbers, strings, booleans, and objects. When called on a number, for example, the method will return the number as a string:

JavaScript

var num = 10;
console.log(num.toString()); // "10"
    

It is also possible to pass a radix parameter to the toString() method to specify the base of the number system to be used:

JavaScript

var num = 10;
console.log(num.toString(2)); // "1010"
    

This will convert the number to its binary representation.

The call() Method#

The call() method is another built-in method in JavaScript that allows you to call a function with a specified this value and arguments provided individually. This method can be used to invoke a function with a different object as the this value:

JavaScript

var obj1 = {name: "John"};
var obj2 = {name: "Jane"};

function sayHello() {
  console.log("Hello, " + this.name + "!");
}

sayHello.call(obj1); // "Hello, John!"
sayHello.call(obj2); // "Hello, Jane!"
    

In the example above, we define two objects, obj1 and obj2, and a function, sayHello(), that uses the this value to access the name property of the object. We then use the call() method to invoke the sayHello() function with the obj1 and obj2 objects as the this value, respectively.

The call() method can also be used to pass arguments to the function:

JavaScript

function multiply(a, b) {
  return a * b;
}

console.log(multiply.call(null, 2, 3)); // 6
    

In this example, we call the multiply() function with the null object as the this value and pass two arguments, 2 and 3. The result is 6.

Conclusion#

The toString() and call() methods are important built-in methods in JavaScript that allow developers to manipulate data in different ways. By using these methods, developers can convert values to strings and invoke functions with a specified this value and arguments.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC