Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join() vs toString() vs valueOf() in Javascript [closed]

Tags:

javascript

Can anyone tell me the exact difference between join() and toString() and valueOf()in Javascript. When and at what situation we need to use them?

like image 699
Vinoth Babu Avatar asked Jun 24 '13 07:06

Vinoth Babu


People also ask

What is the difference between toString and valueOf in JavaScript?

If the hint is String , then toString is used before valueOf . But, if the hint is Number , then valueOf will be used first. Note that if only one is present, or it returns a non-primitive, it will usually call the other as the second choice.

What is the opposite of toString in JavaScript?

toString() method when called on a number is the opposite of parseInt, meaning it converts the decimal to any number system between 2 and 36.

What is the use of toString in JavaScript?

The toString() method returns a string as a string. The toString() method does not change the original string. The toString() method can be used to convert a string object into a string.

Which method converts an array to string in JavaScript?

toString() The toString() method returns a string representing the specified array and its elements.


1 Answers

toString is a method that you can find not only in Arrays, but in every object.

join allows you to convert every element of the Array object in to a string, and joining together using a separator. It behaves just like toString for Arrays, but you can also specify the separator:

var classes = ["first", "second"];
classes.join(" ");    // "first second"
like image 172
MaxArt Avatar answered Nov 06 '22 14:11

MaxArt