toString() in JS

tostring()-in-js

Hello Guys today i will be discussing about toString() method in Javascript.

Lets get started…

toString() method gives us the string representation of array,number,strings and booleans.

Code Example

const number = 12345;
const string = "Javascript";
const boolean = true
const array = [1,2,3];
const mixedArray = [1,2,3,"HTML","CSS"]
let nestedArray = [1, 2, 3, ["Html", "JS"]];


const numberToString = number.toString()
const stringToString = string.toString()
const booleanToString = boolean.toString()
const arrayToString = array.toString()
const mixedToString = mixedArray.toString()
const nestedToString = nestedArray.toString()


console.log(typeof numberToString)
console.log(typeof stringToString)
console.log(typeof booleanToString)
console.log(typeof arrayToString)
console.log(typeof mixedToString)
console.log(typeof nestedToString)

console.log(numberToString)
console.log(stringToString)
console.log(booleanToString)
console.log(arrayToString)
console.log(mixedToString)
console.log(nestedToString)

Output –

string
string
string
string
string
string
12345
Javascript
true
1,2,3
1,2,3,HTML,CSS
1,2,3,Html,JS
  • As you can see we have converted the array,number,strings and booleans to string.

Exceptions –

const Null = null
const Undefined = undefined
const object = {name:"shubham",age:21}


const undefinedToString = Undefined.toString()
const nullToString = Null.toString()
const objectToString = object.toString()

console.log(objectToString)

Output –

TypeError: Cannot read properties of undefined (reading 'toString')
TypeError: Cannot read properties of null(reading 'toString')
[object Object]
  • As you can see it can’t read the undefined and null.
  • For the object , it returns the [object,object] because it is the default string representation of object datatype.To solve this ,we will use another method which is JSON.stringify().

JSON.stringify() –

const object = {name:"shubham",age:21}
const objectToString = JSON.stringify(object)
console.log(objectToString)

Output –

{"name":"shubham","age":21}
  • Now it returns the string representation of our object.

Using in comparison –

const number = 12345;
const string = "12345";

const compare = number === string;
//false due to different datatype
const compare = number.toString() === string;
//true due to same datatype
  • You can see we can convert the number to string and then compare with strict equality operator and it returned true due to same datatype

THANK YOU FOR CHECKING THIS POST
^^You can help me by some donation at the link below Thank you👇👇 ^^
☕ –> https://www.buymeacoffee.com/waaduheck <--

Also check these posts as well
https://dev.to/shubhamtiwari909/javascript-map-with-filter-2jgo

https://dev.to/shubhamtiwari909/e-quotes-3bng

https://dev.to/shubhamtiwari909/deploy-react-app-on-netlify-kl

Total
3
Shares
Leave a Reply

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

Previous Post
cheat-sheet-for-react-part-i-(updated-august-2022)

Cheat sheet for React Part I (Updated August 2022)

Next Post
speaker-free-pass-giveaway-–-developerweek-cloud-2022-(sept-7-14)

Speaker Free Pass Giveaway – DeveloperWeek Cloud 2022 (Sept 7-14)

Related Posts