Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a number to string in coffeescript?

Given a number

n = 42

What is the best way to convert it into a string?

s = String(n)

or

s = ''+n

or any better suggestion?

like image 667
Michael_Scharf Avatar asked Nov 14 '13 16:11

Michael_Scharf


2 Answers

String interpolation might be the most natural approach in CoffeeScript:

s = "#{n}" # Just `'' + n` in disguise.

That might leave people wondering what you're doing though.

like image 87
mu is too short Avatar answered Oct 04 '22 21:10

mu is too short


I think the best way would be:

(10).toString()
// or
n = 11;
n.toString()

Edited to fix syntax error. 10.toString() works in the CoffeeScript simulator but it's better to be safe.

like image 30
aust Avatar answered Oct 04 '22 21:10

aust