0%

how-value-convert-to-string

How many ways to convert a value to string in JavaScript

1. value.toString()

1
2
const num = 123;
const str = num.toString();

2. String(value)

1
2
3
const num = 123;
const str = String(num);
console.log(str);

3. value + ‘’

1
2
const num = 123;
const str = num + '';

4. `${value}`

const num = 123;
const str = `${num}`;

5. JSON.stringify(value)

1
2
const num = 123;
const str = JSON.stringify(num);

What’s the difference between them?